<script lang="ts" setup>
|
import type { VbenFormSchema } from "#/adapter/form";
|
import type { MesProPalletApi } from "#/api/mes/pro/pallet";
|
|
import { useVbenModal } from "@vben/common-ui";
|
import { MesProPalletStatusEnum } from "@vben/constants";
|
|
import { message } from "ant-design-vue";
|
|
import { useVbenForm } from "#/adapter/form";
|
import { bindBagCodePallet } from "#/api/mes/pro/bagCode";
|
import {
|
getPalletBindableList,
|
getPalletSimpleList,
|
} from "#/api/mes/pro/pallet";
|
import { $t } from "#/locales";
|
|
defineOptions({ name: "MesProBagCodeBindPallet" });
|
|
/** 弹窗传入数据(batchId 为空表示所选袋码尚未录入生产批次) */
|
interface ModalData {
|
batchId?: number;
|
batchCode?: string;
|
itemId?: number;
|
ids: number[];
|
}
|
|
const emit = defineEmits(["success"]);
|
|
/** 未录入批次时的托盘展示名:正式托盘带批次号、临时托盘回退到临时码,便于在混合列表中分辨 */
|
function palletLabel(pallet: MesProPalletApi.Pallet) {
|
const code = pallet.code || pallet.tempCode || "-";
|
const batchPart = pallet.batchCode ? `批次 ${pallet.batchCode}` : "临时托盘";
|
return `${code}(${batchPart} / 已装 ${pallet.bagCount ?? 0} 袋)`;
|
}
|
|
/** 构建表单(托盘下拉选项由外部加载注入) */
|
function buildSchema(palletOptions: { label: string; value: number }[]): VbenFormSchema[] {
|
return [
|
{
|
fieldName: "batchCode",
|
label: "生产批次",
|
component: "Input",
|
componentProps: {
|
disabled: true,
|
},
|
},
|
{
|
fieldName: "bagCount",
|
label: "绑定袋数",
|
component: "InputNumber",
|
componentProps: {
|
class: "!w-full",
|
disabled: true,
|
},
|
},
|
{
|
fieldName: "palletId",
|
label: "目标托盘",
|
component: "Select",
|
componentProps: {
|
optionFilterProp: "label",
|
options: palletOptions,
|
placeholder: "请选择目标托盘",
|
showSearch: true,
|
},
|
rules: "selectRequired",
|
},
|
];
|
}
|
|
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;
|
const data = modalApi.getData<ModalData>();
|
const values = await formApi.getValues();
|
modalApi.lock();
|
try {
|
await bindBagCodePallet({ palletId: values.palletId, ids: data.ids });
|
message.success($t("ui.actionMessage.operationSuccess"));
|
emit("success");
|
await modalApi.close();
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
async onOpenChange(isOpen: boolean) {
|
if (!isOpen) return;
|
const data = modalApi.getData<ModalData>();
|
modalApi.lock();
|
try {
|
// 已录入批次:只列本批次的托盘;未录入批次:列该品种可用的临时托盘与正式托盘,
|
// 选到正式托盘时由后端先把这批袋码补录进该批次,再完成绑定
|
const pallets = data.batchId
|
? await getPalletSimpleList(data.batchId)
|
: await getPalletBindableList(data.itemId);
|
const options = pallets
|
.filter((pallet) => pallet.status !== MesProPalletStatusEnum.CANCELED)
|
.map((pallet) => ({
|
label: data.batchId
|
? `${pallet.code}(已装 ${pallet.bagCount ?? 0} 袋)`
|
: palletLabel(pallet),
|
value: pallet.id!,
|
}));
|
await formApi.setState({ schema: buildSchema(options) });
|
await formApi.setValues({
|
bagCount: data.ids.length,
|
batchCode: data.batchCode || "未录入批次",
|
});
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
});
|
</script>
|
|
<template>
|
<Modal title="袋码绑定托盘" class="w-2/5">
|
<Form class="mx-4" />
|
</Modal>
|
</template>
|