<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, DatePicker, Input } from 'ant-design-vue';
|
|
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
|
import { getWorkHistoryListByEmployeeId } from '#/api/hrm/employee/work-history';
|
import { $t } from '#/locales';
|
|
const props = defineProps<{
|
employeeId?: number;
|
formType: FormType;
|
}>();
|
|
const isReadOnly = computed(() => props.formType === 'detail');
|
|
const [Grid, gridApi] = useVbenVxeGrid({
|
gridOptions: {
|
columns: [
|
{ field: 'companyName', title: '公司名称', minWidth: 150, slots: { default: 'companyName' } },
|
{ field: 'position', title: '职位', minWidth: 120, slots: { default: 'position' } },
|
{
|
field: 'startDate',
|
title: '开始时间',
|
minWidth: 120,
|
slots: { default: 'startDate' },
|
},
|
{
|
field: 'endDate',
|
title: '结束时间',
|
minWidth: 120,
|
slots: { default: 'endDate' },
|
},
|
{ field: 'workContent', title: '工作内容', minWidth: 200, slots: { default: 'workContent' } },
|
{ field: 'leaveReason', title: '离职原因', minWidth: 150, slots: { default: 'leaveReason' } },
|
{
|
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.WorkHistory>,
|
});
|
|
/** 添加工作经历 */
|
async function handleAdd() {
|
await gridApi.grid.insertAt({} as HrmEmployeeApi.WorkHistory, -1);
|
}
|
|
/** 删除工作经历 */
|
async function handleDelete(row: HrmEmployeeApi.WorkHistory) {
|
await gridApi.grid.remove(row);
|
}
|
|
/** 提供获取表格数据的方法供父组件调用 */
|
defineExpose({
|
getData: (): HrmEmployeeApi.WorkHistory[] => {
|
const data = gridApi.grid.getData() as HrmEmployeeApi.WorkHistory[];
|
const removeRecords = gridApi.grid.getRemoveRecords() as HrmEmployeeApi.WorkHistory[];
|
const insertRecords = gridApi.grid.getInsertRecords() as HrmEmployeeApi.WorkHistory[];
|
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 getWorkHistoryListByEmployeeId(props.employeeId!));
|
},
|
{ immediate: true },
|
);
|
</script>
|
|
<template>
|
<Grid class="w-full">
|
<template #companyName="{ row }">
|
<Input v-model:value="row.companyName" :disabled="isReadOnly" placeholder="请输入公司名称" />
|
</template>
|
<template #position="{ row }">
|
<Input v-model:value="row.position" :disabled="isReadOnly" placeholder="请输入职位" />
|
</template>
|
<template #startDate="{ row }">
|
<DatePicker
|
v-model:value="row.startDate"
|
:disabled="isReadOnly"
|
placeholder="请选择开始时间"
|
valueFormat="YYYY-MM-DD"
|
style="width: 100%"
|
/>
|
</template>
|
<template #endDate="{ row }">
|
<DatePicker
|
v-model:value="row.endDate"
|
:disabled="isReadOnly"
|
placeholder="请选择结束时间"
|
valueFormat="YYYY-MM-DD"
|
style="width: 100%"
|
/>
|
</template>
|
<template #workContent="{ row }">
|
<Input v-model:value="row.workContent" :disabled="isReadOnly" placeholder="请输入工作内容" />
|
</template>
|
<template #leaveReason="{ row }">
|
<Input v-model:value="row.leaveReason" :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>
|