<template>
	<div class="warning-page">
		<LeftMenu
			v-model="active"
			:tabs="['项目名称', '工程名称']"
			:records="records"
			@change-page="onCurChange"
			@search="onSearchInput"
			:pageable="pages"
			class="leftMenu"
		>
			<template #default="{ data }">
				<div class="leftProject" @click="onSearch(data)">
					<span class="projectName">{{
						(data as ResAiProjectPage).projectName || (data as ResAiEngineerPage).engineeringName
					}}</span>
					<div class="leftMenu_item">
						<div class="video">
							<img style="margin-right: 5px" src="@/assets/images/AIwaring/dustMap.png" alt="" />
							<span class="middleSize">{{ data.projectAddress || data.address }}</span>
						</div>
					</div>
				</div>
			</template>
		</LeftMenu>
		<div class="table-box">
			<div class="table">
				<ProTable
					ref="proTable"
					title="用户列表"
					:columns="columns"
					:requestApi="getTableList"
					:dataCallback="dataCallback"
					:tool-button="false"
					:pagination="true"
					background
					:isShowSearch="false"
					:onReset="true"
				>
					<!-- 表格操作 -->
					<template #operation="{ row }">
						<el-button type="primary" link @click="handleItemDetail(row)">
							<img src="@/assets/images/tableIcon/look.png" alt="" class="configureIcon" />
							<span>查看</span>
						</el-button>
					</template>
				</ProTable>
				<DialogForm
					:title="title"
					:formConfig="formConfig"
					:formData="formData"
					v-model:visible="visible"
					append-to-body
					width="36%"
					@confirm="saveItem"
				>
				</DialogForm>
			</div>
		</div>
	</div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted, watch } from "vue";
