<script lang="ts" setup>
|
import type { MesProRouteProcessApi } from '#/api/mes/pro/route/process';
|
import type { MesProRouteProductApi } from '#/api/mes/pro/route/product';
|
|
import { computed } from 'vue';
|
|
import { IconifyIcon } from '@vben/icons';
|
|
import { Button, Select } from 'ant-design-vue';
|
|
defineOptions({ name: 'RouteOutputAttribution' });
|
|
/** 归属选择中代表“多产品通用”的哨兵值(产品编号均为正数) */
|
const GENERAL_PRODUCT = 0;
|
|
const props = withDefaults(
|
defineProps<{
|
disabled?: boolean;
|
modelValue?: MesProRouteProcessApi.OutputItem[];
|
products?: MesProRouteProductApi.RouteProduct[];
|
}>(),
|
{
|
disabled: false,
|
modelValue: () => [],
|
products: () => [],
|
},
|
);
|
|
const emit = defineEmits<{
|
'update:modelValue': [value: MesProRouteProcessApi.OutputItem[]];
|
}>();
|
|
const rows = computed(() => props.modelValue ?? []);
|
const productOptions = computed(() =>
|
props.products
|
.filter((product) => product.itemId != null)
|
.map((product) => ({
|
label: product.itemName ?? product.itemCode ?? String(product.itemId),
|
value: product.itemId!,
|
})),
|
);
|
const attributionOptions = computed(() => [
|
{ label: '通用(所有产品)', value: GENERAL_PRODUCT },
|
...productOptions.value,
|
]);
|
|
function commit(next: MesProRouteProcessApi.OutputItem[]) {
|
emit('update:modelValue', next);
|
}
|
|
function updateRow(
|
index: number,
|
patch: Partial<MesProRouteProcessApi.OutputItem>,
|
) {
|
commit(rows.value.map((row, i) => (i === index ? { ...row, ...patch } : row)));
|
}
|
|
/** 选择产出物料时,默认归属到自身产品(半成品共用时再手动改成“通用”) */
|
function handleItemChange(index: number, itemId: number | undefined) {
|
const current = rows.value[index];
|
const patch: Partial<MesProRouteProcessApi.OutputItem> = { itemId };
|
// 原来归属自身时,切换产出物料也要同步归属,避免留下旧产品编号。
|
if ((current?.productId == null || current.productId === current.itemId) && itemId != null) {
|
patch.productId = itemId;
|
}
|
updateRow(index, patch);
|
}
|
|
function handleAttributionChange(index: number, value: number | undefined) {
|
updateRow(index, {
|
productId: value === GENERAL_PRODUCT || value == null ? undefined : value,
|
});
|
}
|
|
function addRow() {
|
commit([...rows.value, { itemId: undefined, productId: undefined }]);
|
}
|
|
function removeRow(index: number) {
|
commit(rows.value.filter((_, i) => i !== index));
|
}
|
</script>
|
|
<template>
|
<div class="w-full">
|
<div class="flex flex-col gap-2">
|
<div
|
v-for="(row, index) in rows"
|
:key="index"
|
class="flex items-center gap-2"
|
>
|
<Select
|
:disabled="disabled"
|
:options="productOptions"
|
:placeholder="'选择产出物料'"
|
:value="row.itemId"
|
class="min-w-0 flex-1"
|
option-filter-prop="label"
|
show-search
|
@change="(value) => handleItemChange(index, value as number | undefined)"
|
/>
|
<Select
|
:disabled="disabled || row.itemId == null"
|
:options="attributionOptions"
|
:value="row.productId ?? GENERAL_PRODUCT"
|
class="min-w-0 flex-1"
|
@change="(value) => handleAttributionChange(index, value as number | undefined)"
|
/>
|
<Button
|
:aria-label="`删除第 ${index + 1} 条产出配置`"
|
:disabled="disabled"
|
danger
|
size="small"
|
type="text"
|
@click="removeRow(index)"
|
>
|
<IconifyIcon class="size-4" icon="lucide:trash-2" />
|
</Button>
|
</div>
|
<div v-if="rows.length === 0" class="text-sm text-gray-500">
|
暂无产出;形成半成品或成品时添加
|
</div>
|
</div>
|
<Button
|
v-if="!disabled"
|
class="mt-2"
|
size="small"
|
type="dashed"
|
@click="addRow"
|
>
|
<IconifyIcon class="mr-1 size-4" icon="lucide:plus" />
|
添加产出
|
</Button>
|
</div>
|
</template>
|