gaoluyang
10 天以前 f3cac4aa2db56d6017c1d1dc035bcfeaec76c3e2
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
<script lang="ts" setup>
import type { ApplicantOption, FormType } from '../data';
import type { HrmRegularizeApi } from '#/api/hrm/regularize';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '#/packages/effects/common-ui/src';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import {
  createRegularize,
  getRegularize,
  updateRegularize,
} from '#/api/hrm/regularize';
import { $t } from '#/locales';
 
import { useFormSchema } from '../data';
 
const emit = defineEmits(['success']);
const formType = ref<FormType>('create');
 
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: 100,
  },
  wrapperClass: 'grid-cols-2',
  layout: 'horizontal',
  schema: [],
  showDefaultActions: false,
});
 
/** 选择申请人后,自动带入其部门、岗位与当前用工性质 */
async function handleApplicantChange(option?: ApplicantOption) {
  if (!option) {
    return;
  }
  const patch: Record<string, number> = {};
  if (option.deptId !== undefined && option.deptId !== null) {
    patch.deptId = option.deptId;
  }
  if (option.postId !== undefined && option.postId !== null) {
    patch.postId = option.postId;
  }
  if (
    option.employmentType !== undefined &&
    option.employmentType !== null
  ) {
    patch.employmentType = option.employmentType;
  }
  await formApi.setValues(patch);
}
 
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 HrmRegularizeApi.RegularizeApplication;
    try {
      if (data.id) {
        await updateRegularize(data);
      } else {
        await createRegularize(data);
      }
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      return;
    }
    const data = modalApi.getData<{ formType: FormType; id?: number }>();
    formType.value = data.formType;
    formApi.setState({
      schema: useFormSchema(data.formType, handleApplicantChange),
    });
    formApi.setDisabled(data.formType === 'detail');
    modalApi.setState({ showConfirmButton: data.formType !== 'detail' });
    if (!data?.id) {
      return;
    }
    modalApi.lock();
    try {
      const detail = await getRegularize(data.id);
      await formApi.setValues(detail);
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal :title="getTitle" class="w-[600px]">
    <Form class="mx-4" />
  </Modal>
</template>