<script lang="ts" setup>
|
import type { FormType } from '../data';
|
import type { HrmEmployeeApi } from '#/api/hrm/employee';
|
|
import { computed, ref } from 'vue';
|
|
import { useVbenModal } from '#/packages/effects/common-ui/src';
|
|
import { message, Tabs, Input } from 'ant-design-vue';
|
|
import { useVbenForm } from '#/adapter/form';
|
import { createEmployee, getEmployee, updateEmployee } from '#/api/hrm/employee';
|
import { $t } from '#/locales';
|
|
import { useFormSchema } from '../data';
|
import EducationList from './education-list.vue';
|
import WorkHistoryList from './work-history-list.vue';
|
import EmergencyContactList from './emergency-contact-list.vue';
|
import SocialSecurityList from './social-security-list.vue';
|
import EmployeeDetail from './detail.vue';
|
|
const emit = defineEmits(['success']);
|
const formType = ref<FormType>('create');
|
const employeeId = ref<number>();
|
const employeeData = ref<HrmEmployeeApi.Employee>();
|
const subTabsName = ref('education');
|
const remark = ref<string>();
|
|
// 子表组件 ref
|
const educationListRef = ref<InstanceType<typeof EducationList>>();
|
const workHistoryListRef = ref<InstanceType<typeof WorkHistoryList>>();
|
const emergencyContactListRef = ref<InstanceType<typeof EmergencyContactList>>();
|
const socialSecurityListRef = ref<InstanceType<typeof SocialSecurityList>>();
|
|
const isDetail = computed(() => formType.value === 'detail');
|
const getTitle = computed(() => {
|
if (formType.value === 'detail') {
|
return '查看员工';
|
}
|
return formType.value === 'update' ? '修改员工' : '新增员工';
|
});
|
|
const [Form, formApi] = useVbenForm({
|
commonConfig: {
|
componentProps: {
|
class: 'w-full',
|
},
|
formItemClass: 'col-span-1',
|
labelWidth: 150,
|
},
|
wrapperClass: 'grid-cols-2',
|
layout: 'horizontal',
|
schema: [],
|
showDefaultActions: false,
|
});
|
|
const [Modal, modalApi] = useVbenModal({
|
async onConfirm() {
|
if (isDetail.value) {
|
await modalApi.close();
|
return;
|
}
|
const { valid } = await formApi.validate();
|
if (!valid) {
|
return;
|
}
|
modalApi.lock();
|
// 提交表单
|
const data = (await formApi.getValues()) as HrmEmployeeApi.Employee;
|
data.remark = remark.value;
|
// 获取子表数据
|
data.educations = educationListRef.value?.getData();
|
data.workHistories = workHistoryListRef.value?.getData();
|
data.emergencyContacts = emergencyContactListRef.value?.getData();
|
try {
|
if (data.id) {
|
await updateEmployee(data);
|
} else {
|
await createEmployee(data);
|
}
|
// 关闭并提示
|
await modalApi.close();
|
emit('success');
|
message.success($t('ui.actionMessage.operationSuccess'));
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
async onOpenChange(isOpen: boolean) {
|
if (!isOpen) {
|
employeeId.value = undefined;
|
employeeData.value = undefined;
|
remark.value = undefined;
|
return;
|
}
|
// 加载数据
|
const data = modalApi.getData<{ formType: FormType; id?: number }>();
|
formType.value = data.formType;
|
if (data.formType !== 'detail') {
|
formApi.setState({ schema: useFormSchema(data.formType) });
|
formApi.setDisabled(false);
|
modalApi.setState({ showConfirmButton: true });
|
} else {
|
modalApi.setState({ showConfirmButton: false });
|
}
|
if (!data?.id) {
|
return;
|
}
|
modalApi.lock();
|
try {
|
const formData = await getEmployee(data.id);
|
employeeId.value = formData.id;
|
employeeData.value = formData;
|
remark.value = formData.remark;
|
if (data.formType !== 'detail') {
|
await formApi.setValues(formData);
|
}
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
});
|
</script>
|
|
<template>
|
<Modal :title="getTitle" class="w-[1100px]">
|
<!-- 详情模式:使用专门的详情展示组件 -->
|
<EmployeeDetail v-if="isDetail && employeeData" :employee="employeeData" />
|
<!-- 新增/编辑模式:使用表单 -->
|
<template v-else>
|
<Form class="mx-4" />
|
<Tabs
|
v-if="!isDetail || employeeId"
|
v-model:active-key="subTabsName"
|
class="mx-4 mt-4"
|
>
|
<Tabs.TabPane key="education" tab="教育经历">
|
<EducationList
|
ref="educationListRef"
|
:form-type="formType"
|
:employee-id="employeeId"
|
/>
|
</Tabs.TabPane>
|
<Tabs.TabPane key="workHistory" tab="工作经历">
|
<WorkHistoryList
|
ref="workHistoryListRef"
|
:form-type="formType"
|
:employee-id="employeeId"
|
/>
|
</Tabs.TabPane>
|
<Tabs.TabPane key="emergencyContact" tab="紧急联系人">
|
<EmergencyContactList
|
ref="emergencyContactListRef"
|
:form-type="formType"
|
:employee-id="employeeId"
|
/>
|
</Tabs.TabPane>
|
<Tabs.TabPane key="socialSecurity" tab="社保公积金档案" v-if="employeeId">
|
<SocialSecurityList
|
ref="socialSecurityListRef"
|
:form-type="formType"
|
:employee-id="employeeId"
|
:user-id="employeeData?.userId"
|
/>
|
</Tabs.TabPane>
|
</Tabs>
|
<div class="mx-4 mt-4">
|
<div class="mb-2 font-medium">备注</div>
|
<Input.TextArea
|
v-model:value="remark"
|
placeholder="请输入备注"
|
:rows="3"
|
/>
|
</div>
|
</template>
|
</Modal>
|
</template>
|