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

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

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

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

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

export default TableList;
