From 27c8277d717b34b55f3ea967027b53b067f77df3 Mon Sep 17 00:00:00 2001
From: xiaoyi <908147524@qq.com>
Date: 星期四, 20 八月 2026 16:58:49 +0800
Subject: [PATCH] feat 功能变更

---
 src/views/erp/purchase/plan/modules/form.vue |  236 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 236 insertions(+), 0 deletions(-)

diff --git a/src/views/erp/purchase/plan/modules/form.vue b/src/views/erp/purchase/plan/modules/form.vue
new file mode 100644
index 0000000..8ad6dc6
--- /dev/null
+++ b/src/views/erp/purchase/plan/modules/form.vue
@@ -0,0 +1,236 @@
+<script lang="ts" setup>
+import type { ErpPurchasePlanApi } from '#/api/erp/purchase/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 {
+  createPurchasePlan,
+  getPurchasePlan,
+  updatePurchasePlan,
+} from '#/api/erp/purchase/plan';
+import { MdmItemSelect } from '#/views/basicData/mdm/components';
+import { $t } from '#/locales';
+
+import type { FormType } from '../data';
+import { useFormSchema } from '../data';
+
+const emit = defineEmits(['success']);
+const formData = ref<ErpPurchasePlanApi.PurchasePlan>();
+const formType = ref<FormType>('create');
+const items = ref<ErpPurchasePlanApi.PurchasePlanItem[]>([]);
+
+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({
+    productId: undefined as unknown as number,
+    productName: '',
+    productUnitId: undefined as unknown as number,
+    productUnitName: '',
+    count: 1,
+    demandTime: undefined,
+    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.productId = item?.id;
+  row.productName = item?.name;
+  row.productUnitId = item?.unitMeasureId;
+  row.productUnitName = 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.productId || !item.count)) {
+      message.error('璇峰畬鍠勮鍒掓槑缁嗙殑浜у搧涓庢暟閲�');
+      return;
+    }
+    modalApi.lock();
+    const data =
+      (await formApi.getValues()) as ErpPurchasePlanApi.PurchasePlan;
+    data.items = items.value.map((item) => ({
+      productId: item.productId,
+      productUnitId: item.productUnitId,
+      count: item.count,
+      demandTime: item.demandTime,
+      remark: item.remark,
+    }));
+    try {
+      await (formType.value === 'create'
+        ? createPurchasePlan(data)
+        : updatePurchasePlan(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 getPurchasePlan(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 w-40">闇�姹傛棩鏈�</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">
+                  <MdmItemSelect
+                    v-if="formType !== 'detail'"
+                    :model-value="item.productId"
+                    placeholder="璇烽�夋嫨浜у搧"
+                    @change="handleItemSelect(index, $event)"
+                  />
+                  <span v-else>{{ item.productName }}</span>
+                </td>
+                <td class="border border-gray-200 px-2 py-1 text-center">
+                  {{ item.productUnitName || '-' }}
+                </td>
+                <td class="border border-gray-200 px-2 py-1">
+                  <InputNumber
+                    v-if="formType !== 'detail'"
+                    v-model:value="item.count"
+                    class="!w-full"
+                    :min="0.01"
+                    :precision="2"
+                  />
+                  <span v-else>{{ item.count }}</span>
+                </td>
+                <td class="border border-gray-200 px-2 py-1">
+                  <DatePicker
+                    v-if="formType !== 'detail'"
+                    v-model:value="item.demandTime"
+                    class="!w-full"
+                    value-format="YYYY-MM-DD"
+                  />
+                  <span v-else>{{ item.demandTime || '-' }}</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' ? 6 : 7"
+                  class="border border-gray-200 px-2 py-4 text-center text-gray-400"
+                >
+                  鏆傛棤鏄庣粏锛岃娣诲姞
+                </td>
+              </tr>
+            </tbody>
+          </table>
+        </div>
+      </template>
+    </Form>
+  </Modal>
+</template>

--
Gitblit v1.9.3