import {Card} from 'antd';
import {FC} from 'react';

import {Table, TableTools} from '@components';
import {context, pageContext, searchContext} from '../context';
import {getReceiveList} 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 [{data, count}, {refetch}] = useQueryTableList({
    queryFn: getReceiveList,
    params,
    pageContext,
    searchContext,
  });
  const columns = useColumns(refetch);
  const [visible, {setTrue: onAdd, setFalse: onClose}] = useBoolean();
  const isSuper = useSupertube();

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

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

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

export default TableList;
