import {
  useContextSection,
  useQueryTableList,
  useSupertube,
  useTableExportEvent,
} from '@hooks';
import {FC} from 'react';
import {context, pageContext, searchContext} from '../context';
import {exportSellOrderList, getSellOrderList} 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}, {refetch}] = useQueryTableList({
    queryFn: getSellOrderList,
    params,
    pageContext,
    searchContext,
  });
  const columns = useColumns(refetch);
  const [visible, {setTrue, setFalse}] = useBoolean();
  const isSuper = useSupertube();
  const [isExporting, onExport] = useTableExportEvent({
    fn: exportSellOrderList,
    context,
    pageContext,
  });

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

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

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

export default TableList;
