<!-- 新增地点弹窗 --> <script lang="ts" setup name="addNotice"> import type { FormInstance, FormRules } from 'element-plus' import { ElLoading, ElMessage } from 'element-plus' import type { IForm, IList } from './place-interface' import { addLocationList, updateLocationList } from '@/api/laboratory/place' import useUserStore from '@/store/modules/user' const emits = defineEmits(['refresh']) const textMap: { [key: string]: string } = { edit: '编辑', add: '新建', detail: '详情', }// 字典 const user = useUserStore() // 用户信息 const ruleFormRef = ref<FormInstance>() // from组件 const form = ref<IForm>({ createTime: '', // 创建时间 createUser: '', // 创建人 createUserId: '', // 创建人id locationName: '', // 地点名称 locationNo: '', // 地点编号 locationPosition: '', // 地点位置 locationLab: '', // 地点实验室 }) // 表单 const rules = ref<FormRules>({ locationNo: [{ required: true, message: '地点编号不能为空', trigger: 'blur' }], locationName: [{ required: true, message: '地点名称不能为空', trigger: 'blur' }], locationPosition: [{ required: true, message: '地点位置不能为空', trigger: 'blur' }], locationLab: [{ required: true, message: '所属实验室不能为空', trigger: 'blur' }], }) // 表单验证规则 const dialogVisible = ref(false) // 弹窗显示 const pageType = ref('') // 页面类型 add新增、edit编辑 const infoId = ref('') // id,主键 const loading = ref(false) const locationLabList = [ { id: '西昌实验室', name: '西昌实验室', }, { id: '海口实验室', name: '海口实验室', }, ] // ----------------------------------------方法------------------------------------------------- // 清空表单 const clear = () => { form.value = { createTime: '', // 创建时间 createUser: '', // 创建人 createUserId: '', // 创建人id locationName: '', // 地点名称 locationNo: '', // 地点编号 locationPosition: '', // 地点位置 locationLab: '', // 地点实验室 } } // 弹窗初始化 const initDialog = (pageTypeParam = 'detail', row: IList) => { dialogVisible.value = true // 打开弹窗 pageType.value = pageTypeParam // 页面类型 form.value.createUserId = user.id// 创建人id form.value.createUser = user.name // 创建人名字 if (pageTypeParam === 'edit') { // 编辑时初始化数据 form.value.locationName = row.locationName // 地点名称 form.value.locationNo = row.locationNo // 地点编号 form.value.locationPosition = row.locationPosition // 地点位置 form.value.locationLab = row.locationLab // 地点实验室 infoId.value = row.id // 主键 } else if (pageTypeParam === 'add') { form.value.locationName = '' // 地点名称 form.value.locationNo = '' // 地点编号 form.value.locationPosition = '' // 地点位置 form.value.locationLab = '' // 地点实验室 infoId.value = '' // 主键 } } // 提交 const submitForm = async (formEl: FormInstance | undefined) => { ruleFormRef.value!.validate((valid: boolean) => { if (valid) { const params = { ...form.value, id: infoId.value, } loading.value = true // 新建 if (pageType.value === 'add') { // 新建 addLocationList(params).then((res) => { ElMessage.success('保存成功') pageType.value = 'detail' loading.value = false resetForm(ruleFormRef.value) emits('refresh') }).catch(() => { loading.value = false resetForm(ruleFormRef.value) }) } // 保存 else if (pageType.value === 'edit') { // 编辑 updateLocationList(params).then((res) => { ElMessage.success('保存成功') pageType.value = 'detail' loading.value = false emits('refresh') resetForm(ruleFormRef.value) }).catch(() => { loading.value = false resetForm(ruleFormRef.value) }) } } else { console.log('表单校验不通过') } }) } // 取消 function resetForm(formEl: FormInstance | undefined) { formEl?.resetFields() dialogVisible.value = false } defineExpose({ initDialog }) </script> <template> <el-dialog v-if="dialogVisible" v-model="dialogVisible" :title="`${textMap[pageType]}地点`" append-to-body width="65%" class="container" @close="resetForm"> <el-form ref="ruleFormRef" v-loading="loading" :model="form" :rules="rules" label-position="right" label-width="110px"> <el-form-item label="地点编号:" prop="locationNo"> <el-input v-model="form.locationNo" :disabled="pageType === 'detail'" placeholder="请输入地点编号" /> </el-form-item> <el-form-item label="地点名称:" prop="locationName"> <el-input v-model="form.locationName" :disabled="pageType === 'detail'" placeholder="请输入地点名称" /> </el-form-item> <el-form-item label="地点位置:" prop="locationPosition"> <el-input v-model="form.locationPosition" type="textarea" autosize :disabled="pageType === 'detail'" placeholder="请输入地点位置" /> </el-form-item> <el-form-item label="所属实验室:" prop="locationLab"> <el-select v-model="form.locationLab" placeholder="请选择所属实验室" filterable clearable :disabled="pageType === 'detail'" style="width: 100%;" > <el-option v-for="item in locationLabList" :key="item.id" :label="item.name" :value="item.id" /> </el-select> </el-form-item> </el-form> <template #footer> <div class="dialog-footer footer"> <el-button :loading="loading" type="primary" @click="submitForm(ruleFormRef)"> 确定 </el-button> <el-button :disabled="loading" type="info" @click="resetForm(ruleFormRef)"> 取消 </el-button> </div> </template> </el-dialog> </template> <style lang="scss" scoped> .container { .isDetail { ::v-deep { .el-form-item.is-required:not(.is-no-asterisk) .el-form-item__label-wrap > .el-form-item__label::before, .el-form-item.is-required:not(.is-no-asterisk) > .el-form-item__label::before { content: ""; display: none; } } } .center { display: flex; align-items: flex-end; } } </style>