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

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

  const [{data, count}, {refetch}] = useQueryTableList({
    queryFn: getMenuList,
    params,
    pageContext,
    searchContext,
  });

  const [
    {columns, visible, editId, pid, childVisible},
    {onModalClose, onAdd, closeChildModal},
  ] = useHandle(refetch);

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools
          title='菜单管理'
          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}
      />
      <ChildMenuModal
        visible={childVisible}
        id={pid}
        onClose={closeChildModal}
      />
    </>
  );
};

export default TableList;
