import {
  ActionType,
  ParamsType,
  ProColumns,
  ProTable,
  ProTableProps,
} from '@ant-design/pro-components';
import { useState } from 'react';

const DEFAULT_PAGESIZE = 10;

/** 干掉ProTable的默认配置 */
const defaultSetting: any = {
  bordered: true,
  loading: false,
};

export type { ActionType, ProTableProps as BaseTableProps, ProColumns };

/** @name 表格组件(若有额外的功能，自行在该组件封装添加) */

export default function BaseTable<
  DataType extends Record<string, any>,
  Params extends ParamsType = ParamsType,
  ValueType = 'text',
>(props: ProTableProps<DataType, Params, ValueType>): JSX.Element {
  const { request, pagination = {}, scroll, ...rest } = props;
  const [total, setTotal] = useState(0);
  return (
    <ProTable
      style={{ marginBottom: '50px' }}
      scroll={scroll ? scroll : { x: 0 }}
      {...defaultSetting}
      {...rest}
      request={async (params: Params, sort, filter) => {
        const baseData = await request?.(params, sort, filter);
        setTotal(baseData?.total || 0);
        return baseData;
      }}
      pagination={
        pagination
          ? {
              defaultPageSize: DEFAULT_PAGESIZE,
              showQuickJumper: total > DEFAULT_PAGESIZE,
              showSizeChanger: total > DEFAULT_PAGESIZE,
              hideOnSinglePage: total <= DEFAULT_PAGESIZE,
              ...pagination,
            }
          : false
      }
    />
  );
}
