import {
  useContextSection,
  useQueryTableList,
  useSupertube,
  useTableExportEvent,
} from '@hooks';
import {FC} from 'react';
import {context, pageContext, searchContext} from '../content';
import {exportRelocationOrderList, getRelocationOrderList} from '@apis';
import {Card} from 'antd';
import {Table, TableTools} from '@components';
import {useColumns} from './hooks';
import {useBoolean} from 'ahooks';
import AddModal from './modal';

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{data, count, isFetching}, {refetch}] = useQueryTableList({
    queryFn: getRelocationOrderList,
    params,
    pageContext,
    searchContext,
  });
  const columns = useColumns(refetch);
  const isSuper = useSupertube();
  const [visible, {setTrue, setFalse}] = useBoolean();
  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    fn: exportRelocationOrderList,
    context,
  });

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools
          onAdd={isSuper ? setTrue : void 0}
          isRefreshing={isFetching}
          onRefresh={refetch}
          onExport={onExport}
          isExporting={isExporting}
        />

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

      {isSuper && (
        <AddModal visible={visible} onClose={setFalse} onFetch={refetch} />
      )}
    </>
  );
};

export default TableList;
