import {Table, TableTools} from '@components';
import {Card} from 'antd';
import {FC} from 'react';
import {useHandle} from './hooks';
import {context, pageContext, searchContext} from '../context';
import PutModal from './put-modal';
import ScrapModal from './scrap-modal';
import ScrapLogModal from './scrap-log-modal';
import {exportContainer, getContainerList} from '@apis';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{data, count}, {refetch}] = useQueryTableList({
    queryFn: getContainerList,
    params,
    pageContext,
    searchContext,
  });
  const [
    {columns, putVisible, editId, scrapVisible, scrapId, logId, logVisible},
    {onPutClose, onAdd, onScrapClose, onLogClose},
  ] = useHandle(refetch);
  const [isExporting, onExport] = useTableExportEvent({
    fn: exportContainer,
    context,
    pageContext,
  });

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools
          onAdd={onAdd}
          pageContext={pageContext}
          searchContext={searchContext}
          onExport={onExport}
          isExporting={isExporting}
        />

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

      <PutModal
        visible={putVisible}
        onClose={onPutClose}
        onFetch={refetch}
        id={editId}
      />
      <ScrapModal
        visible={scrapVisible}
        id={scrapId}
        onClose={onScrapClose}
        onFetch={refetch}
      />
      <ScrapLogModal visible={logVisible} id={logId} onClose={onLogClose} />
    </>
  );
};

export default TableList;
