import {Card} from 'antd';
import {FC} from 'react';
import StorageModal from './modal';
import {context, pageContext, searchContext} from '../context';
import {useHandle} from './hooks';
import {Table, TableTools} from '@components';
import {exportStorage, getStorageList} from '@apis';
import {
  useContextSection,
  useDownlaod,
  useQueryTableList,
  useTableExportEvent,
  useUpload,
} from '@hooks';

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);

  const [{data, count}, {refetch}] = useQueryTableList({
    queryFn: getStorageList,
    params,
    pageContext,
    searchContext,
  });
  const [{columns, visible, editId}, {onAdd, onClose}] = useHandle(refetch);
  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    context,
    fn: exportStorage,
  });
  const onDownload = useDownlaod('/库位信息模板.xlsx');
  const uploadProps = useUpload('/storage/import', refetch);

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools
          pageContext={pageContext}
          searchContext={searchContext}
          title='库位管理'
          onAdd={onAdd}
          onExport={onExport}
          isExporting={isExporting}
          onDownload={onDownload}
          {...uploadProps}
        />

        <Table
          data-testid='storage_table'
          columns={columns}
          data={data}
          pageContext={pageContext}
          searchContext={searchContext}
          count={count}
        />
      </Card>

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

export default TableList;
