import {FC} from 'react';
import {useHandle} from './hooks';
import {Card} from 'antd';
import {Table, TableTools} from '@components';
import {context, pageContext, searchContext} from '../context';
import {getSemiManufacturesDrawList} from '@apis';
import {useContextSection, useQueryTableList, useSupertube} from '@hooks';
import {useBoolean} from 'ahooks';
import AddModal from './modal';
import PutOutModal from './put-out-modal';

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);

  const [{data, count}, {refetch}] = useQueryTableList({
    queryFn: getSemiManufacturesDrawList,
    params,
    pageContext,
    searchContext,
  });
  const [{columns, visible: putOutVisible, putOutId}, {onClose}] =
    useHandle(refetch);
  const [visible, {setTrue, setFalse}] = useBoolean();
  const isSuper = useSupertube();

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

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

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

export default TableList;
