import {FC} from 'react';
import {Button, Card, Table} from 'antd';
import {useHandle, useList} from './hooks';
import Modal from './modal';
import {PAGE_SIZE_LIST} from '@utils';

const TableList: FC = function() {
  const [
    {count, data, isFetching, page, pageSize},
    {onPageChange, refetch},
  ] = useList();
  const [
    {modalVisible, columns, editState},
    {onAdd, setFalse},
  ] = useHandle(refetch, data);

  return (
    <>
      <Card className='table-wrapper'>
        <section className='table-tool'>
          <Button type='primary' onClick={onAdd} data-testid='add_btn'>新增</Button>
        </section>
        <Table
          data-testid='department_table'
          columns={columns}
          dataSource={data}
          pagination={{
            pageSize,
            pageSizeOptions: PAGE_SIZE_LIST,
            total: count,
            showSizeChanger: true,
            onChange: onPageChange,
            current: page,
          }}
          rowKey='code'
          loading={isFetching}
          bordered
        />
      </Card>
      <Modal visible={modalVisible} onClose={setFalse} onFetch={refetch} {...editState} />
    </>
  );
};

export default TableList;
