import { ParamsType, ProTableProps } from '@ant-design/pro-components';
import { SortOrder } from 'antd/es/table/interface';

export const MyProTableProps = {
  props: {
    scroll: { x: 'max-content', scrollToFirstRowOnChange: true },
    //修改列表的样式
    bordered: true,
    size: 'middle',
    rowKey: 'id',
    pagination: {
      showTotal: (total) => `总共${total}条`,
      showSizeChanger: true,
      showQuickJumper: true,
    },
    search: {
      defaultCollapsed: false,
    },
  } as ProTableProps<Record<string, any>, ParamsType, 'text'>,

  request: async (
    params: Record<string, any> & {
      pageSize?: number;
      current?: number;
      keyword?: string;
    },
    sort: Record<string, SortOrder>,
    api: (data?: any) => any,
    setParams?: (params: any) => void,
    setRes?: (res: any) => void,
    // defaultParams?: Record<string, any>,
  ) => {
    const sortKeys = Object.keys(sort);
    const sorter =
      sortKeys.length > 0 ? [sortKeys[0], sort[sortKeys[0]]] : undefined;
    const { current, pageSize, ...rest } = params;
    const body = {
      page: current,
      perPage: pageSize,
      ...rest,
      sorter: sorter,
      // ...defaultParams,
    };
    setParams?.(body);
    const data = await api(body);
    setRes?.(data);
    return {
      data: data.data,
      success: data.success,
      total: data.meta?.total,
    };
  },
};
