<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 #investmentPaymentId="scope">
						<span>{{ scope.row.investmentPaymentId ? "已支付" : "未支付" }}</span>
					</template>
					<template #operation="{ row }">
						<el-button type="primary" link @click="handleItemDetail(2, row)">
							<img src="@/assets/images/tableIcon/updateIcon.png" alt="" class="configureIcon" />
							<span>查看</span>
						</el-button>
					</template>
				</ProTable>
				<!-- 新增 -->
				<DialogForm
					title="新增"
					:formConfig="formConfig"
					:formData="formData"
					v-model:visible="openVisible"
					append-to-body
					width="700px"
					@confirm="saveItem"
				>
				</DialogForm>
				<!-- 投资支付详情 -->
				<transformInfo1 v-model:detailsDialog="detailsDialog" :relativeId="relativeId"></transformInfo1>
			</div>
		</div>
	</div>
</template>

<script setup lang="tsx" name="ProjectSupervisionRecord">
import { ref, reactive, nextTick, computed, onMounted, watch } from "vue";
import { ElMessage, ElMessageBox, UploadProps } from "element-plus";
import { useRouter } from "vue-router";
import { ColumnProps } from "@/components/ProTable/interface";
import ProTable from "@/components/ProTable/index.vue";
import changeTheme from "@/components/changeTheme/index.vue";
import { applyTimeGovermentList, applyTimeAdd, payGovermentEngList, payGovermentProList } from "@/api/modules/goverment";
import { GlobalStore } from "@/stores";
import DialogForm from "@/components/DialogForm/index.vue";
import type { FormRules, FormInstance, UploadInstance } from "element-plus";
import transformInfo1 from "./components/transformInfo1.vue";
import LeftMenu from "@/components/LeftMenu/LeftMenu.vue";
const relativeId = ref("");
const detailsDialog = ref(false);
const globalStore = GlobalStore();
const openVisible = ref(false);
const active = ref(0);
const records = ref([]);
// 弹窗中的配置
const formConfig = {
	formItemConfig: [
		{
			label: "申报名称",
			prop: "name",
			type: "input"
		},
		{
			label: "申报时段",
			prop: "applyTime",
			type: "date",
			format: "YYYY-MM-DD",
			valueFormat: "YYYY-MM-DD"
		}
	],
	rules: {
		name: [
			{
				required: true,
				message: "请输入",
				trigger: "blur"
			}
		],
		applyTime: [
			{
				required: true,
				message: "请选择",
				trigger: "change"
			}
		]
	}
};
const treeRef = ref(null);

const datas = reactive([]);
const router = useRouter();
const visible = ref(false);
const drawerVisible = ref(false);
const title = ref("");
const formData = ref({
	name: "",
	applyTime: ""
});
const pages = ref({
	pageNo: 1,
	pageSize: 7,
	total: 0
});
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref();
// 搜索用的项目sn或者工程sn
const searchSn = ref("");
const searchName = ref("");

const getId = ref<number | undefined>(undefined);

// 表格配置项
const columns: ColumnProps[] = [
	{ type: "index", label: "序号", width: 80 },
	// 多级 prop
	{ prop: "projectName", label: "项目名称" },
	{ prop: "name", label: "申报时段名称", search: { el: "input" } },
	{ prop: "applyTime", label: "申报时段" },
	{ prop: "investmentPaymentId", label: "支付状态" },
	// {
	// 	prop: "contractTime",
	// 	label: "合同签订日期",
	// 	width: 150,
	// 	search: {
	// 		el: "date-picker",
	// 		props: {
	// 			type: "daterange",
	// 			format: "YYYY-MM-DD",
	// 			valueFormat: "YYYY-MM-DD"
	// 			// defaultTime: defaultTime2
	// 		}
	// 	}
	// },
	{ prop: "operation", label: "操作", fixed: "right", width: 120 }
];
// 页面的项目名称和工程名称的div点击事件
const onSearch = async (params: any) => {
	console.log(params);
	if (active.value === 0) {
		searchSn.value = params ? params.projectSn : "";
		searchName.value = params ? params.projectName : "";
	} else {
		searchSn.value = params ? params.engineeringSn : "";
		searchName.value = params ? params.engineeringName : "";
	}
	proTable.value.getTableList();
};
// leftMenu页面的搜索按钮
const onSearchInput = async (params: string) => {
	console.log(params);
	if (active.value === 0) {
		const { result } = await payGovermentProList({ projectName: params, ...pages.value });
		records.value = result.records;
	} else {
		const { result } = await payGovermentEngList({ engineeringName: params, ...pages.value });
		records.value = result.records;
	}
};
// leftMenu页面的分页
const onCurChange = async (params: number) => {
	if (active.value === 0) {
		const { result } = await payGovermentProList({ ...pages.value, pageNo: params });
		records.value = result.records;
	} else {
		const { result } = await payGovermentEngList({ ...pages.value, pageNo: params });
		records.value = result.records;
		pages.value.total = +result.total;
	}
	pages.value.total = +res.result.total;
};
const handleAddItem = () => {
	formData.value = reactive({
		name: "",
		applyTime: ""
	});
	openVisible.value = true;
};
const saveItem = async (form: any) => {
	if (form.id) {
		// console.log(form.dictCode);
		const res = await applyTimeAdd(form);
		proTable.value.getTableList();
		ElMessage.success("编辑成功");
	} else {
		const res = await applyTimeAdd(form);
		ElMessage.success("新增成功");
		proTable.value.getTableList();
	}
	openVisible.value = false;
};
// 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 transfrom = (arr: Array<any>) => {
	const result = [];
	return arr
		.map(item => {
			if (arr.children && Array.isArray(arr.children)) {
				result.concat(transfrom(arr.children));
			}

			return {
				authorityId: item.name,
				roleId: getId.value,
				type: item.type
			};
		})
		.concat(result);
};

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
	let newParams = JSON.parse(JSON.stringify(params));
	if (newParams.contractTime) {
		newParams.contractTime_begin = newParams.contractTime[0];
		newParams.contractTime_end = newParams.contractTime[1];
		delete newParams.contractTime;
	}
	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 applyTimeGovermentList(newParams);
};

const handleItemDetail = (index: number, row: any) => {
	console.log(row);
	// if (index === 1) {
	// 	title.value = "新增角色";
	// 	formData.value = reactive({});
	// } else {
	// 	title.value = "编辑角色";
	// 	formData.value = reactive({ ...row });
	// }
	relativeId.value = row.id;
	detailsDialog.value = true;
};
// 获取项目名称分页
const getProPage = async () => {
	const { result } = await payGovermentProList(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 payGovermentEngList(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[0].projectSn;
	searchName.value = records.value[0].projectName;
});
</script>
<style scoped lang="scss">
@import "./index.scss";
</style>
