4 天以前 91c82965b2d987452ca276e3a03b48c8dcda2ae9
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
<script lang="ts" setup>
  import type { FormType } from "../data";
  import type { MesProLineApi } from "#/api/mes/pro/line";
 
  import { computed, ref } from "vue";
 
  import { useVbenModal } from "@vben/common-ui";
 
  import { Button, message } from "ant-design-vue";
 
  import { useVbenForm } from "#/adapter/form";
  import { createLine, getLine, updateLine } from "#/api/mes/pro/line";
  import { $t } from "#/locales";
 
  import { useFormSchema } from "../data";
 
  defineOptions({ name: "MesProLineForm" });
 
  const emit = defineEmits(["success"]);
 
  const formType = ref<FormType>("create");
  const formData = ref<MesProLineApi.Line>();
 
  const isEditable = computed(() =>
    ["create", "update"].includes(formType.value)
  );
 
  const getTitle = computed(() => {
    switch (formType.value) {
      case "detail": {
        return $t("ui.actionTitle.view", ["产线"]);
      }
      case "update": {
        return $t("ui.actionTitle.edit", ["产线"]);
      }
      default: {
        return $t("ui.actionTitle.create", ["产线"]);
      }
    }
  });
 
  const [Form, formApi] = useVbenForm({
    commonConfig: {
      componentProps: {
        class: "w-full",
      },
      formItemClass: "col-span-1",
      labelWidth: 100,
    },
    layout: "horizontal",
    schema: [],
    showDefaultActions: false,
    wrapperClass: "grid-cols-2",
  });
 
  const [Modal, modalApi] = useVbenModal({
    async onConfirm() {
      if (!isEditable.value) {
        await modalApi.close();
        return;
      }
      const { valid } = await formApi.validate();
      if (!valid) return;
      modalApi.lock();
      try {
        const data = (await formApi.getValues()) as MesProLineApi.Line;
        if (formData.value?.id) {
          await updateLine({ ...data, id: formData.value.id });
        } else {
          await createLine(data);
        }
        message.success($t("ui.actionMessage.operationSuccess"));
        emit("success");
        await modalApi.close();
      } 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(formType.value) });
      if (data?.id) {
        modalApi.lock();
        try {
          formData.value = await getLine(data.id);
          await formApi.setValues(formData.value);
        } finally {
          modalApi.unlock();
        }
      }
    },
  });
</script>
 
<template>
  <Modal :title="getTitle" class="w-2/5">
    <Form class="mx-4" />
    <template #append-footer>
      <div class="flex gap-2">
        <Button @click="modalApi.close()">
          {{ isEditable ? '取消' : '关闭' }}
        </Button>
      </div>
    </template>
  </Modal>
</template>