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
<script lang="ts" setup>
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import { createMapping, getMapping, updateMapping } from '#/api/erp/warehouseMapping';
import { $t } from '#/locales';
 
import { useFormSchema } from '../data';
 
defineOptions({ name: 'ErpWarehouseMappingForm' });
 
const emit = defineEmits<{ success: [] }>();
 
const formType = ref<'create' | 'update'>('create');
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
  },
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false,
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const values = await formApi.getValues();
    try {
      if (formType.value === 'create') {
        await createMapping(values);
        message.success('创建成功');
      } else {
        await updateMapping(values);
        message.success('更新成功');
      }
      emit('success');
      modalApi.close();
    } catch (error) {
      console.error('保存失败:', error);
    }
  },
  onOpenChange(isOpen) {
    if (isOpen) {
      const data = modalApi.getData() as {
        formType: 'create' | 'update';
        id?: number;
      };
      if (data) {
        formType.value = data.formType;
        if (data.formType === 'update' && data.id) {
          // 加载详情
          getMapping(data.id).then((detail) => {
            formApi.setValues(detail);
          });
        } else {
          formApi.resetForm();
        }
      }
    }
  },
});
 
const modalTitle = computed(() => {
  return formType.value === 'create' ? '新增仓库映射' : '编辑仓库映射';
});
</script>
 
<template>
  <Modal :title="modalTitle" class="w-[500px]">
    <Form />
  </Modal>
</template>