<template>
	<div class="store-box">
		<div class="menu-content">
			<div class="top-search">
				<el-input placeholder="请输入" v-model="bigForm.name" @input="getBigData" />
				<!-- <span>安全检测库--大项</span> -->
				<el-button type="primary" @click="opeateBigItem(1)">增加大项</el-button>
			</div>
			<div class="big-option">
				<el-tree :data="bigData" :highlight-current="true" node-key="id" default-expand-all :expand-on-click-node="false">
					<template #default="{ node, data }">
						<span class="custom-tree-node" @click.capture="selectTree(data)">
							<span>{{ data.name }}</span>
							<el-button type="primary" link @click="opeateBigItem(2, data)">
								<img src="@/assets/images/tableIcon/updateIcon.png" alt="" class="configureIcon" />
								<span>编辑</span>
							</el-button>
							<el-button type="danger" style="margin-right: 5px" link :icon="Delete" @click="deleteBigItem(data)">删除</el-button>
						</span>
					</template>
				</el-tree>
			</div>
		</div>
		<div class="table-content">
			<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 :disabled="!treeData.id" class="addButtonStyle" @click="handleAddItem(1)">新增</el-button>
				</template>
				<template #operation="{ row }">
					<el-button type="primary" link @click="handleAddItem(2, row)">
						<img src="@/assets/images/tableIcon/updateIcon.png" alt="" class="configureIcon" />
						<span>编辑</span>
					</el-button>
					<el-button type="danger" link :icon="Delete" @click="deleteSmallItem(row)">删除</el-button>
				</template>
			</ProTable>
		</div>
		<DialogForm
			:title="bigTitle"
			:formConfig="formBigConfig"
			:formData="formBigData"
			v-model:visible="bigVisible"
			append-to-body
			width="700px"
			@confirm="saveBigItem"
		>
		</DialogForm>
		<DialogForm
			:title="smallTitle"
			:formConfig="formSmallConfig"
			:formData="formSmallData"
			v-model:visible="smallVisible"
			append-to-body
			width="700px"
			@confirm="saveSmallItem"
		>
		</DialogForm>
	</div>
</template>

<script setup lang="tsx" name="ProjectSupervisionRecord">
import { ref, reactive, nextTick, onMounted } from "vue";
import { ElMessage, ElMessageBox, ElTree, FormInstance } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps } from "@/components/ProTable/interface";
import {
	bigStore,
	smallStore,
	bigStoreAdd,
	bigStoreUpdate,
	bigStoreDelete,
	smallStoreAdd,
	smallStoreUpdate,
	smallStoreDelete
} from "@/api/modules/goverment";
import DialogForm from "@/components/DialogForm/index.vue";
import { Delete } from "@element-plus/icons-vue";
import { jxj_User } from "@/api/types";
import { useHandleData } from "@/hooks/useHandleData";

const bigTitle = ref("");
const smallTitle = ref("");
const formBigData = ref({
	name: ""
});
const formSmallData = ref({
	name: "",
	description: ""
});
const bigVisible = ref(false);
const smallVisible = ref(false);
const bigData = ref([]);
const bigForm = ref({
	name: ""
});
// 大项弹窗中的配置
const formBigConfig = {
	formItemConfig: [
		{
			label: "大项名称",
			prop: "name",
			type: "input"
		}
	]
};
// 小项弹窗中的配置
const formSmallConfig = {
	formItemConfig: [
		{
			label: "小项名称",
			prop: "name",
			type: "input"
		},
		{
			label: "描述",
			prop: "description",
			type: "input"
		}
	]
};
// 表格配置项
const columns: ColumnProps[] = [
	{ type: "index", label: "序号", width: 80 },
	{
		prop: "name",
		label: "小项名称",
		search: { el: "input" }
	},
	{
		prop: "description",
		label: "描述"
	},
	{ prop: "operation", label: "操作", fixed: "right", width: 260 }
];

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref();
const treeData = ref({});
const selectTree = data => {
	// 选择大项数节点
	console.log(data);
	treeData.value = data;
	proTable.value.getTableList();
};
// 删除大项
const deleteBigItem = async (params: jxj_User.ResUserList) => {
	await useHandleData(bigStoreDelete, { id: params.id }, `删除【${params.name}】`);
	getBigData();
};
// 删除小项
const deleteSmallItem = async (params: jxj_User.ResUserList) => {
	await useHandleData(smallStoreDelete, { id: params.id }, `删除【${params.name}】`);
	proTable.value.getTableList();
};
const opeateBigItem = (index, row = {}) => {
	console.log(row);
	if (index === 1) {
		bigTitle.value = "新增大项";
		formBigData.value = reactive({
			name: ""
		});
	} else {
		bigTitle.value = "编辑大项";
		formBigData.value = reactive({ ...row });
	}
	bigVisible.value = true;
};
const saveBigItem = async (form: any) => {
	form.type = 1;
	if (form.id) {
		// console.log(form.dictCode);
		const res = await bigStoreUpdate(form);
		getBigData();
		ElMessage.success("编辑成功");
	} else {
		const res = await bigStoreAdd(form);
		ElMessage.success("新增成功");
		getBigData();
	}
	bigVisible.value = false;
};
const saveSmallItem = async (form: any) => {
	form.type = 1;
	form.libraryId = treeData.value.id;
	if (form.id) {
		// console.log(form.dictCode);
		const res = await smallStoreUpdate(form);
		proTable.value.getTableList();
		ElMessage.success("编辑成功");
	} else {
		const res = await smallStoreAdd(form);
		ElMessage.success("新增成功");
		proTable.value.getTableList();
	}
	smallVisible.value = false;
};
const handleAddItem = (index: number, row: any) => {
	if (index === 1) {
		smallTitle.value = "新增小项";
		formSmallData.value = reactive({
			name: "",
			description: ""
		});
	} else {
		smallTitle.value = "编辑小项";
		formSmallData.value = reactive({ ...row });
	}
	smallVisible.value = true;
};
const getBigData = async () => {
	// 大项列表
	const { result } = await bigStore({ name: bigForm.value.name, type: 1 });
	bigData.value = result;
};
// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
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 (treeData.value.id) {
		newParams.libraryId = treeData.value.id;
		console.log(newParams);
		return smallStore(newParams);
	} else {
		return { result: { current: "1", pages: "1", records: [], size: "10", total: "0" } };
	}
};
// 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)
	};
};
onMounted(() => {
	getBigData();
});
</script>
<style scoped lang="scss">
.store-box {
	display: flex;
	height: 100%;
	border-radius: 8px;
	.menu-content {
		width: 290px;
		height: 100%;
		margin-right: 20px;
		border-radius: 8px;
		background-color: white;
		.top-search {
			display: flex;
			align-items: center;
			margin: 15px 12px 22px 12px;
			span {
				margin-right: auto;
				font-size: 14px;
				font-family: Source Han Sans CN-Regular, Source Han Sans CN;
				font-weight: 400;
				color: #333333;
			}
			:deep(.el-input) {
				margin-right: 12px;
			}
		}
		.big-option {
			min-height: 83%;
			// padding: 0 10px;
			background-color: #ffffff;
			.custom-tree-node {
				width: 100%;
				display: flex;
				align-items: center;
				> span:first-child {
					margin-right: auto;
				}
			}
			:deep(.el-tree-node__content) {
				height: 35px !important;
			}
		}
	}
	.table-content {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		:deep(.table-search) {
			margin-bottom: 20px;
			padding: 20px 20px 0px 20px !important;
		}
		:deep(.table-main) {
			height: 88.3%;
		}
	}
}
</style>
