<!-- 委托方名录 人员信息 --> <script name="CustomerInfoStaff" lang="ts" setup> import { ElMessage, dayjs } from 'element-plus' import type { ICustomerInfoStaff } from './customer-info' import { useCheckList } from '@/commonMethods/useCheckList' import { useSetAllRowReadable } from '@/commonMethods/useSetAllRowReadable' import { addAuthorizeRecList, delAuthorizeRecList, getAuthorizeList } from '@/api/resource/register' const props = defineProps({ operation: { type: String, default: '' }, customerId: { type: String, default: '' }, }) const listQuery = { id: '', limit: 1000, offset: 1, } const mobileReg = (val: string) => { const reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/ return reg.test(val) } const staffColumns = [ { text: '姓名', value: 'staffName', align: 'center', required: true }, { text: '工作部门', value: 'deptName', align: 'center', required: true }, { text: '职务', value: 'staffJob', align: 'center' }, { text: '联系方式', value: 'mobile', align: 'center', required: true, reg: mobileReg }, ] // 表头 const staffList = ref<ICustomerInfoStaff[]>([]) // 表格数据 // 表格被选中的行 const staffSelected = ref<ICustomerInfoStaff[]>([]) const staffRecord: ICustomerInfoStaff = { id: '', customerId: '', staffName: '', deptName: '', staffJob: '', mobile: '', editable: true, } // 逻辑 const addEditableRow = (tableName: string) => { switch (tableName) { case 'staff': if (useCheckList(staffList.value, staffColumns, '委托方人员')) { useSetAllRowReadable(staffList.value) staffList.value.push({ ...staffRecord }) } break default: break } } const authMultiSelect = (e: any) => { staffSelected.value = e } const getAuthorizeListByStaffId = () => { getAuthorizeList(listQuery).then((res) => { if (res.code === 200) { staffList.value = res.data.rows } }) } // 增加委托方人员记录 const addAuthorizeRecords = () => { if (useCheckList(staffList.value, staffColumns, '委托方人员') === false) { return } const newRecords = ref<Array<ICustomerInfoStaff>>([]) newRecords.value = staffList.value.filter(item => item.id === '') if (newRecords.value.length > 0) { addAuthorizeRecList(newRecords.value).then((res) => { if (res.code === 200) { ElMessage.success('添加委托方人员记录成功') getAuthorizeListByStaffId() } else { ElMessage.error(`添加委托方人员记录失败:${res.message}`) } }) } } // 删除委托方人员记录 const delAuthorizeRecords = () => { if (staffSelected.value.length === 0) { ElMessage.warning('请至少选择一行') return } // 前端界面删除 staffList.value = staffList.value.filter(item => staffSelected.value.includes(item) === false) // 调用后端接口进行删除 const authIdsSelected = ref<string[]>([]) authIdsSelected.value = staffSelected.value.map((item: { id: string }) => item.id) authIdsSelected.value = authIdsSelected.value.filter(item => item !== '') if (authIdsSelected.value.length > 0) { delAuthorizeRecList({ ids: authIdsSelected.value }).then((res) => { if (res.code === 200) { ElMessage.success('删除委托方人员记录成功') } else { ElMessage.error(`删除委托方人员记录失败:${res.message}`) } getAuthorizeListByStaffId() }) } } watch(() => props.customerId, (newVal: string) => { listQuery.id = newVal staffRecord.customerId = newVal getAuthorizeListByStaffId() }) defineExpose({ addAuthorizeRecords, }) </script> <template> <app-container> <el-form ref="staffFormRef" label-position="right" label-width="110px" border stripe> <table-container title="委托方人员"> <template v-if="props.operation !== 'detail'" #btns-right> <el-button type="primary" @click="addEditableRow('staff')"> 增加行 </el-button> <el-button type="info" @click="delAuthorizeRecords"> 删除行 </el-button> </template> <!-- 表格区域 --> <el-table :data="staffList" border style="width: 100%;" @selection-change="authMultiSelect"> <el-table-column v-if="props.operation !== 'detail'" type="selection" align="center" width="38" /> <el-table-column align="center" label="序号" width="55" type="index" /> <el-table-column v-for="item in staffColumns" :key="item.value" :prop="item.value" :label="item.text" align="center" > <template #header> <span v-show="item.required && props.operation !== 'detail'" style="color: red;">*</span><span>{{ item.text }}</span> </template> <template #default="scope"> <span v-if="!scope.row.editable">{{ scope.row[item.value] }}</span> <el-input v-else v-model="scope.row[item.value]" :autofocus="true" :placeholder="`${item.text}`" class="input" /> </template> </el-table-column> </el-table> </table-container> </el-form> </app-container> </template>