import { formatParam } from '@/utils/formatParam';
import { ProList } from '@ant-design/pro-components';
import { BizListProps } from './typing';
import { ProListProps } from '@ant-design/pro-list';

export default function BizList<
	BizVO extends Record<string, any>,
	BizPageQry extends Record<string, any>,
	Func extends Record<string, any>,
>(props: BizListProps<BizVO, BizPageQry, Func>) {
	const { rowKey, method, func, methodUpper, metas, fieldProps } = props;

	const rest: ProListProps<BizVO, BizPageQry> = {
		rowKey: rowKey,
		scroll: { x: 'max-content' },
		...fieldProps,
		metas: metas,
		params: {
			...fieldProps?.params,
		} as any,
		...(fieldProps?.search !== false && {
			search: {
				labelWidth: 'auto',
				showHiddenNum: true,
			},
		}),
	};

	console.log(rest);
	return (
		<ProList<BizVO, BizPageQry>
			request={async (params, sorter, filter) => {
				const { data, success, totalCount } = await func?.[
					`list${methodUpper}`
				]({
					[`${method}ListQry`]: formatParam<typeof params>(
						params,
						sorter,
						filter,
					),
				});

				return {
					data,
					success,
					total: totalCount,
				};
			}}
			{...rest}
		/>
	);
}
