云
16 小时以前 c2e8526f8f857be5e3a8c6f2b39c5efd424de33e
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
<script setup lang="ts">
import type { SpareSave } from '#/api/mes/dv/spare';
import type { FormInstance } from 'ant-design-vue';
import { reactive, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { Alert, Form, Input, InputNumber, Select } from 'ant-design-vue';
import { createSpare, getSpare, updateSpare } from '#/api/mes/dv/spare';
const emit = defineEmits(['success']);
const formRef = ref<FormInstance>();
const saving = ref(false);
const model = reactive<SpareSave>({ code: '', name: '', unit: '件', initialQuantity: 0, safetyQuantity: 0, status: 0 });
const required = [{ required: true, message: '请填写此项' }];
const [Modal, modalApi] = useVbenModal({
  onBeforeClose: () => !saving.value,
  async onOpenChange(open) {
    if (!open) return;
    Object.assign(model, { id: undefined, code: '', name: '', specification: '', unit: '件', location: '', initialQuantity: 0, safetyQuantity: 0, status: 0, remark: '' });
    formRef.value?.clearValidate();
    const { id } = modalApi.getData<{ id?: number }>();
    if (id) { modalApi.lock(); try { Object.assign(model, await getSpare(id)); } finally { modalApi.unlock(); } }
  },
  async onConfirm() {
    if (saving.value) return;
    await formRef.value?.validate();
    saving.value = true; modalApi.lock();
    try { await (model.id ? updateSpare(model) : createSpare(model)); emit('success'); saving.value = false; await modalApi.close(); }
    finally { saving.value = false; modalApi.unlock(); }
  },
});
</script>
<template>
  <Modal :title="model.id ? '编辑备件' : '新增备件'" class="w-full sm:w-[640px]">
    <Alert class="mb-4" type="info" :message="model.id ? '库存通过入库、领用和退回变更。已有流水后不可修改计量单位。' : '初始库存登记后会保留流水,后续补货请使用入库。'" />
    <Form ref="formRef" :model="model" layout="vertical" class="grid grid-cols-1 gap-x-4 sm:grid-cols-2">
      <Form.Item label="备件编码" name="code" :rules="required"><Input v-model:value="model.code" :maxlength="64" /></Form.Item>
      <Form.Item label="备件名称" name="name" :rules="required"><Input v-model:value="model.name" :maxlength="128" /></Form.Item>
      <Form.Item label="规格型号" name="specification"><Input v-model:value="model.specification" :maxlength="255" /></Form.Item>
      <Form.Item label="计量单位" name="unit" :rules="required"><Input v-model:value="model.unit" :maxlength="32" /></Form.Item>
      <Form.Item label="存放位置" name="location"><Input v-model:value="model.location" :maxlength="128" /></Form.Item>
      <Form.Item label="安全库存" name="safetyQuantity" :rules="required"><InputNumber v-model:value="model.safetyQuantity" :min="0" :precision="3" class="w-full" /></Form.Item>
      <Form.Item v-if="!model.id" label="初始库存" name="initialQuantity" :rules="required"><InputNumber v-model:value="model.initialQuantity" :min="0" :precision="3" class="w-full" /></Form.Item>
      <Form.Item label="状态" name="status"><Select v-model:value="model.status" :options="[{label:'启用',value:0},{label:'停用',value:1}]" /></Form.Item>
      <Form.Item label="备注" name="remark" class="sm:col-span-2"><Input.TextArea v-model:value="model.remark" :maxlength="500" :rows="3" /></Form.Item>
    </Form>
  </Modal>
</template>