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 {TableTools} from '@components';

const TableList: FC = function() {
  const [{isFetching, data, page, pageSize, count}, {refetch, onPageChange}] = useList();
  const [
    {columns, visible, editId},
    {onModalClose, onAdd},
  ] = 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} />
    </>
  );
};

export default TableList;
