<script lang="ts" setup>
|
import type { MdmItemApi } from '#/api/mdm/item';
|
import type { MdmItemBatchConfigApi } from '#/api/mes/md/item-batch-config';
|
import type { VbenFormApi } from '#/adapter/form';
|
|
import { computed, ref, watch } from 'vue';
|
|
import { useVbenModal } from '#/packages/effects/common-ui/src';
|
|
import { message } from 'ant-design-vue';
|
|
import { useVbenForm } from '#/adapter/form';
|
import { createItem, getItem, updateItem } from '#/api/mdm/item';
|
import { saveItemBatchConfig } from '#/api/mes/md/item-batch-config';
|
import { $t } from '#/locales';
|
|
import { useFormSchema } from '../data';
|
import ItemBatchConfig from '../components/batch-config.vue';
|
|
defineOptions({ name: 'ItemForm' });
|
|
const emit = defineEmits(['success']);
|
const formData = ref<MdmItemApi.Item>();
|
const batchConfigRef = ref<InstanceType<typeof ItemBatchConfig>>();
|
const batchConfig = ref<Partial<MdmItemBatchConfigApi.ItemBatchConfig>>({});
|
|
// 当前表单中的批次管理开关值(用于控制批次配置显示)
|
const currentIsBatchManaged = ref<boolean>(false);
|
|
const getTitle = computed(() => {
|
return formData.value?.id
|
? $t('ui.actionTitle.edit', ['物料'])
|
: $t('ui.actionTitle.create', ['物料']);
|
});
|
|
const [Form, formApi] = useVbenForm({
|
commonConfig: {
|
componentProps: {
|
class: 'w-full',
|
},
|
labelWidth: 110,
|
},
|
layout: 'horizontal',
|
schema: useFormSchema(),
|
showDefaultActions: false,
|
wrapperClass: 'grid-cols-2',
|
// 监听表单值变化
|
handleValuesChange: (values: any) => {
|
currentIsBatchManaged.value = values?.isBatchManaged === true;
|
},
|
});
|
|
const [Modal, modalApi] = useVbenModal({
|
async onConfirm() {
|
const { valid } = await formApi.validate();
|
if (!valid) {
|
return;
|
}
|
modalApi.lock();
|
const data = (await formApi.getValues()) as MdmItemApi.Item;
|
try {
|
// 保存物料基本信息
|
const itemId = await (formData.value?.id ? updateItem(data) : createItem(data));
|
const savedItemId = typeof itemId === 'number' ? itemId : data.id;
|
|
// 如果启用批次管理,保存批次配置
|
if (data.isBatchManaged && savedItemId) {
|
await saveItemBatchConfig({
|
...batchConfig.value,
|
itemId: savedItemId,
|
});
|
}
|
|
await modalApi.close();
|
emit('success');
|
message.success($t('ui.actionMessage.operationSuccess'));
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
async onOpenChange(isOpen: boolean) {
|
if (!isOpen) {
|
formData.value = undefined;
|
batchConfig.value = {};
|
currentIsBatchManaged.value = false;
|
return;
|
}
|
const data = modalApi.getData<MdmItemApi.Item>();
|
if (data?.id) {
|
// 编辑模式
|
modalApi.lock();
|
try {
|
formData.value = await getItem(data.id);
|
await formApi.setValues(formData.value);
|
currentIsBatchManaged.value = formData.value?.isBatchManaged === true;
|
// 加载批次配置
|
if (batchConfigRef.value && currentIsBatchManaged.value) {
|
await batchConfigRef.value.loadConfig();
|
}
|
} finally {
|
modalApi.unlock();
|
}
|
} else {
|
// 新增模式
|
formData.value = undefined;
|
await formApi.resetForm();
|
// 获取表单默认值(isBatchManaged 默认 true)
|
const defaultValues = await formApi.getValues();
|
currentIsBatchManaged.value = defaultValues?.isBatchManaged === true;
|
}
|
},
|
});
|
|
/** 处理批次配置变化 */
|
function handleBatchConfigChange(config: Partial<MdmItemBatchConfigApi.ItemBatchConfig>) {
|
batchConfig.value = config;
|
}
|
</script>
|
|
<template>
|
<Modal class="w-[1000px]" :title="getTitle">
|
<Form class="mx-4" />
|
<ItemBatchConfig
|
ref="batchConfigRef"
|
:item-id="formData?.id"
|
:is-batch-managed="currentIsBatchManaged"
|
@change="handleBatchConfigChange"
|
/>
|
</Modal>
|
</template>
|