<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="leftMenu_item flx-justify-between">
							<div style="margin-top: 5px" class="flx-justify-between">
								<img style="margin-right: 5px" src="@/assets/images/AIwaring/dustMap.png" alt="" />
								<span class="middleSize">{{ data.projectAddress || data.address }}</span>
							</div>
							<!-- <div>
							<img src="@/assets/images/AIwaring/报警.png" alt="" />
							<span class="middleSize">今日报警次数：{{ data.todayAlarm }}</span>
						</div> -->
						</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 #formButton="scope">
						<el-button class="btnStyle" @click="handleAddItem">新增</el-button>
					</template>
					<template #operation="{ row }">
						<el-button type="primary" link @click="handleLookItem(row)">
							<img src="@/assets/images/tableIcon/look.png" alt="" class="configureIcon" />
							<span>查看</span>
						</el-button>
					</template>
					<template #state="{ row }">
						<span>{{
							row.state == 1
								? "执法中"
								: row.state == 2
								? "待整改"
								: row.state == 3
								? "待审核"
								: row.state == 4
								? "已闭合"
								: row.state == 5
								? "已驳回"
								: ""
						}}</span>
					</template>
					<template #slippage="{ row }">
						<span :style="{ color: row.slippage ? 'red' : 'var(--el-menu-text-color)' }">{{
							row.slippage ? row.slippage : ""
						}}</span>
					</template>
				</ProTable>
			</div>
			<!-- 执法单信息 -->
			<transformInfo v-model:detailsDialog="detailsDialog" :relativeId="relativeId" @confirm="allConfirm"></transformInfo>
			<!-- 执法单新增 -->
			<orderAdd v-model:orderDialog="orderDialog" @confirm="allConfirm"></orderAdd>
		</div>
	</div>
</template>

