import { computed, unref, type ComputedRef, type Ref } from 'vue' import type { Plan } from '@/api/pl' import { buildPlPlanListSpanMethod, flattenPlanListRows, getPlanListDisplayIndex, type PlPlanListDisplayRow } from '@/utils/pl-plan-list-display-rows' export function usePlPlanMergeListTable(options: { planList: Ref | ComputedRef visiblePlanMergeProps: ComputedRef selectedRows: Ref pagination: { page: number; pageSize: number } onView?: (plan: Plan) => void /** 计划区固定合并列数:默认 3(勾选+序号+计划编号);无勾选列时传 2 */ fixedMergeColCount?: number }) { const displayRows = computed(() => flattenPlanListRows(unref(options.planList))) const planListSpanMethod = computed(() => buildPlPlanListSpanMethod( [...options.visiblePlanMergeProps.value], options.fixedMergeColCount ) ) function getDisplayRowIndex(index: number) { return getPlanListDisplayIndex( displayRows.value, index, options.pagination.page, options.pagination.pageSize ) } function isPlanFirstRowSelectable(row: PlPlanListDisplayRow) { return row.isPlanFirstRow } function handleDisplaySelectionChange(selection: PlPlanListDisplayRow[]) { const seen = new Set() const plans: Plan[] = [] for (const item of selection || []) { const plan = item?.plan if (!plan?.id || seen.has(plan.id)) continue seen.add(plan.id) plans.push(plan) } options.selectedRows.value = plans } function handleDisplayRowDblClick(row: PlPlanListDisplayRow) { if (row?.plan && options.onView) options.onView(row.plan) } function findPlanFirstDisplayRow(planId: string) { return displayRows.value.find((item) => item.plan.id === planId && item.isPlanFirstRow) } return { displayRows, planListSpanMethod, getDisplayRowIndex, isPlanFirstRowSelectable, handleDisplaySelectionChange, handleDisplayRowDblClick, findPlanFirstDisplayRow } }