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

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

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

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools
          title='二级菜单管理'
          onAdd={onAdd}
          onRefresh={refetch}
          isRefreshing={isFetching}
        />

        <Table
          data-testid='child_menu_table'
          columns={columns}
          data={data}
          pageContext={pageContext}
          searchContext={searchContext}
          count={count}
          pageSizeList={MODAL_PAGE_SIZE_LIST}
        />
      </Card>
      <MenuModal
        visible={visible}
        onClose={onModalClose}
        id={editId}
        onFetch={refetch}
      />
    </>
  );
};

export default TableList;
