import {FC} from 'react';
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 {useColumns} from './hooks';
import {useBoolean} from 'ahooks';
import AddModal from './modal';

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{count, data}, {refetch}] = useQueryTableList({
    queryFn: getNoticeList,
    params,
    pageContext,
    searchContext,
  });
  const columns = useColumns(refetch);
  const [visible, {setFalse, setTrue}] = useBoolean();
  const isSuper = useSupertube();

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

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

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

export default TableList;
