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

const TableList: FC = function() {
  const [{data, count}, {refetch}] = useList();
  const [
    {columns, putVisible, editId, scrapVisible, scrapId, logId, logVisible},
    {onPutClose, onAdd, onScrapClose, onLogClose},
  ] = useHandle(refetch);

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools onClick={onAdd} />

        <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;
