import {Table, TableTools} from '@components';
import {
  useContextSection,
  useDownlaod,
  useQueryTableList,
  useTableExportEvent,
  useUpload,
} from '@hooks';
import {Card} from 'antd';
import {FC} from 'react';
import {context, pageContext, searchContext} from '../context';
import {exportTraceBack, getTraceBackList} from '@apis';
import {useColumns} from './hooks';
import SnListModal from './modal';

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{data, isFetching, count}, {refetch}] = useQueryTableList({
    queryFn: getTraceBackList,
    pageContext,
    searchContext,
    params,
  });
  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    context,
    fn: exportTraceBack,
  });
  const onDownload = useDownlaod('/发货任务.xlsx');
  const uploadProps = useUpload('/traceBack/import', refetch);
  const [{columns, editId, visible}, {onClose}] = useColumns(refetch);

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools
          isRefreshing={isFetching}
          onRefresh={refetch}
          isExporting={isExporting}
          onExport={onExport}
          {...uploadProps}
          onDownload={onDownload}
        />

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

      <SnListModal id={editId} visible={visible} onClose={onClose} />
    </>
  );
};

export default TableList;
