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

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{data, count}, {refetch}] = useQueryTableList({
    queryFn: getNoticeList,
    params,
    pageContext,
    searchContext,
  });
  const [{columns, visible: putInVisible, editId: putInId}, {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_report_table'
          columns={columns}
          data={data}
          pageContext={pageContext}
          searchContext={searchContext}
          count={count}
        />
      </Card>

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

export default TableList;
