xiaoyi
4 天以前 5c243d9d05da668a32b1bc9a4f543ea081488d11
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<script lang="ts" setup>
import type { MesProMaintenancePlanApi } from '#/api/mes/pro/maintenance-plan';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
 
import { Button, DatePicker, Input, InputNumber, message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import {
  createPlan,
  getPlan,
  updatePlan,
} from '#/api/mes/pro/maintenance-plan';
import { MdItemSelect } from '#/views/mes/md/item/components';
import { $t } from '#/locales';
 
import type { FormType } from '../data';
import { useFormSchema } from '../data';
 
const emit = defineEmits(['success']);
const formData = ref<MesProMaintenancePlanApi.MaintenancePlan>();
const formType = ref<FormType>('create');
const items = ref<MesProMaintenancePlanApi.PlanItem[]>([]);
 
const getTitle = computed(() => {
  if (formType.value === 'create') {
    return $t('ui.actionTitle.create', ['电力作业计划']);
  } else if (formType.value === 'edit') {
    return $t('ui.actionTitle.edit', ['电力作业计划']);
  } else {
    return '电力作业计划详情';
  }
});
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    labelWidth: 120,
  },
  wrapperClass: 'grid-cols-3',
  layout: 'vertical',
  schema: useFormSchema(formType.value),
  showDefaultActions: false,
});
 
function handleAddItem() {
  items.value.push({
    itemId: undefined as unknown as number,
    itemName: '',
    itemCode: '',
    unitMeasureName: '',
    quantity: 1,
    remark: '',
  });
}
 
function handleRemoveItem(index: number) {
  items.value.splice(index, 1);
}
 
function handleItemSelect(index: number, item: any) {
  const row = items.value[index];
  if (!row) {
    return;
  }
  row.itemId = item?.id;
  row.itemName = item?.name;
  row.itemCode = item?.code;
  row.unitMeasureName = item?.unitMeasureName;
}
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    if (!items.value.length) {
      message.error('请至少添加一条备件/物资需求');
      return;
    }
    if (items.value.some((item) => !item.itemId || !item.quantity)) {
      message.error('请完善备件需求的产品与数量');
      return;
    }
    modalApi.lock();
    const data =
      (await formApi.getValues()) as MesProMaintenancePlanApi.MaintenancePlan;
    data.items = items.value.map((item) => ({
      itemId: item.itemId,
      quantity: item.quantity,
      remark: item.remark,
    }));
    try {
      await (formType.value === 'create'
        ? createPlan(data)
        : updatePlan(data));
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      items.value = [];
      return;
    }
    const data = modalApi.getData<{ formType: FormType; id?: number }>();
    formType.value = data.formType;
    formApi.setDisabled(formType.value === 'detail');
    formApi.updateSchema(useFormSchema(formType.value));
    if (!data || !data.id) {
      return;
    }
    modalApi.lock();
    try {
      formData.value = await getPlan(data.id);
      await formApi.setValues(formData.value);
      items.value = (formData.value.items ?? []).map((item) => ({ ...item }));
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal
    :title="getTitle"
    class="w-3/4"
    :show-confirm-button="formType !== 'detail'"
  >
    <Form class="mx-3">
      <template #items>
        <div class="mt-2">
          <div v-if="formType !== 'detail'" class="mb-2">
            <Button type="dashed" @click="handleAddItem">+ 添加备件需求</Button>
          </div>
          <table class="w-full border-collapse border border-gray-200 text-sm">
            <thead>
              <tr class="bg-gray-100">
                <th class="border border-gray-200 px-2 py-1 w-12">序号</th>
                <th class="border border-gray-200 px-2 py-1">物料/备件</th>
                <th class="border border-gray-200 px-2 py-1 w-20">单位</th>
                <th class="border border-gray-200 px-2 py-1 w-32">需求数量</th>
                <th class="border border-gray-200 px-2 py-1">备注</th>
                <th
                  v-if="formType !== 'detail'"
                  class="border border-gray-200 px-2 py-1 w-16"
                >
                  操作
                </th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item, index) in items" :key="index">
                <td class="border border-gray-200 px-2 py-1 text-center">
                  {{ index + 1 }}
                </td>
                <td class="border border-gray-200 px-2 py-1">
                  <MdItemSelect
                    v-if="formType !== 'detail'"
                    :model-value="item.itemId"
                    placeholder="请选择物料/备件"
                    @change="handleItemSelect(index, $event)"
                  />
                  <span v-else>{{ item.itemName }}</span>
                </td>
                <td class="border border-gray-200 px-2 py-1 text-center">
                  {{ item.unitMeasureName || '-' }}
                </td>
                <td class="border border-gray-200 px-2 py-1">
                  <InputNumber
                    v-if="formType !== 'detail'"
                    v-model:value="item.quantity"
                    class="!w-full"
                    :min="0.01"
                    :precision="2"
                  />
                  <span v-else>{{ item.quantity }}</span>
                </td>
                <td class="border border-gray-200 px-2 py-1">
                  <Input
                    v-if="formType !== 'detail'"
                    v-model:value="item.remark"
                    placeholder="备注"
                  />
                  <span v-else>{{ item.remark || '-' }}</span>
                </td>
                <td
                  v-if="formType !== 'detail'"
                  class="border border-gray-200 px-2 py-1 text-center"
                >
                  <Button type="link" danger @click="handleRemoveItem(index)">
                    删除
                  </Button>
                </td>
              </tr>
              <tr v-if="!items.length">
                <td
                  :colspan="formType === 'detail' ? 5 : 6"
                  class="border border-gray-200 px-2 py-4 text-center text-gray-400"
                >
                  暂无备件需求,请添加
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </template>
    </Form>
  </Modal>
</template>