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

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

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

        <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} id={editState} />
    </>
  );
};

export default TableList;