import { GlobalStore } from "@/stores";
import LeftMenu from "@/components/LeftMenu/LeftMenu.vue";
import type { ResAiProjectPage, ResAiEngineerPage } from "@/api/types/government/AIwaring";
import ProTable from "@/components/ProTable/index.vue";
import DialogForm from "@/components/DialogForm/index.vue";
import { ColumnProps } from "@/components/ProTable/interface";
import { globalPlanGovermentList, globalPlanProList, globalPlanEngList } from "@/api/modules/huizhou";
import { getDicList } from "@/api/modules/jxjview";
const pages = ref({
	pageNo: 1,
	pageSize: 7,
	total: 0
});
const records = ref<ResAiProjectPage[] | ResAiEngineerPage[]>([]);
const DicMainList = ref([]);
const active = ref(0);
const store = GlobalStore();
// 表格配置项
const columns: ColumnProps[] = [
	{ type: "index", label: "序号", width: 80 },
	{
		prop: "nodeNumber",
		label: "节点编号",
		width: 150
	},
	// 多级 prop
	{ prop: "nodeName", label: "节点名称", width: 150, search: { el: "input" } },
	{ prop: "type", label: "节点类型", width: 150 },
	{ prop: "standardCompleteTime", label: "标准完成时间", width: 150 },
	{ prop: "planCompleteTime", label: "计划完成时间", width: 150 },
	{ prop: "expectCompleteTime", label: "预计完成时间", width: 150 },
	{
		prop: "state",
		label: "状态",
		width: 150,
		isShow: false,
		search: { el: "select" },
		enum: DicMainList.value,
		fieldNames: { label: "dictValue", value: "dictLabel" }
	},
	{
		prop: "status",
		label: "状态",
		width: 150
	},
	{
		prop: "realCompleteTime",
		label: "实际完成时间",
		width: 150,
		search: {
			el: "date-picker",
			span: 2,
			props: { type: "daterange", format: "YYYY-MM-DD", valueFormat: "YYYY-MM-DD" }
			// defaultValue: "2023-05"
		}
	},
	{ prop: "phaseName", label: "智能条线", width: 150 },
	{ prop: "chargerNumber", label: "节点责任人编号", width: 150 },
	{ prop: "chargerName", label: "节点责任人", width: 150 },
	{
		prop: "isDeleted",
		label: "节点失效标记",
		width: 150,
		render: scoped => {
			return scoped.row.isDeleted == 0 ? "有效" : "失效";
		}
	},
	{
		prop: "isSendBack",
		label: "是否退回",
		width: 150,
		render: scoped => {
			return scoped.row.isSendBack == 1 ? "是" : "否";
		}
	},
	{ prop: "operation", label: "操作", width: 100, fixed: "right" }
];
interface formData {
	title: string;
	uploadTime: string;
	remark: string;
	videoUrl: string;
}
const formData = ref<formData>({
	title: "",
	uploadTime: "",
	remark: "",
	videoUrl: ""
});
const visible = ref(false);
const title = ref("新增");
// 搜索用的项目sn或者工程sn
const searchSn = ref("");
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref();
// 弹窗中的配置
const formConfig = {
	formItemConfig: [
		{
			label: "节点编码",
			prop: "nodeNumber",
			type: "input",
			disabled: true
		},
		{
			label: "节点名称",
			prop: "nodeName",
			type: "input",
			disabled: true
		},
		{
			label: "节点类型",
			prop: "type",
			type: "input",
			disabled: true
		},
		{
			label: "标准完成时间",
			prop: "standardCompleteTime",
			type: "date",
			disabled: true
		},
		{
			label: "计划完成时间",
			prop: "planCompleteTime",
			type: "date",
			disabled: true
		},
		{
			label: "预计完成时间",
			prop: "expectCompleteTime",
			type: "date",
			disabled: true
		},
		{
			label: "实际完成时间",
			prop: "realCompleteTime",
			type: "date",
			disabled: true
		},
		{
			label: "状态",
			prop: "state",
			type: "select",
			data: [],
			clearable: true,
			disabled: true
		},
		{
			label: "实际偏差",
			prop: "actualDeviation",
			type: "input",
			disabled: true
		},
		{
			label: "职能线条",
			prop: "phaseName",
			type: "input",
			disabled: true
		},
		{
			label: "节点负责人编号",
			prop: "chargerNumber",
			type: "input",
			disabled: true
		},
		{
			label: "节点负责人",
			prop: "chargerName",
			type: "input",
			disabled: true
		}
	],
	rules: {
		nodeNumber: [
			{
				required: true,
				message: "请输入",
				trigger: "blur"
			}
		],
		nodeName: [
			{
				required: true,
				message: "请输入",
				trigger: "blur"
			}
		],
		type: [
			{
				required: true,
				message: "请输入",
				trigger: "blur"
			}
		],
		standardCompleteTime: [
			{
				required: true,
				message: "请输入",
				trigger: "blur"
			}
		],
		planCompleteTime: [
			{
				required: true,
				message: "请输入",
				trigger: "blur"
			}
		],
		engineeringSn: [
			{
				required: true,
				message: "请输入",
				trigger: "change"
			}
		],
		phaseName: [
			{
				required: true,
				message: "请输入",
				trigger: "blur"
			}
		],
		chargerNumber: [
			{
				required: true,
				message: "请输入",
				trigger: "blur"
			}
		],
		chargerName: [
			{
				required: true,
				message: "请输入",
				trigger: "blur"
			}
		]
	}
};
// 项目或者工程名字
const searchName = ref<string>("");
const getDicMainList = async () => {
	// 获取状态字典
	const { result } = await getDicList({ dictType: "node_plan_state" });
	console.log(result);
	if (result.length > 0) {
		formConfig.formItemConfig[7].data = result.map(item => {
			return {
				label: item.dictValue,
				value: item.status
			};
		});
		DicMainList.value.length = 0;
		DicMainList.value.push(...result);
	}
};
// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total && pageNum && pageSize 这些字段，那么你可以在这里进行处理成这些字段
// 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
const dataCallback = (data: any) => {
	// console.log(data);
	return {
		list: data.records,
		total: Number(data.total),
		pageNo: Number(data.current),
		pageSize: Number(data.size)
	};
};
// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
	let newParams = JSON.parse(JSON.stringify(params));
	if (newParams.realCompleteTime) {
		newParams.realCompleteTime_begin = newParams.realCompleteTime[0];
		newParams.realCompleteTime_end = newParams.realCompleteTime[1];
		delete newParams.realCompleteTime;
	}
	if (active.value === 0) {
		newParams.projectSn = searchSn.value;
	} else {
		newParams.engineeringSn = searchSn.value;
	}
	return globalPlanGovermentList(newParams);
};
// 页面的项目名称和工程名称的div点击事件
const onSearch = async (params: ResAiProjectPage | ResAiEngineerPage) => {
	active.value === 0
		? (searchSn.value = (params as ResAiProjectPage).projectSn)
		: (searchSn.value = (params as ResAiEngineerPage).engineeringSn);
	active.value === 0
		? (searchName.value = (params as ResAiProjectPage).projectName)
		: (searchName.value = (params as ResAiEngineerPage).engineeringName);
	proTable.value.getTableList();
};
// leftMenu页面的搜索按钮
const onSearchInput = async (params: string) => {
	console.log(params);
	if (active.value === 0) {
		const { result } = await globalPlanProList({ projectName: params, ...pages.value });
		records.value = result.records;
	} else {
		const { result } = await globalPlanEngList({ engineeringName: params, ...pages.value });
		records.value = result.records;
	}
	proTable.value.getTableList();
};
// leftMenu页面的分页
const onCurChange = async (params: number) => {
	if (active.value === 0) {
		const { result } = await globalPlanProList({ ...pages.value, pageNo: params });
		records.value = result.records;
	} else {
		const { result } = await globalPlanEngList({ ...pages.value, pageNo: params });
		records.value = result.records;
		pages.value.total = +result.total;
	}
	pages.value.total = +res.result.total;
};
// 获取项目名称分页
const getProPage = async () => {
	const { result } = await globalPlanProList(pages.value);
	records.value = result.records;
	records.value.map(item => {
		let showGif = false;
		item.showGif = showGif;
	});
	pages.value.total = Number(result.total);
};
// 获取工程名称分页
const getEngPage = async () => {
	const { result } = await globalPlanEngList(pages.value);
	records.value = result.records;
	records.value.map(item => {
		let showGif = false;
		item.showGif = showGif;
	});
	pages.value.total = +result.total;
};
// 查看数据按钮
const handleItemDetail = async (row: any) => {
	console.log(row);
	formData.value = reactive({ ...row });
	title.value = "查看";
	visible.value = true;
};
// 新增，编辑数据
const saveItem = async (form: any) => {
	visible.value = false;
};
watch(
	() => active.value,
	async (value: number) => {
		pages.value.pageNo = 1;
		pages.value.total = 0;
		// console.log(value);

		if (value === 0) {
			await getProPage();
			onSearch(records.value[0]);
		} else {
			await getEngPage();
			onSearch(records.value[0]);
		}
	},
	{
		deep: true
	}
);
onMounted(async () => {
	getDicMainList();
	await getProPage();
	onSearch(records.value[0]);
	searchSn.value = (records.value as ResAiProjectPage[])[0].projectSn;
	searchName.value = (records.value as ResAiProjectPage[])[0].projectName;
});
</script>

<style lang="scss" scoped>
@import "./index.scss";
</style>
