import { h, ref, onMounted, reactive, watch, toRaw } from "vue"; import { request } from "@/utils/request"; import { Search } from "@element-plus/icons-vue"; import DetailDialog from "./detailDialog.vue"; import EditDialog from "./editDialog.vue"; import AddDialog from "./addDialog.vue"; const treeData = ref([]); const treeProps = { children: "children", label: "name", key: "id", } const filterText = ref(''); // 树节点过滤条件 const tableData = ref([]); const qxmc = ref(''); // 区县名称 const yhzid = ref(''); // 养护站id const filterData = reactive({ sbmc: '', sbdl: '', sblx: '', sbzt: '', }); // 表格过滤条件 const pagination = reactive({ current: 1, pageSize: 10, total: 0, pageSizes: [10, 20, 50], layout: "prev, pager, next, jumper", onChange: (page, pageSize) => { pagination.current = page; pagination.pageSize = pageSize; getyhzsbList(qxmc.value, yhzid.value, filterData); }, }); // 表格分页 const modelVisible = ref(false); // 弹窗显示状态 const model = reactive({ title: '', content: null, props: {}, onCancel: null, onConfirm: null, }); // 弹窗内容 const dialogType = ref(''); // 弹窗类型 const dialogRef = ref(null); // 弹窗实例 const INIT_FORM = { rid: "", qxmc: "", sbbh: "", sbdl: "", sbmc: "", sblx: "", sbxh: "", sbwz: "", jd: "", wd: "", glry: "", glryid: "", czy: "", czyid: "", gzrq: "", gmfy: "", sbzt: "", sccj: "", sfyjsb: "", sfnrsjbz: "", fsfw: "", yhzid: "", }; // 表单初始值 const form = reactive({ ...INIT_FORM }); // 表单 // 节点过滤函数 const filterNode = (value, node) => { const keyword = (value || '').toLowerCase(); const nodeData = node || {} // 处理区域节点匹配 if (nodeData.type === 'area') { return (nodeData.rawName || '').toLowerCase().includes(keyword) } // 处理站点节点匹配 if (nodeData.type === 'site') { const parentData = node.parent?.data?.type === 'area' ? node.parent.data : {} const parentMatch = (parentData.rawName || '').toLowerCase().includes(keyword) return parentMatch || (nodeData.name || '').toLowerCase().includes(keyword) } return false } // 获取养护站列表分组 const getTreeData = async () => { try { const res = await request({ url: '/api/yhz/listAreaGroup', method: 'GET' }) if (res.code === '00000') { treeData.value = Object.entries(res.data).map(([areaName, sites]) => ({ id: areaName, name: `${areaName}(${sites.length})`, type: 'area', children: sites.map(site => ({ id: site.id, name: site.mc, type: 'site' })), rawName: areaName // 原始名称 })); console.log('treeData', toRaw(treeData.value)) } else { throw new Error(res.message); } } catch (error) { ElMessage.error(error.message); console.log(error); } }; // 处理节点点击事件 const handleNodeClick = (data, node) => { if (node.expanded === false) { console.log('树节点关闭', node.expanded) yhzid.value = ''; // 重置养护站id qxmc.value = ''; // 重置区县名称 return; } if (data.type === 'area') { console.log('你点击的是区县', data.id) yhzid.value = ''; // 重置养护站id qxmc.value = data.id; // 保存区县名称 } if (data.type === 'site') { console.log('你点击的是站点', data.name) yhzid.value = data.id; // 保存养护站id qxmc.value = ''; // 重置区县名称 } }; // 获取养护站设备列表 const getyhzsbList = async (qxmc, yhzid, filterData) => { try { const data = { qxmc: qxmc, yhzid: yhzid, sbmc: filterData?.sbmc || '', sbdl: filterData?.sbdl || '', sblx: filterData?.sblx || '', sbzt: filterData?.sbzt || '', pageNum: pagination.current, pageSize: pagination.pageSize, } const res = await request({ url: '/api/yjsb/list', method: 'GET', params: data, }) if (res.code === '00000') { tableData.value = res.data.records; pagination.total = res.data.total; } else { tableData.value = []; pagination.total = 0; throw new Error(res.message); } } catch (error) { ElMessage.error(error.message); console.log(error); } }; const columns = [ { prop: 'sbzt', label: '设备状态', }, { prop: 'sbmc', label: '设备名称', }, { prop: 'sbbh', label: '设备编号', }, { prop: 'sbxh', label: '设备型号', }, { prop: 'sbdl', label: '设备大类', }, { prop: 'sblx', label: '设备类型', }, { prop: 'gzrq', label: '购置日期', }, { prop: 'gmfy', label: '购买费用(万元)', }, { prop: 'qxmc', label: '所属区县', }, { label: "操作", fixed: "right", width: 150, render: (row) => () => h("div", { class: "action-btns" }, [ h( ElButton, { type: "primary", link: true, onClick: async () => { dialogType.value = 'detail' await getDetailData(row); }, }, () => "详情" ), h( ElButton, { type: "primary", link: true, onClick: async () => { dialogType.value = 'edit' await getDetailData(row); }, }, () => "编辑" ), h( ElButton, { type: "danger", link: true, onClick: async () => { try { await ElMessageBox.confirm("确定要删除该设备吗?", "删除确认", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning", }); const res = await request({ url: `/api/yjsb/${row.rid}`, method: 'DELETE', }); if (res.code === '00000') { ElMessage.success('删除成功'); getyhzsbList(qxmc.value, yhzid.value, filterData); } else { throw new Error(res.message); } } catch (error) { ElMessage.error(error.message); console.log(error); } }, }, () => "删除" ), ]), }, ] const handleEdit = async () => { try { await dialogRef?.value?.dynamicComponentRef?.formRef.validate(); console.log('form', toRaw(form)) const res = await request({ url: '/api/yjsb', method: 'PUT', data: toRaw(form), }); if (res.code === '00000') { ElMessage.success('编辑成功'); dialogType.value = ''; modelVisible.value = false; getyhzsbList(qxmc.value, yhzid.value, filterData); } else { throw new Error(res.message); } } catch (error) { ElMessage.error(error.message); console.log('error', error) } }; // 获取养护站设备详情 const getDetailData = async (row) => { try { const res = await request({ url: `/api/yjsb/${row.rid}`, method: 'GET', }); if (!res || res.code !== '00000') { throw new Error('获取物资详情失败') } if (res.code === '00000') { if (dialogType.value === 'detail') { model.title = `设备详情`; model.content = DetailDialog; model.props = { detailData: res.data, }; model.onCancel = () => { dialogType.value = ''; modelVisible.value = false; }; model.onConfirm = () => { dialogType.value = ''; modelVisible.value = false; }; modelVisible.value = true; } if (dialogType.value === 'edit') { model.title = `编辑设备`; model.content = EditDialog; Object.assign(form, res.data); model.props = { detailData: res.data, form: form, }; model.onCancel = () => { dialogType.value = ''; modelVisible.value = false; }; model.onConfirm = async () => { await handleEdit(); }; modelVisible.value = true; } } else { throw new Error(res.message); } } catch (error) { ElMessage.error(error.message); console.log(error); } }; // 打开新增设备弹窗 const openAddEquipmentModel = () => { model.title = `新增设备`; model.content = AddDialog; Object.assign(form, INIT_FORM); model.props = { detailData: {}, form: form, }; model.onCancel = () => { dialogType.value = ''; modelVisible.value = false; }; model.onConfirm = async () => { try { console.log('form', toRaw(form)) const res = await request({ url: '/api/yjsb', method: 'POST', data: toRaw(form), }); if (res.code === '00000') { ElMessage.success('新增成功'); dialogType.value = ''; modelVisible.value = false; getyhzsbList(qxmc.value, yhzid.value, filterData); } else { throw new Error(res.message); } } catch (error) { ElMessage.error(error.message); console.log('error', error) } }; modelVisible.value = true; } export default () => { const treeRef = ref(null); watch(() => filterText.value, (val) => { treeRef.value.filter(val || '') }) watch( [() => filterData, qxmc, yhzid], ([newFilterData, newQxmc, newYhzid]) => { getyhzsbList(newQxmc, newYhzid, newFilterData) }, { deep: true } ) onMounted(() => { getTreeData(); getyhzsbList(); }); return { treeRef, treeData, treeProps, filterText, filterNode, handleNodeClick, tableData, filterData, pagination, columns, modelVisible, dialogRef, model, openAddEquipmentModel, } }