xiaoyi
3 天以前 07bb7e462ea911a698da03cf50e92103e6434e88
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
<script lang="ts" setup>
  import type { FormType } from '../data';
 
  import type { MesPdCtcReelApi } from '#/api/mes/pd/ctc-reel';
 
  import { computed, ref } from 'vue';
 
  import { useVbenModal } from '@vben/common-ui';
  import { $t } from '@vben/locales';
 
  import { Button, message } from 'ant-design-vue';
 
  import { useVbenForm } from '#/adapter/form';
  import {
    createCtcReel,
    getCtcReel,
    updateCtcReel,
  } from '#/api/mes/pd/ctc-reel';
  import { CalcModal } from '#/views/mes/pd/ctc/modules';
 
  import { useFormSchema } from '../data';
 
  const emit = defineEmits(['success']);
  const formType = ref<FormType>('create'); // 表单模式
  const formData = ref<MesPdCtcReelApi.CtcReel>();
  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: 130,
    },
    wrapperClass: 'grid-cols-3',
    layout: 'horizontal',
    schema: [],
    showDefaultActions: false,
  });
 
  const [CalcToolModal, calcModalApi] = useVbenModal({
    connectedComponent: CalcModal,
    destroyOnClose: true,
  });
 
  /** 打开容量计算工具 */
  function handleOpenCalc() {
    calcModalApi.open();
  }
 
  /** 回填最大容量(km) */
  function handleFillReel(value: { capacityKm: number }) {
    formApi.setFieldValue('maxCapacity', value.capacityKm);
  }
 
  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 MesPdCtcReelApi.CtcReel;
      try {
        await (data.id ? updateCtcReel(data) : createCtcReel(data));
        // 关闭并提示
        await modalApi.close();
        emit('success');
        message.success($t('ui.actionMessage.operationSuccess'));
      } finally {
        modalApi.unlock();
      }
    },
    async onOpenChange(isOpen: boolean) {
      if (!isOpen) {
        formData.value = undefined;
        return;
      }
      // 加载数据
      const data = modalApi.getData<{ formType: FormType; id?: number }>();
      formType.value = data.formType;
      formApi.setState({ schema: useFormSchema() });
      formApi.setDisabled(formType.value === 'detail');
      modalApi.setState({ showConfirmButton: formType.value !== 'detail' });
      if (!data?.id) {
        return;
      }
      modalApi.lock();
      try {
        formData.value = await getCtcReel(data.id);
        // 设置到 values
        await formApi.setValues(formData.value);
      } finally {
        modalApi.unlock();
      }
    },
  });
</script>
 
<template>
  <Modal :title="getTitle" class="w-4/5">
    <Form class="mx-4" />
    <template #prepend-footer>
      <Button
        v-if="!isDetail"
        type="default"
        @click="handleOpenCalc"
      >
        容量计算
      </Button>
    </template>
    <CalcToolModal @reel="handleFillReel" />
  </Modal>
</template>