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

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

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

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

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

export default TableList;
