gaoluyang
昨天 b64a0deae5b5d33f9e20671a68936b27f0b9b00b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<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>