gaoluyang
2026-06-29 27cd042df9aca0383a49f3514bc21958dd890912
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
<script lang="ts" setup>
import type { FormType } from '../data';
 
import type { MesDvCheckPlanApi } from '#/api/mes/dv/checkplan';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '../../../../../packages/effects/common-ui/src';
import { MesDvCheckPlanStatusEnum } from '../../../../../packages/constants/src';
 
import { message, Tabs } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import {
  createCheckPlan,
  getCheckPlan,
  updateCheckPlan,
} from '#/api/mes/dv/checkplan';
import { $t } from '#/locales';
 
import { useFormSchema } from '../data';
import MachineryList from './machinery-list.vue';
import SubjectList from './subject-list.vue';
 
const emit = defineEmits(['success']);
const formType = ref<FormType>('create');
const subTabsName = ref('machinery');
const formData = ref<MesDvCheckPlanApi.CheckPlan>();
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: 110,
  },
  wrapperClass: 'grid-cols-3',
  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 MesDvCheckPlanApi.CheckPlan;
    try {
      if (formType.value === 'create') {
        const id = await createCheckPlan(data);
        formData.value = {
          ...data,
          id: id as number,
          status: MesDvCheckPlanStatusEnum.PREPARE,
        };
        await formApi.setFieldValue('id', id);
        formType.value = 'update';
      } else {
        await updateCheckPlan(data);
        formData.value = { ...formData.value, ...data };
      }
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    subTabsName.value = 'machinery';
    // 加载数据
    const data = modalApi.getData<{ formType: FormType; id?: number }>();
    formType.value = data.formType;
    formApi.setState({ schema: useFormSchema(data.formType, formApi) });
    formApi.setDisabled(formType.value === 'detail');
    modalApi.setState({ showConfirmButton: formType.value !== 'detail' });
    if (!data?.id) {
      return;
    }
    modalApi.lock();
    try {
      formData.value = await getCheckPlan(data.id);
      // 设置到 values
      await formApi.setValues(formData.value);
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
<template>
  <Modal :title="getTitle" class="w-4/5">
    <Form class="mx-4" />
    <Tabs
      v-if="formType !== 'create' && formData?.id"
      v-model:active-key="subTabsName"
      class="mx-4 mt-4"
    >
      <Tabs.TabPane key="machinery" tab="设备">
        <MachineryList :form-type="formType" :plan-id="formData.id" />
      </Tabs.TabPane>
      <Tabs.TabPane key="subject" tab="项目">
        <SubjectList
          :form-type="formType"
          :plan-id="formData.id"
          :plan-type="formData.type"
        />
      </Tabs.TabPane>
    </Tabs>
  </Modal>
</template>