import {exportTraceBackSN, getTraceBackSNList} from '@apis';
import {Table, TableTools} from '@components';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';
import {Card} from 'antd';
import {FC} from 'react';
import {PRELOAD_KEY, context, pageContext, searchContext} from '../context';
import {ColumnsType} from 'antd/es/table';
import {CODE_COL_WIDTH, NORMAL_TABLE_WIDTH} from '@utils';
import {GetTraceBackSNListData} from '@models';

const columns: ColumnsType<GetTraceBackSNListData> = [
  {title: 'SN编号', dataIndex: 'snCode', width: CODE_COL_WIDTH},
  {title: '交货单号', dataIndex: 'deliveryOrder', width: CODE_COL_WIDTH},
  {title: '销售订单编号', dataIndex: 'deliveryCode', width: CODE_COL_WIDTH},
  {
    title: '销售公司编号',
    dataIndex: 'deliveryCompany',
    width: NORMAL_TABLE_WIDTH,
  },
];

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{data, count}] = useQueryTableList({
    queryFn: getTraceBackSNList,
    params,
    pageContext,
    searchContext,
  });
  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    context,
    fn: exportTraceBackSN,
  });

  return (
    <Card className='table-wrapper'>
      <TableTools
        title='发货详情'
        pageContext={pageContext}
        searchContext={searchContext}
        isExporting={isExporting}
        onExport={onExport}
        preloadKey={PRELOAD_KEY}
      />

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

export default TableList;
