gaoluyang
2 天以前 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
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
<script lang="ts" setup>
import type { TaskFormType } from './data';
 
import { ref } from 'vue';
 
import { useVbenModal } from '#/packages/effects/common-ui/src';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import {
  createPdTask,
  dispatchPdTask,
  getPdTask,
  updatePdTask,
} from '#/api/mes/pd/task';
import { $t } from '#/locales';
 
import { useTaskDispatchSchema, useTaskFormSchema } from './data';
 
const emit = defineEmits(['success']);
 
const mode = ref<TaskFormType>('create');
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: { class: 'w-full' },
    labelWidth: 100,
  },
  layout: 'horizontal',
  schema: [],
  showDefaultActions: false,
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    try {
      if (mode.value === 'dispatch') {
        const data = await formApi.getValues();
        await dispatchPdTask({
          id: data.id,
          assigneeId: 0,
          planFinishDate: data.planFinishDate,
          remark: data.remark,
        });
      } else if (mode.value === 'create') {
        await createPdTask(await formApi.getValues());
      } else if (mode.value === 'update') {
        await updatePdTask(await formApi.getValues());
      }
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
      await modalApi.close();
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      return;
    }
    const data = modalApi.getData<{
      formType: TaskFormType;
      id?: number;
      projectId?: number;
    }>();
    mode.value = data.formType;
    const readonly = data.formType === 'detail';
    formApi.setState({
      schema:
        data.formType === 'dispatch'
          ? useTaskDispatchSchema()
          : useTaskFormSchema(data.formType),
    });
    formApi.setDisabled(readonly);
    modalApi.setState({
      showConfirmButton: data.formType !== 'detail',
      title:
        data.formType === 'dispatch'
          ? '任务分发'
          : data.formType === 'detail'
            ? '查看任务'
            : data.formType === 'update'
              ? '编辑任务'
              : '新增任务',
    });
    if (data.formType === 'create' && data.projectId) {
      await formApi.setValues({ projectId: data.projectId });
      return;
    }
    if (!data.id) {
      return;
    }
    modalApi.lock();
    try {
      const detail = await getPdTask(data.id);
      await formApi.setValues(detail);
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal class="w-[560px]">
    <Form class="mx-4" />
  </Modal>
</template>