<script lang="ts" setup>
|
import type { CrmBusinessApi } from '#/api/crm/business';
|
import type { CrmContractApi } from '#/api/crm/contract';
|
import type { MdmItemApi } from '#/api/mdm/item';
|
|
import { nextTick, ref, watch } from 'vue';
|
|
import { erpPriceMultiply } from '@vben/utils';
|
|
import { InputNumber } from 'ant-design-vue';
|
|
import { TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
|
import { BizTypeEnum } from '#/api/crm/permission';
|
import { MdmItemSelect } from '#/views/basicData/mdm/components';
|
import { $t } from '#/locales';
|
|
import { useProductEditTableColumns } from './data';
|
|
const props = defineProps<{
|
bizType: BizTypeEnum;
|
products?:
|
| CrmBusinessApi.BusinessProduct[]
|
| CrmContractApi.ContractProduct[];
|
}>();
|
|
const emit = defineEmits(['update:products']);
|
|
/** 表格内部数据 */
|
const tableData = ref<any[]>([]);
|
|
/** 添加产品行 */
|
function handleAdd() {
|
gridApi.grid.insertAt(null, -1);
|
}
|
|
/** 删除产品行 */
|
function handleDelete(row: CrmProductApi.Product) {
|
gridApi.grid.remove(row);
|
}
|
|
/** 切换产品时同步基础信息 */
|
function handleProductChange(product: MdmItemApi.Item | undefined, row: any) {
|
if (!product) {
|
return;
|
}
|
row.itemId = product.id;
|
row.itemName = product.name;
|
row.itemCode = product.code;
|
row.itemBarCode = product.barCode;
|
row.itemUnitName = product.unitMeasureName;
|
row.itemUnitName2 = product.unitMeasureName2;
|
row.itemUnitName3 = product.unitMeasureName3;
|
row.itemSpecification = product.specification;
|
row.itemPrice = product.salesPrice;
|
row.contractPrice = product.salesPrice;
|
row.count = row.count || 1;
|
row.totalPrice = erpPriceMultiply(row.contractPrice, row.count) ?? 0;
|
handleUpdateValue(row);
|
}
|
|
/** 金额变动时重新计算合计 */
|
function handlePriceChange(row: any) {
|
row.totalPrice = erpPriceMultiply(row.contractPrice, row.count) ?? 0;
|
handleUpdateValue(row);
|
}
|
|
/** 将最新数据写回并通知父组件 */
|
function handleUpdateValue(row: any) {
|
const index = tableData.value.findIndex((item) => item.id === row.id);
|
row.totalPrice = erpPriceMultiply(row.contractPrice, row.count) ?? 0;
|
if (index === -1) {
|
row.id = tableData.value.length + 1;
|
tableData.value.push(row);
|
} else {
|
tableData.value[index] = row;
|
}
|
emit('update:products', [...tableData.value]);
|
}
|
|
const [Grid, gridApi] = useVbenVxeGrid({
|
gridOptions: {
|
editConfig: {
|
trigger: 'click',
|
mode: 'cell',
|
},
|
columns: useProductEditTableColumns(),
|
data: tableData.value,
|
border: true,
|
showOverflow: true,
|
autoResize: true,
|
keepSource: true,
|
rowConfig: {
|
keyField: 'id',
|
isHover: true,
|
},
|
pagerConfig: {
|
enabled: false,
|
},
|
toolbarConfig: {
|
enabled: false,
|
},
|
},
|
});
|
|
/** 监听外部传入的列数据 */
|
watch(
|
() => props.products,
|
async (products) => {
|
if (!products) {
|
return;
|
}
|
await nextTick();
|
tableData.value = products;
|
await gridApi.grid.reloadData(tableData.value);
|
},
|
{
|
immediate: true,
|
},
|
);
|
</script>
|
|
<template>
|
<Grid class="w-full">
|
<template #itemId="{ row }">
|
<MdmItemSelect
|
:model-value="row.itemId"
|
placeholder="请选择产品"
|
@update:model-value="row.itemId = $event"
|
@change="handleProductChange($event, row)"
|
/>
|
</template>
|
<template #contractPrice="{ row }">
|
<InputNumber
|
v-model:value="row.contractPrice"
|
:min="0.001"
|
:precision="2"
|
@change="handlePriceChange(row)"
|
/>
|
</template>
|
<template #count="{ row }">
|
<InputNumber
|
v-model:value="row.count"
|
:min="0.001"
|
:precision="3"
|
@change="handlePriceChange(row)"
|
/>
|
</template>
|
<template #bottom>
|
<TableAction
|
class="mt-4 flex justify-center"
|
:actions="[
|
{
|
label: '添加产品',
|
type: 'default',
|
onClick: handleAdd,
|
},
|
]"
|
/>
|
</template>
|
<template #actions="{ row }">
|
<TableAction
|
:actions="[
|
{
|
label: $t('common.delete'),
|
type: 'link',
|
danger: true,
|
popConfirm: {
|
title: $t('ui.actionMessage.deleteConfirm', [row.itemName]),
|
confirm: handleDelete.bind(null, row),
|
},
|
},
|
]"
|
/>
|
</template>
|
</Grid>
|
</template>
|