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

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'>
        <section className='table-tool'>
          <Button type='primary' onClick={onAdd} data-testid='add_btn'>新增</Button>
        </section>

        <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;
