import {FC} from 'react';
import {ColumnsType} from 'antd/es/table';
import {ReserveWarningListData} from '@models';
import {Card} from 'antd';
import {Table, TableTools} from '@components';
import {context, pageContext, searchContext} from '../context';
import {
  MATERIAL_CODE_COL_WIDTH,
  MATERIAL_NAME_COL_WIDTH,
  SMALL_TABLE_WIDTH,
} from '@utils';
import {exportWarning, getReserveWarningList} from '@apis';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';

const columns: ColumnsType<ReserveWarningListData> = [
  {
    title: '物料编号',
    dataIndex: 'wllbCode',
    key: 'wllbCode',
    width: MATERIAL_CODE_COL_WIDTH,
  },
  {
    title: '物料名称',
    dataIndex: 'materialName',
    key: 'materialName',
    width: MATERIAL_NAME_COL_WIDTH,
  },
  {
    title: '数量',
    dataIndex: 'amount',
    key: 'amount',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '最小数量',
    dataIndex: 'minNum',
    key: 'minNum',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '最大数量',
    dataIndex: 'maxNum',
    key: 'maxNum',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
];

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{data, count}] = useQueryTableList({
    queryFn: getReserveWarningList,
    params,
    pageContext,
    searchContext,
  });

  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    context,
    fn: exportWarning,
  });

  return (
    <Card className='table-wrapper'>
      <TableTools
        pageContext={pageContext}
        searchContext={searchContext}
        onExport={onExport}
        isExporting={isExporting}
      />

      <Table
        data={data}
        count={count}
        columns={columns}
        pageContext={pageContext}
        searchContext={searchContext}
        data-testid='reserve_warning_table'
      />
    </Card>
  );
};

export default TableList;
