<script lang="ts" setup name="SummaryDialog"> import type { Ref } from 'vue' import { defineExpose, nextTick, reactive, ref } from 'vue' import type { FormRules } from 'element-plus' import { ElMessage, ElMessageBox } from 'element-plus' import useUserStore from '@/store/modules/user' import { getAdminDept, getUserDept } from '@/api/system/user' import { getDeptTreeList } from '@/api/system/dept' import { toTreeList } from '@/utils/structure' const emits = defineEmits(['confirm']) const $router = useRouter() const userStore = useUserStore() const dataFormRef = ref() const dialogFormVisible = ref(false) // 对话框是否显示 const dataForm = ref({ commpany: '', commpanyId: '', deptId: '', deptName: '', year: `${new Date().getFullYear()}`, }) // 表单 const rules = ref<any>({ year: [{ required: true, message: '年度必选', trigger: ['blur', 'change'] }], }) // 前端校验规则) // 重置表单 const resetForm = () => { dataForm.value = { commpany: '', commpanyId: '', deptId: '', deptName: '', year: '', } } const commpanyList = ref<any[]>([]) const isAdmin = ref(false) const isDeptAdmin = ref(false) const fetchCommany = () => { if (userStore.roleNames.length === 1 && userStore.roleNames[0].includes('单位管理员')) { isDeptAdmin.value = true } else { isDeptAdmin.value = false } getUserDept().then((res) => { const fun = () => { if (commpanyList.value.length) { dataForm.value.deptId = '' dataForm.value.deptName = '' } else { dataForm.value.deptId = userStore.deptId dataForm.value.deptName = userStore.deptName } if (isAdmin.value) { rules.value.commpanyId = [{ required: true, message: '单位必选', trigger: ['blur', 'change'] }] } else { rules.value.commpanyId = undefined } } if (res.data.fullName === '顶级' || res.data.version === '1' || res.data.version === 1) { getAdminDept({}).then((res) => { commpanyList.value = res.data.map((item: any) => ({ id: item.id, value: item.id, name: item.fullName })) isAdmin.value = true fun() }) } else { dataForm.value.commpany = res.data.fullName dataForm.value.commpanyId = res.data.id fun() } }) } // 初始化对话框 const initDialog = () => { resetForm() fetchCommany() dialogFormVisible.value = true dataForm.value.year = `${new Date().getFullYear()}` } const deptList = ref<any[]>([]) // 部门列表 watch(() => dataForm.value.commpanyId, (newVal) => { dataForm.value.deptId = '' if (newVal) { getDeptTreeList({ pid: newVal }).then((res) => { deptList.value = toTreeList(res.data.map((item: any) => ({ ...item, label: item.name, value: item.id }))) }) } else { deptList.value = [] } }) // 确认 const saveData = () => { if (dataFormRef.value) { dataFormRef.value?.validate((valid: boolean) => { if (valid) { $router.push({ path: '/plan/summary', query: { data: JSON.stringify({ ...dataForm.value, isDeptAdmin: isDeptAdmin.value }), statusName: '', row: JSON.stringify({ id: '' }), }, }) } }) } } defineExpose({ initDialog, }) const cancel = () => { dialogFormVisible.value = false } // 年度下拉框 const yearList = ref<any>([]) const fetchList = () => { const currentYear = new Date().getFullYear() for (let index = 0; index < 6; index++) { yearList.value.push({ name: String(currentYear - index), value: String(currentYear - index), }) } } fetchList() </script> <template> <el-dialog v-model="dialogFormVisible" title="汇总年度计划" append-to-body width="600"> <el-form ref="dataFormRef" :rules="rules" :model="dataForm" label-position="right" label-width="60px"> <el-row :gutter="24"> <el-col :span="24"> <el-form-item label="单位" prop="commpanyId"> <el-select v-if="commpanyList.length" v-model="dataForm.commpanyId" clearable filterable placeholder="单位" style="width: 100%;"> <el-option v-for="item in commpanyList" :key="item.id" :label="item.name" :value="item.id" /> </el-select> <el-input v-else v-model.trim="dataForm.commpany" type="text" disabled /> </el-form-item> </el-col> </el-row> <el-row v-if="!isDeptAdmin" :gutter="24"> <el-col :span="24"> <el-form-item label="部门" prop="deptId"> <el-input v-if="!commpanyList.length" v-model.trim="dataForm.deptName" type="text" disabled /> <el-tree-select v-else v-model="dataForm.deptId" style="width: 100%;" :data="deptList" :render-after-expand="false" check-strictly placeholder="部门" /> </el-form-item> </el-col> </el-row> <el-row :gutter="24"> <el-col :span="24"> <el-form-item label="年度" prop="year"> <el-select v-model="dataForm.year" placeholder="必选"> <el-option v-for="item in yearList" :key="item.value" :label="item.name" :value="item.value" /> </el-select> </el-form-item> </el-col> </el-row> </el-form> <template #footer> <div class="dialog-footer"> <el-button type="primary" @click="saveData(dataFormRef)"> 确认 </el-button> <el-button @click="cancel"> 取消 </el-button> </div> </template> </el-dialog> </template> <style lang="scss" scoped> .el-dialog { width: 700px; } .el-select { width: 100%; } </style>