gaoluyang
8 天以前 696e85cf6422c703fbba63df097320bd813d9926
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 { MdmItemApi } from '#/api/mdm/item';
import type { MdmItemBatchConfigApi } from '#/api/mes/md/item-batch-config';
import type { VbenFormApi } from '#/adapter/form';
 
import { computed, ref, watch } from 'vue';
 
  import { useVbenModal } from "#/packages/effects/common-ui/src";
 
  import { message } from "ant-design-vue";
 
import { useVbenForm } from '#/adapter/form';
import { createItem, getItem, updateItem } from '#/api/mdm/item';
import { saveItemBatchConfig } from '#/api/mes/md/item-batch-config';
import { $t } from '#/locales';
 
import { useFormSchema } from '../data';
import ItemBatchConfig from '../components/batch-config.vue';
 
  defineOptions({ name: "ItemForm" });
 
const emit = defineEmits(['success']);
const formData = ref<MdmItemApi.Item>();
const batchConfigRef = ref<InstanceType<typeof ItemBatchConfig>>();
const batchConfig = ref<Partial<MdmItemBatchConfigApi.ItemBatchConfig>>({});
 
// 当前表单中的批次管理开关值(用于控制批次配置显示)
const currentIsBatchManaged = ref<boolean>(false);
 
const getTitle = computed(() => {
  return formData.value?.id
    ? $t('ui.actionTitle.edit', ['物料'])
    : $t('ui.actionTitle.create', ['物料']);
});
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    labelWidth: 110,
  },
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
  // 监听表单值变化
  handleValuesChange: (values: any) => {
    currentIsBatchManaged.value = values?.isBatchManaged === true;
  },
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    const data = (await formApi.getValues()) as MdmItemApi.Item;
    try {
      // 保存物料基本信息
      const itemId = await (formData.value?.id ? updateItem(data) : createItem(data));
      const savedItemId = typeof itemId === 'number' ? itemId : data.id;
 
      // 如果启用批次管理,保存批次配置
      if (data.isBatchManaged && savedItemId) {
        await saveItemBatchConfig({
          ...batchConfig.value,
          itemId: savedItemId,
        });
      }
 
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      batchConfig.value = {};
      currentIsBatchManaged.value = false;
      return;
    }
    const data = modalApi.getData<MdmItemApi.Item>();
    if (data?.id) {
      // 编辑模式
      modalApi.lock();
      try {
        formData.value = await getItem(data.id);
        await formApi.setValues(formData.value);
        currentIsBatchManaged.value = formData.value?.isBatchManaged === true;
        // 加载批次配置
        if (batchConfigRef.value && currentIsBatchManaged.value) {
          await batchConfigRef.value.loadConfig();
        }
      } finally {
        modalApi.unlock();
      }
    } else {
      // 新增模式
      formData.value = undefined;
      await formApi.resetForm();
      // 获取表单默认值(isBatchManaged 默认 true)
      const defaultValues = await formApi.getValues();
      currentIsBatchManaged.value = defaultValues?.isBatchManaged === true;
    }
  },
});
 
/** 处理批次配置变化 */
function handleBatchConfigChange(config: Partial<MdmItemBatchConfigApi.ItemBatchConfig>) {
  batchConfig.value = config;
}
</script>
 
<template>
  <Modal class="w-[1000px]" :title="getTitle">
    <Form class="mx-4" />
    <ItemBatchConfig
      ref="batchConfigRef"
      :item-id="formData?.id"
      :is-batch-managed="currentIsBatchManaged"
      @change="handleBatchConfigChange"
    />
  </Modal>
</template>