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

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

  const [{data, count, isFetching}, {refetch}] = useQueryTableList({
    queryFn: getMaterialBindList,
    params,
    pageContext,
    searchContext,
  });
  const [{visible, columns, editId}, {onAdd, onClose}] = useHandle(refetch);
  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    context,
    fn: exportUserBind,
  });
  const onDownload = useDownlaod('/物料用户绑定模板.xlsx');
  const uploadProps = useUpload('/userMaterial/import', refetch);

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools
          onAdd={onAdd}
          onRefresh={refetch}
          onExport={onExport}
          isRefreshing={isFetching}
          isExporting={isExporting}
          onDownload={onDownload}
          {...uploadProps}
        />

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

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

export default TableList;
