import {Card, Button, Table, Space} 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';

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'>
        <section className='table-tool'>
          <Space>
            <Button type='primary' onClick={onAdd}>新增</Button>
          </Space>
        </section>

        <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;
