<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.projectName || data.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>
					</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" v-if="row.state != 1 && row.state != 2" link @click="handleItemDetails(row)">
							<img src="@/assets/images/tableIcon/look.png" alt="" class="configureIcon" />
							<span>查看</span>
						</el-button>
						<el-button type="primary" v-if="row.state == 1" link @click="handleAudit(row)">
							<img src="@/assets/images/tableIcon/seal.png" alt="" class="configureIcon" />
							<span>资料审批</span>
						</el-button>
						<el-button type="primary" v-if="row.state == 2" link @click="handleCheck(row)">
							<img src="@/assets/images/tableIcon/check.png" alt="" class="configureIcon" />
							<span>现场检查</span>
						</el-button>
					</template>
					<template #examineState="{ row }">
						{{ row.examineState === 1 ? "待审核" : row.examineState === 2 ? "审核驳回" : "审核通过" }}
					</template>
				</ProTable>
			</div>
		</div>
	</div>
	<detailsDialog
		v-model:dialogVisible="dialogVisible"
		:title="newTitle"
		:relativeId="relativeId"
		@confirm="confirmReform"
	></detailsDialog>
</template>

<script setup lang="tsx" name="ProjectSupervisionRecord">
import { ref, reactive, onMounted, watch } from "vue";
import { ColumnProps } from "@/components/ProTable/interface";
import ProTable from "@/components/ProTable/index.vue";
import detailsDialog from "./components/detailsDialog.vue";
import { GlobalStore } from "@/stores";
import LeftMenu from "@/components/LeftMenu/LeftMenu.vue";
import { acceptanceApplyProList, acceptanceApplyEngList, acceptanceApplyPage } from "@/api/modules/goverment";
import { ElMessage } from "element-plus";
const pages = ref({
	pageNo: 1,
	pageSize: 7,
	total: 0
});
// 搜索用的项目sn或者工程sn
const searchSn = ref("");
const searchName = ref("");
const records = ref([]);
const active = ref(0);
const globalStore = GlobalStore();
const newTitle = ref("新增");
const relativeId = ref();
const dialogVisible = ref(false);
const engList = ref([]);
const engVisable = ref(false);
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref();
const engDrawer = ref();

// 表格配置项
const columns: ColumnProps[] = [
	{ type: "index", label: "序号", width: 80 },

	// 多级 prop
	{ prop: "stage", label: "验收阶段", search: { el: "input" } },

	{ prop: "engineeringName", label: "工程名称" },
	{ prop: "singleProjectNum", label: "单体工程数量" },
	{ prop: "typeName", label: "类型" },
	{ prop: "remark", label: "备注" },
	{
		prop: "state",
		label: "状态",
		enum: [
			{ label: "审批中", value: 1 },
			{ label: "资料验收通过", value: 2 },
			{ label: "已驳回", value: 3 },
			{ label: "已验收通过", value: 4 },
			{ label: "已重新发起", value: 5 }
		]
	},
	{ prop: "operation", label: "操作", fixed: "right" }
];
// 页面的项目名称和工程名称的div点击事件
const onSearch = async (params: any) => {
	active.value === 0 ? (searchSn.value = params.projectSn) : (searchSn.value = params.engineeringSn);
	active.value === 0 ? (searchName.value = params.projectName) : (searchName.value = params.engineeringName);
	proTable.value.getTableList();
};

// 获取项目名称分页
const getProPage = async () => {
	const { result } = await acceptanceApplyProList(pages.value);
	records.value = result.records;
	pages.value.total = Number(result.total);
};
// 获取工程名称分页
const getEngPage = async () => {
	const { result } = await acceptanceApplyEngList(pages.value);
	records.value = result.records;
	pages.value.total = +result.total;
};
// leftMenu页面的搜索按钮
const onSearchInput = async (params: string) => {
	console.log(params);
	if (active.value === 0) {
		const { result } = await acceptanceApplyProList({ projectName: params, ...pages.value });
		records.value = result.records;
	} else {
		const { result } = await acceptanceApplyEngList({ engineeringName: params, ...pages.value });
		records.value = result.records;
	}
	proTable.value.getTableList();
};
// leftMenu页面的分页
const onCurChange = async (params: number) => {
	if (active.value === 0) {
		const { result } = await acceptanceApplyProList({ ...pages.value, pageNo: params });
		records.value = result.records;
		pages.value.total = +result.total;
	} else {
		const { result } = await acceptanceApplyEngList({ ...pages.value, pageNo: params });
		records.value = result.records;
		pages.value.total = +result.total;
	}
};
// 整改完成，全部已提交审核
const confirmReform = () => {
	proTable.value.getTableList();
};
// 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 (searchSn.value) {
		if (active.value == 0) {
			newParams.projectSn = searchSn.value;
		} else {
			newParams.engineeringSn = searchSn.value;
		}
	} else {
		return { result: { current: "1", pages: "1", records: [], size: "10", total: "0" } };
	}
	return acceptanceApplyPage(newParams);
};
// 查看详情按钮
const handleItemDetails = (row: any) => {
	console.log(row);
	relativeId.value = row.id;
	newTitle.value = "详情";
	dialogVisible.value = true;
};
// 资料审批按钮
const handleAudit = async (row: any) => {
	relativeId.value = row.id;
	newTitle.value = "资料审批";
	dialogVisible.value = true;
};
// 现场检查按钮
const handleCheck = async (row: any) => {
	relativeId.value = row.id;
	newTitle.value = "现场检查";
	dialogVisible.value = true;
};
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();
	searchSn.value = records.value[0].projectSn;
	searchName.value = records.value[0].projectName;
	onSearch(records.value[0]);
});
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>
