<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="" />
								<el-tooltip effect="dark" :content="data.projectAddress || data.address" placement="top-start">
									<span class="middleSize">{{ data.projectAddress || data.address }}</span>
								</el-tooltip>
							</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" link @click="downloadTemp(row)">
							<img src="@/assets/images/tableIcon/下载附件.png" alt="" class="configureIcon" />
							<span>资料下载</span>
						</el-button>
					</template>
					<template #examineState="{ row }">
						{{ row.examineState === 1 ? "待审核" : row.examineState === 2 ? "审核驳回" : "审核通过" }}
					</template>
					<template #state="{ row }">
						<span>{{ row.state == 1 ? "已验收" : "未验收" }}</span>
					</template>
				</ProTable>
			</div>
		</div>
	</div>
</template>

<script setup lang="tsx" name="acceptancePlan">
import { ref, reactive, onMounted, watch } from "vue";
import { ElMessage } from "element-plus";
import { ColumnProps } from "@/components/ProTable/interface";
import ProTable from "@/components/ProTable/index.vue";
import { acceptancePlanPage } from "@/api/modules/goverment";
import LeftMenu from "@/components/LeftMenu/LeftMenu.vue";
import { acceptancePlanProList, acceptancePlanEngList } from "@/api/modules/goverment";
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 baseUrl = import.meta.env.VITE_API_URL;
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref();

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

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

	{ prop: "typeName", label: "类型" },
	{ prop: "singleProject", label: "单体工程" },
	{ prop: "planCheckDate", label: "计划验收日期", width: 150 },
	{ prop: "startFlag", label: "起始标识" },
	{ prop: "endFlag", label: "截止标识" },
	{ prop: "headPerson", label: "验收负责人", width: 120 },
	{ prop: "headPersonPhone", label: "负责人电话", width: 120 },
	{ prop: "state", label: "状态" },
	{ prop: "completeDate", label: "实际验收完成日期", width: 150 },
	{ prop: "remark", label: "备注" },
	{ prop: "operation", width: 120, 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 acceptancePlanProList(pages.value);
	records.value = result.records;
	pages.value.total = Number(result.total);
};
// 获取工程名称分页
const getEngPage = async () => {
	const { result } = await acceptancePlanEngList(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 acceptancePlanProList({ projectName: params, ...pages.value });
		records.value = result.records;
	} else {
		const { result } = await acceptancePlanEngList({ engineeringName: params, ...pages.value });
		records.value = result.records;
	}
	proTable.value.getTableList();
};
// leftMenu页面的分页
const onCurChange = async (params: number) => {
	if (active.value === 0) {
		const { result } = await acceptancePlanProList({ ...pages.value, pageNo: params });
		records.value = result.records;
		pages.value.total = +result.total;
	} else {
		const { result } = await acceptancePlanEngList({ ...pages.value, pageNo: params });
		records.value = result.records;
		pages.value.total = +result.total;
	}
};
// 下载
const downloadTemp = (row: any) => {
	if (row.fileUrl) {
		window.open(baseUrl + "/xmgl/file/preview?fileUrl=" + row.fileUrl);
	} else {
		ElMessage.error("暂无可下载文件");
	}
};
// 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 acceptancePlanPage(newParams);
};
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>
