<script lang="ts" setup>
|
import type { FormType } from '../data';
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
import type { HrmEmployeeApi } from '#/api/hrm/employee';
|
|
import { computed, nextTick, watch } from 'vue';
|
|
import { IconifyIcon } from '#/packages/icons/src';
|
|
import { Button, Input } from 'ant-design-vue';
|
|
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
|
import { getEmergencyContactListByEmployeeId } from '#/api/hrm/employee/emergency-contact';
|
import { $t } from '#/locales';
|
|
const props = defineProps<{
|
employeeId?: number;
|
formType: FormType;
|
}>();
|
|
const isReadOnly = computed(() => props.formType === 'detail');
|
|
const [Grid, gridApi] = useVbenVxeGrid({
|
gridOptions: {
|
columns: [
|
{ field: 'contactName', title: '联系人姓名', minWidth: 120, slots: { default: 'contactName' } },
|
{ field: 'relationship', title: '关系', minWidth: 100, slots: { default: 'relationship' } },
|
{ field: 'phone', title: '联系电话', minWidth: 130, slots: { default: 'phone' } },
|
{ field: 'address', title: '地址', minWidth: 200, slots: { default: 'address' } },
|
{
|
title: '操作',
|
width: 80,
|
fixed: 'right',
|
slots: { default: 'actions' },
|
visible: !isReadOnly.value,
|
},
|
],
|
border: true,
|
showOverflow: true,
|
autoResize: true,
|
keepSource: true,
|
rowConfig: {
|
keyField: 'id',
|
isHover: true,
|
},
|
pagerConfig: {
|
enabled: false,
|
},
|
toolbarConfig: {
|
enabled: false,
|
},
|
} as VxeTableGridOptions<HrmEmployeeApi.EmergencyContact>,
|
});
|
|
/** 添加紧急联系人 */
|
async function handleAdd() {
|
await gridApi.grid.insertAt({} as HrmEmployeeApi.EmergencyContact, -1);
|
}
|
|
/** 删除紧急联系人 */
|
async function handleDelete(row: HrmEmployeeApi.EmergencyContact) {
|
await gridApi.grid.remove(row);
|
}
|
|
/** 提供获取表格数据的方法供父组件调用 */
|
defineExpose({
|
getData: (): HrmEmployeeApi.EmergencyContact[] => {
|
const data = gridApi.grid.getData() as HrmEmployeeApi.EmergencyContact[];
|
const removeRecords = gridApi.grid.getRemoveRecords() as HrmEmployeeApi.EmergencyContact[];
|
const insertRecords = gridApi.grid.getInsertRecords() as HrmEmployeeApi.EmergencyContact[];
|
return [
|
...data.filter((row) => !removeRecords.some((removed) => removed.id === row.id)),
|
...insertRecords.map((row: any) => ({ ...row, id: undefined })),
|
];
|
},
|
});
|
|
/** 监听员工ID变化,加载子表数据 */
|
watch(
|
() => props.employeeId,
|
async (val) => {
|
if (!val) {
|
return;
|
}
|
await nextTick();
|
await gridApi.grid.loadData(await getEmergencyContactListByEmployeeId(props.employeeId!));
|
},
|
{ immediate: true },
|
);
|
</script>
|
|
<template>
|
<Grid class="w-full">
|
<template #contactName="{ row }">
|
<Input v-model:value="row.contactName" :disabled="isReadOnly" placeholder="请输入联系人姓名" />
|
</template>
|
<template #relationship="{ row }">
|
<Input v-model:value="row.relationship" :disabled="isReadOnly" placeholder="请输入关系" />
|
</template>
|
<template #phone="{ row }">
|
<Input v-model:value="row.phone" :disabled="isReadOnly" placeholder="请输入联系电话" />
|
</template>
|
<template #address="{ row }">
|
<Input v-model:value="row.address" :disabled="isReadOnly" placeholder="请输入地址" />
|
</template>
|
<template #actions="{ row }">
|
<TableAction
|
:actions="[
|
{
|
label: $t('common.delete'),
|
danger: true,
|
type: 'link',
|
icon: ACTION_ICON.DELETE,
|
onClick: handleDelete.bind(null, row),
|
},
|
]"
|
/>
|
</template>
|
</Grid>
|
<div v-if="!isReadOnly" class="-mt-4 flex justify-center">
|
<Button type="primary" ghost @click="handleAdd">
|
<IconifyIcon icon="lucide:plus" />
|
添加紧急联系人
|
</Button>
|
</div>
|
</template>
|