import {Card, Table} from 'antd';
import {FC} from 'react';
import MenuModal from './modal';
import {useHandle, useList} from './hooks';
import {PAGE_SIZE_LIST} from '@utils';
import ChildMenuModal from '../child-menu';
import {TableTools} from '@components';

const TableList: FC = function() {
  const [{isFetching, data, page, pageSize, count}, {refetch, onPageChange}] = useList();
  const [
    {columns, visible, editId, pid, childVisible},
    {onModalClose, onAdd, closeChildModal},
  ] = useHandle(refetch);

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

        <Table
          data-testid='menu_table'
          bordered
          columns={columns}
          dataSource={data}
          loading={isFetching}
          rowKey='id'
          pagination={{
            pageSize,
            pageSizeOptions: PAGE_SIZE_LIST,
            total: count,
            showSizeChanger: true,
            onChange: onPageChange,
            current: page,
          }}
        />
      </Card>
      <MenuModal visible={visible} onClose={onModalClose} id={editId} onFetch={refetch} />
      <ChildMenuModal visible={childVisible} id={pid} onClose={closeChildModal} />
    </>
  );
};

export default TableList;