<script setup lang="tsx" name="ProjectSupervisionRecord">
import { ref, reactive, nextTick, onMounted, watch } from "vue";
import { ElMessage, ElMessageBox, ElTree, FormInstance } from "element-plus";
import { ColumnProps } from "@/components/ProTable/interface";
import ProTable from "@/components/ProTable/index.vue";
import transformInfo from "./components/transformInfo.vue";
import orderAdd from "./components/orderAdd.vue";
import { statisticsInfo, statisticsTable } from "@/api/modules/goverment";
import LeftMenu from "@/components/LeftMenu/LeftMenu.vue";
import type { ResAiProjectPage, ResAiEngineerPage } from "@/api/types/government/AIwaring";
import { getDustprojectPage, getDustengineeringPage, getAIQuestionPage } from "@/api/modules/goverment";
import { sendIframeMessage } from "@/utils/util";
import { GlobalStore } from "@/stores";
const pages = ref({
	pageNo: 1,
	pageSize: 7,
	total: 0
});
const store = GlobalStore();
const records = ref<ResAiProjectPage[] | ResAiEngineerPage[]>([]);
const statisticsOption = ref([
	{
		name: "累计下达整改单",
		value: 0,
		img: new URL("@/assets/images/govermentImg/整改单.png", import.meta.url).href,
		prop: "inspectNum"
	},
	{
		name: "累计下达停工单",
		value: 0,
		img: new URL("@/assets/images/govermentImg/停工单.png", import.meta.url).href,
		prop: "shutdownNum"
	},
	{
		name: "累计问题数",
		value: 0,
		img: new URL("@/assets/images/govermentImg/问题数.png", import.meta.url).href,
		prop: "questionNum"
	},
	{
		name: "累计闭合问题数",
		value: 0,
		img: new URL("@/assets/images/govermentImg/闭合问题数.png", import.meta.url).href,
		prop: "solveNum"
	},
	{
		name: "总检查次数",
		value: 0,
		img: new URL("@/assets/images/govermentImg/总检查次数.png", import.meta.url).href,
		prop: "recordNum"
	}
]);
// 表格配置项
const columns: ColumnProps[] = [
	{ type: "index", label: "序号", width: 80 },
	{
		prop: "engineeringName",
		label: "工程名称",
		search: { el: "input" },
		width: 150
	},
	{
		prop: "inspectUserName",
		label: "监督员",
		width: 150
	},
	{
		prop: "createTime",
		label: "检查时间",
		width: 220
	},
	{
		prop: "buildEnt",
		label: "建设单位",
		width: 150
	},
	{
		prop: "supervisorEnt",
		label: "监理单位",
		width: 150
	},
	{
		prop: "opEnt",
		label: "施工单位",
		width: 150
	},
	{
		prop: "surveyEnt",
		label: "勘察单位",
		width: 150
	},
	{
		prop: "designEnt",
		label: "设计单位",
		width: 150
	},
	{
		prop: "state",
		label: "状态",
		search: { el: "select" },
		width: 150,
		enum: [
			{ label: "执法中", value: 1 },
			{ label: "待整改", value: 2 },
			{ label: "待审核", value: 3 },
			{ label: "已闭合", value: 4 },
			{ label: "已驳回", value: 5 }
		]
	},
	{ prop: "slippage", label: "逾期情况(天)", width: 150 },
	{ prop: "operation", label: "操作", fixed: "right", width: 100 }
];
const pageable = ref({
	pageNo: 1,
	pageSize: 12,
	total: 0
});
const active = ref(0);
const detailsDialog = ref(false);
const orderDialog = ref(false);
const relativeId = ref("");
// 搜索用的项目sn或者工程sn
const searchSn = ref("");
const searchName = ref("");
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref();
// 新增执法单
const handleAddItem = () => {
	orderDialog.value = true;
};
// 页面的项目名称和工程名称的div点击事件
const onSearch = async (params: ResAiProjectPage | ResAiEngineerPage) => {
	const { result } = await getAIQuestionPage(
		active.value === 0
			? { projectSn: (params as ResAiProjectPage).projectSn, pageNo: pageable.value.pageNo, pageSize: pageable.value.pageSize }
			: {
					engineeringSn: (params as ResAiEngineerPage).engineeringSn,
					pageNo: pageable.value.pageNo,
					pageSize: pageable.value.pageSize
			  }
	);
	pageable.value.total = Number(result.total);
	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();
};
// 全部审核完成
const allConfirm = () => {
	proTable.value.getTableList();
};
const handleLookItem = row => {
	console.log(row);
	relativeId.value = row.id;
	detailsDialog.value = true;
};
// leftMenu页面的搜索按钮
const onSearchInput = async (params: string) => {
	console.log(params);
	if (active.value === 0) {
		const { result } = await getDustprojectPage({ projectName: params, ...pages.value });
		records.value = result.records;
	} else {
		const { result } = await getDustengineeringPage({ engineeringName: params, ...pages.value });
		records.value = result.records;
	}
};
// leftMenu页面的分页
const onCurChange = async (params: number) => {
	if (active.value === 0) {
		const { result } = await getDustprojectPage({ ...pages.value, pageNo: params });
		records.value = result.records;
	} else {
		const { result } = await getDustengineeringPage({ ...pages.value, pageNo: params });
		records.value = result.records;
		pages.value.total = +result.total;
	}
	pages.value.total = +res.result.total;
};
const getTableList = (params: any) => {
	let newParams = JSON.parse(JSON.stringify(params));
	console.log(newParams);
	if (newParams.createTime) {
		newParams.createTime_begin = newParams.createTime[0];
		newParams.createTime_end = newParams.createTime[1];
		delete newParams.createTime;
	}
	if (active.value === 0) {
		newParams.projectSn = searchSn.value;
	} else {
		newParams.engineeringSn = searchSn.value;
	}
	newParams.type = 2;
	return statisticsTable(newParams);
};
// 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)
	};
};
// 获取项目名称分页
const getProPage = async () => {
	const { result } = await getDustprojectPage(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 getDustengineeringPage(pages.value);
	records.value = result.records;
	records.value.map(item => {
		let showGif = false;
		item.showGif = showGif;
	});
	pages.value.total = +result.total;
};
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 () => {
	await getProPage();
	onSearch(records.value[0]);
	searchSn.value = (records.value as ResAiProjectPage[])[0].projectSn;
	searchName.value = (records.value as ResAiProjectPage[])[0].projectName;
	await sendIframeMessage({ obj: { path: store.path, moduleId: store.moduleId } }, 2, undefined);
});
</script>
<style scoped lang="scss">
@import "./index.scss";
</style>
