import { formatParam } from '@/utils/formatParam';
import { ProTable, ProTableProps } from '@ant-design/pro-components';
import { BizTreeProps, BizValueType } from './typing';

export default function BizTree<
	BizVO extends Record<string, any>,
	BizPageQry extends Record<string, any>,
	Api extends Record<string, any>,
>(props: BizTreeProps<BizVO, BizPageQry, Api>) {
	const { rowKey, method, func, methodUpper, columns, fieldProps } = props;

	const rest: ProTableProps<BizVO, BizPageQry, BizValueType> = {
		rowKey: rowKey,
		...fieldProps,
		columns: columns,
		columnsState: {
			persistenceType: 'sessionStorage',
			persistenceKey: method + 'ColumnStateKey',
			defaultValue: {
				// option: { show: true, fixed: 'right' },
			},
		},
		scroll: { x: 'max-content' },
		params: {
			...fieldProps?.params,
		} as any,
		...(fieldProps?.search !== false && {
			search: {
				labelWidth: 'auto',
				showHiddenNum: true,
			},
		}),
	};

	return (
		<ProTable<BizVO, BizPageQry, BizValueType>
			request={async (params, sorter, filter) => {
				const { data, success, totalCount } = await func?.[
					`tree${methodUpper}`
				]({
					[`${method}TreeQry`]: formatParam<typeof params>(
						params,
						sorter,
						filter,
					),
				});

				return {
					data,
					success,
					total: totalCount,
				};
			}}
			expandable={{
				childrenColumnName: 'children',
			}}
			pagination={false}
			{...rest}
		/>
	);
}
