import {Card, Button, Table} from 'antd';
import {FC} from 'react';
import MenuModal from './modal';
import {useHandle, useList} from './hooks';
import {PAGE_SIZE_LIST} from '@utils';
import {useContextSection} from '@hooks';
import {context} from '../context';
import {TableTools} from '@components';

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

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools onClick={onAdd} testId='child_menu_add_btn'>
          <Button type='default' onClick={onClose!} data-testid='child_menu_back'>返回</Button>
        </TableTools>

        <Table
          data-testid='child_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;
