<template>
	<div class="table-box">
		<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="handleEditItem(row)">
					<img src="@/assets/images/tableIcon/updateIcon.png" alt="" class="configureIcon" />
					<span>编辑</span>
				</el-button>
			</template>
		</ProTable>
		<changeTheme />
		<!-- 新增全景计划 -->
		<DialogForm
			:title="newTitle"
			:formConfig="formConfig"
			:formData="formData"
			v-model:visible="visible"
			append-to-body
			width="700px"
			@confirm="saveItem"
		>
		</DialogForm>
	</div>
</template>

<script setup lang="tsx" name="ProjectSupervisionRecord">
import { ref, reactive, nextTick, computed, onMounted } 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 { getRoleList, getTreemRoleList, getTreeByIdList } from "@/api/modules/project";
import { GlobalStore } from "@/stores";
import DialogForm from "@/components/DialogForm/index.vue";
import type { FormRules, FormInstance, UploadInstance } from "element-plus";
import { getDicList } from "@/api/modules/jxjview";
import { globalPlanAdd, globalPlanEdit, globalPlanList } from "@/api/modules/huizhou";

const globalStore = GlobalStore();
const baseUrl = import.meta.env.VITE_API_URL;
const fileList = ref([]);
const headers = ref({ Authorization: "Bearer " + globalStore.token });
const ruleFormRef = ref<FormInstance>();
const rules = reactive<FormRules>({
	name: {
		required: true,
		message: "请输入",
		trigger: "blur"
	}
});
const childrenVisible = ref(false);
const subVisible = ref(false);
// 弹窗中的配置
const formConfig = {
	formItemConfig: [
		{
			label: "节点编码",
			prop: "nodeNumber",
			type: "input"
		},
		{
			label: "节点名称",
			prop: "nodeName",
			type: "input"
		},
		{
			label: "节点类型",
			prop: "type",
			type: "input"
		},
		{
			label: "标准完成时间",
			prop: "standardCompleteTime",
			type: "date"
		},
		{
			label: "计划完成时间",
			prop: "planCompleteTime",
			type: "date"
		},
		{
			label: "预计完成时间",
			prop: "expectCompleteTime",
			type: "date"
		},
		{
			label: "实际完成时间",
			prop: "realCompleteTime",
			type: "date"
		},
		{
			label: "状态",
			prop: "state",
			type: "select",
			data: [],
			clearable: true
		},
		{
			label: "实际偏差",
			prop: "actualDeviation",
			type: "input"
		},
		{
			label: "职能线条",
			prop: "phaseName",
			type: "input"
		},
		{
			label: "节点负责人编号",
			prop: "chargerNumber",
			type: "input"
		},
		{
			label: "节点负责人",
			prop: "chargerName",
			type: "input"
		}
	],
	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 treeRef = ref(null);

const datas = reactive([]);
const router = useRouter();
const visible = ref(false);
const drawerVisible = ref(false);
const title = ref("");
const newTitle = ref("新增");
const formData = ref({
	nodeNumber: "",
	nodeName: "",
	type: "",
	standardCompleteTime: "",
	planCompleteTime: "",
	expectCompleteTime: "",
	realCompleteTime: "",
	state: null,
	actualDeviation: "",
	phaseName: "",
	chargerNumber: "",
	chargerName: ""
});
const statusList: any = ref([]);
const DicMainList = ref([]);
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref();

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

// 表格配置项
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: "createTime",
	// 	label: "实际完成时间",
	// 	isShow: false,
	// 	search: {
	// 		el: "date-picker",
	// 		span: 2,
	// 		props: { type: "month", 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: "操作", fixed: "right" }
];
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);
	}
	statusList.value.length = 0;
	statusList.value.push(...result);
};
// 新增，编辑数据
const saveItem = async (form: any) => {
	if (form.id) {
		// console.log(form.dictCode);
		const res = await globalPlanEdit(form);
		proTable.value.getTableList();
		ElMessage.success("编辑成功");
	} else {
		const res = await globalPlanAdd(form);
		ElMessage.success("新增成功");
		proTable.value.getTableList();
	}
	visible.value = false;
};
const uploadSuccess = (response: any) => {
	ElMessage.success("上传成功");
	// formData.value.videoUrl = response.result.url;
	// fileList.value = [{ name: response.result.originalFilename, url: response.result.downloadPath }];
};
const onRemove: UploadProps["onRemove"] = (file, uploadFiles) => {
	// formData.value.videoUrl = "";
	fileList.value = reactive([]);
};
const saveSubItem = async (form: any) => {
	// if (form.roleId) {
	// 	// console.log(form.dictCode);
	// 	const res = await editRole(form);
	// 	proTable.value.getTableList();
	// 	ElMessage.success("编辑成功");
	// } else {
	// 	const res = await addRole(form);
	// 	ElMessage.success("新增成功");
	// 	proTable.value.getTableList();
	// }
	visible.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.realCompleteTime) {
		newParams.realCompleteTime_begin = newParams.realCompleteTime[0];
		newParams.realCompleteTime_end = newParams.realCompleteTime[1];
		delete newParams.realCompleteTime;
	}
	return globalPlanList(newParams);
};

const handleAddItem = () => {
	formData.value = reactive({
		nodeNumber: "",
		nodeName: "",
		type: "",
		standardCompleteTime: "",
		planCompleteTime: "",
		expectCompleteTime: "",
		realCompleteTime: "",
		state: null,
		actualDeviation: "",
		phaseName: "",
		chargerNumber: "",
		chargerName: ""
	});
	newTitle.value = "新增";
	// fileList.value = reactive([]);
	visible.value = true;
};

// 修改数据按钮
const handleEditItem = async (row: any) => {
	console.log(row);
	formData.value = reactive({ ...row });
	newTitle.value = "编辑";
	visible.value = true;
};
onMounted(() => {
	getDicMainList();
});
</script>
<style scoped lang="scss">
.table-box {
	height: 100%;
	position: relative;
	.imgDialog {
		.uploadBtn {
			color: white;
			font-size: 16px;
			border: 1px solid #087ba4;
			background: #087ba4;
		}
	}
}
:deep() {
	.el-form .el-form-item__content .el-range-editor {
		width: 300px;
	}
	.el-range-separator {
		color: var(--el-menu-text-color);
	}
}
</style>
