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

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

  const [{data, count, isFetching}, {refetch}] = useQueryTableList({
    queryFn: getMaterialList,
    params,
    pageContext,
    searchContext,
  });
  const [{visible, editId, columns}, {onAdd, onClose}] = useHandle(refetch);
  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    context,
    fn: exportMatter,
  });

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

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

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

export default TableList;
