import {Card} from 'antd';
import {FC} from 'react';
import MenuModal from './modal';
import {useHandle} from './hooks';
import {Table, TableTools} from '@components';
import {context, pageContext, searchContext} from '../context';
import {getPDAMenuList} from '@apis';
import {useContextSection, useQueryTableList} from '@hooks';

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

  const [{data, count}, {refetch}] = useQueryTableList({
    queryFn: getPDAMenuList,
    params,
    pageContext,
    searchContext,
  });
  const [{columns, visible, editId}, {onModalClose, onAdd}] = useHandle();

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

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

      <MenuModal
        visible={visible}
        onClose={onModalClose}
        id={editId}
        onFetch={refetch}
      />
    </>
  );
};

export default TableList;
