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

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

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

        <Table
          data-testid='user_table'
          bordered
          scroll={{x: 2050}}
          columns={columns}
          dataSource={data}
          pagination={{
            current: page,
            pageSize,
            onChange: onPageChange,
            pageSizeOptions: PAGE_SIZE_LIST,
            total: count,
            showSizeChanger: true,
          }}
          loading={isFetching}
          rowKey='id'
        />
      </Card>

      <UserModal visible={visible} onClose={onClose} onFetch={refetch} id={editId} />
    </>
  );
};

export default TableList;
