<script lang="ts" setup>
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
import type { ErpPurchaseOrderApi } from '#/api/erp/purchase/order';
|
|
import { nextTick, ref } from 'vue';
|
|
import { message, Modal } from 'ant-design-vue';
|
|
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
import { getPurchaseOrderPage } from '#/api/erp/purchase/order';
|
|
defineOptions({ name: 'ErpPurchaseOrderSelectDialog' });
|
|
const emit = defineEmits<{
|
selected: [rows: ErpPurchaseOrderApi.PurchaseOrder[]];
|
}>();
|
|
const open = ref(false);
|
const multiple = ref(true);
|
const selectedRows = ref<ErpPurchaseOrderApi.PurchaseOrder[]>([]);
|
const preSelectedIds = ref<number[]>([]);
|
|
/** 获取当前表格数据 */
|
function getTableRows() {
|
return gridApi.grid.getTableData().fullData as ErpPurchaseOrderApi.PurchaseOrder[];
|
}
|
|
/** 获取多选记录 */
|
function getMultipleSelectedRows() {
|
const selectedMap = new Map<number, ErpPurchaseOrderApi.PurchaseOrder>();
|
const records = [
|
...(gridApi.grid.getCheckboxReserveRecords?.() ?? []),
|
...(gridApi.grid.getCheckboxRecords?.() ?? []),
|
] as ErpPurchaseOrderApi.PurchaseOrder[];
|
records.forEach((row) => {
|
if (row.id) {
|
selectedMap.set(row.id, row);
|
}
|
});
|
return [...selectedMap.values()];
|
}
|
|
/** 处理勾选变化 */
|
function handleCheckboxSelectChange() {
|
selectedRows.value = getMultipleSelectedRows();
|
}
|
|
/** 处理单选变化 */
|
function handleRadioChange(row: ErpPurchaseOrderApi.PurchaseOrder) {
|
selectedRows.value = [row];
|
}
|
|
/** 处理行双击 */
|
async function handleCellDblclick({ row }: { row: ErpPurchaseOrderApi.PurchaseOrder }) {
|
if (multiple.value) {
|
const selected = gridApi.grid.isCheckedByCheckboxRow(row);
|
await gridApi.grid.setCheckboxRow(row, !selected);
|
selectedRows.value = getMultipleSelectedRows();
|
return;
|
}
|
selectedRows.value = [row];
|
await gridApi.grid.setRadioRow(row);
|
handleConfirm();
|
}
|
|
/** 回显预选采购订单 */
|
async function applyPreSelection() {
|
if (preSelectedIds.value.length === 0) {
|
return;
|
}
|
const rows = getTableRows();
|
for (const row of rows) {
|
if (row.id && preSelectedIds.value.includes(row.id)) {
|
if (multiple.value) {
|
await gridApi.grid.setCheckboxRow(row, true);
|
} else {
|
await gridApi.grid.setRadioRow(row);
|
selectedRows.value = [row];
|
return;
|
}
|
}
|
}
|
if (multiple.value) {
|
selectedRows.value = getMultipleSelectedRows();
|
}
|
}
|
|
/** 列表字段 */
|
function useGridColumns(isMultiple: boolean): VxeTableGridOptions<ErpPurchaseOrderApi.PurchaseOrder>['columns'] {
|
return [
|
{ type: isMultiple ? 'checkbox' : 'radio', width: 50 },
|
{ field: 'no', title: '订单号', minWidth: 140 },
|
{ field: 'supplierName', title: '供应商', minWidth: 120 },
|
{ field: 'orderTime', title: '订单时间', width: 120, formatter: 'formatDate' },
|
{ field: 'totalCount', title: '合计数量', width: 100 },
|
{ field: 'totalPrice', title: '合计金额', width: 100 },
|
];
|
}
|
|
const [Grid, gridApi] = useVbenVxeGrid({
|
gridOptions: {
|
columns: useGridColumns(true),
|
height: 400,
|
keepSource: true,
|
checkboxConfig: {
|
highlight: true,
|
reserve: true,
|
},
|
radioConfig: {
|
highlight: true,
|
trigger: 'row',
|
},
|
proxyConfig: {
|
ajax: {
|
query: async ({ page }, formValues) => {
|
return await getPurchaseOrderPage({
|
pageNo: page.currentPage,
|
pageSize: page.pageSize,
|
...formValues,
|
});
|
},
|
},
|
},
|
rowConfig: {
|
keyField: 'id',
|
isHover: true,
|
},
|
} as VxeTableGridOptions<ErpPurchaseOrderApi.PurchaseOrder>,
|
gridEvents: {
|
cellDblclick: handleCellDblclick,
|
checkboxAll: handleCheckboxSelectChange,
|
checkboxChange: handleCheckboxSelectChange,
|
radioChange: ({ row }: { row: ErpPurchaseOrderApi.PurchaseOrder }) => {
|
handleRadioChange(row);
|
},
|
},
|
});
|
|
/** 重置查询和选择状态 */
|
async function resetQueryState() {
|
selectedRows.value = [];
|
await gridApi.grid.clearCheckboxRow();
|
await gridApi.grid.clearCheckboxReserve();
|
await gridApi.grid.clearRadioRow();
|
}
|
|
/** 打开采购订单选择弹窗 */
|
async function openModal(
|
selectedIds?: number[],
|
options?: { multiple?: boolean },
|
) {
|
open.value = true;
|
multiple.value = options?.multiple ?? true;
|
preSelectedIds.value = selectedIds || [];
|
await nextTick();
|
gridApi.setGridOptions({
|
columns: useGridColumns(multiple.value),
|
});
|
await resetQueryState();
|
await gridApi.query();
|
await nextTick();
|
await applyPreSelection();
|
}
|
|
/** 关闭采购订单选择弹窗 */
|
async function closeModal() {
|
open.value = false;
|
await resetQueryState();
|
}
|
|
/** 确认选择采购订单 */
|
function handleConfirm() {
|
const rows = multiple.value ? getMultipleSelectedRows() : selectedRows.value;
|
if (rows.length === 0) {
|
message.warning(multiple.value ? '请至少选择一条数据' : '请选择一条数据');
|
return;
|
}
|
emit('selected', multiple.value ? rows : [rows[0]!]);
|
open.value = false;
|
}
|
|
defineExpose({ open: openModal });
|
</script>
|
|
<template>
|
<Modal
|
v-model:open="open"
|
title="采购订单选择"
|
width="70%"
|
:destroy-on-close="true"
|
@ok="handleConfirm"
|
@cancel="closeModal"
|
>
|
<Grid table-title="采购订单列表" />
|
</Modal>
|
</template>
|