import {Card} from 'antd';
import {FC} from 'react';
import {PRELOAD_KEY, context, pageContext, searchContext} from '../context';
import {ScrapContainerLogData} from '@models';
import {ColumnsType} from 'antd/es/table';
import {Table, TableTools} from '@components';
import {
  MIDDLE_TABLE_WIDTH,
  NORMAL_TABLE_WIDTH,
  SMALL_TABLE_WIDTH,
} from '@utils';
import {containerScrapLogExport, containerScrapLogList} from '@apis';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';

const columns: ColumnsType<ScrapContainerLogData> = [
  {
    title: '工号',
    dataIndex: 'operationName',
    key: 'operationName',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '报废原因',
    dataIndex: 'scrapCause',
    key: 'scrapCause',
    width: MIDDLE_TABLE_WIDTH,
  },
  {
    title: '报废时间',
    dataIndex: 'scrapTime',
    key: 'scrapTime',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '报废数量',
    dataIndex: 'scrapNum',
    key: 'scrapNum',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
];

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);

  const [{data, count}] = useQueryTableList({
    queryFn: containerScrapLogList,
    params,
    pageContext,
    searchContext,
  });

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

  return (
    <Card className='table-wrapper'>
      <TableTools
        title='容器菜单列表'
        isExporting={isExporting}
        onExport={onExport}
        pageContext={pageContext}
        searchContext={searchContext}
        preloadKey={PRELOAD_KEY}
      />

      <Table
        data-testid='container_scrap_table'
        columns={columns}
        data={data}
        pageContext={pageContext}
        searchContext={searchContext}
        count={count}
        preloadKey={PRELOAD_KEY}
        isSecondLevel
      />
    </Card>
  );
};

export default TableList;
