<!-- 供方名录 人员信息 -->
<script name="SupplierInfoStaff" lang="ts" setup>
import { ElMessage } from 'element-plus'
import { phoneReg } from './supplier-interface'
import type { ISupplierInfoStaff } from './supplier-interface'
import { useCheckList } from '@/commonMethods/useCheckList'
import { useSetAllRowReadable } from '@/commonMethods/useSetAllRowReadable'
import { addSupplierStaff, deleteSupplierStaff, getSupplierStaffList } from '@/api/resource/supplier'
const props = defineProps({
operation: { type: String, default: '' },
supplierId: { type: String, default: '' },
})
const listQuery = {
id: '',
limit: 1000,
offset: 1,
}
const staffColumns = [
{ text: '姓名', value: 'name', align: 'center', required: true },
{ text: '工作部门', value: 'department', align: 'center', required: true },
{ text: '职务', value: 'job', align: 'center' },
{ text: '联系方式', value: 'phone', align: 'center', required: true, reg: phoneReg },
] // 表头
const staffList = ref<ISupplierInfoStaff[]>([]) // 表格数据
// 表格被选中的行
const staffSelected = ref<ISupplierInfoStaff[]>([])
const staffRecord: ISupplierInfoStaff = {
id: '',
supplierId: '',
name: '',
department: '',
job: '',
phone: '',
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
}
// 根据供方id查询相关人员
const getStaffListBySupplierId = () => {
getSupplierStaffList(listQuery).then((res: any) => {
if (res.code === 200) {
staffList.value = res.data.rows
}
})
}
// 增加供方人员
const addSupplierStaffRecords = async () => {
if (useCheckList(staffList.value, staffColumns, '供方人员') === false) {
return
}
const newRecords = ref<Array<ISupplierInfoStaff>>([])
newRecords.value = staffList.value.filter(item => item.id === '')
// 逐个调用添加接口
if (newRecords.value.length > 0) {
let recAdded = 0
await Promise.all(newRecords.value.map(async (record) => {
await addSupplierStaff(record).then((res) => {
if (res.code !== 200) {
ElMessage.error(`供方人员 ${record.name} 添加失败:${res.message}`)
}
else {
recAdded++
}
})
}))
if (recAdded === newRecords.value.length) {
ElMessage.success('添加供方人员成功')
}
getStaffListBySupplierId()
}
}
// 删除供方人员
const delSupplierStaffRecords = async () => {
if (staffSelected.value.length === 0) {
ElMessage.warning('请至少选择一行')
return
}
// 前端界面删除
staffList.value = staffList.value.filter(item => staffSelected.value.includes(item) === false)
// 逐个调用后端接口进行删除
const staffIdsSelected = ref<string[]>([])
staffIdsSelected.value = staffSelected.value.map((item: { id: string }) => item.id)
staffIdsSelected.value = staffIdsSelected.value.filter(item => item !== '')
if (staffIdsSelected.value.length > 0) {
let recDeleted = 0
await Promise.all(staffIdsSelected.value.map(async (staffId) => {
await deleteSupplierStaff({ id: staffId }).then((res) => {
if (res.code !== 200) {
ElMessage.error(`删除供方人员失败:${res.message}`)
}
else {
recDeleted++
}
})
}))
if (recDeleted === staffIdsSelected.value.length) {
ElMessage.success('删除供方人员成功')
}
getStaffListBySupplierId()
}
}
watch(() => props.supplierId, (newVal: string) => {
listQuery.id = newVal
staffRecord.supplierId = newVal
getStaffListBySupplierId()
})
defineExpose({
addSupplierStaffRecords,
})
</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="delSupplierStaffRecords">
删除行
</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>