import {Table, TableTools} from '@components';
import {Card} from 'antd';
import {FC} from 'react';
import {useHandle} from './hooks';
import {context, pageContext, searchContext} from '../context';
import EditModal from './modal';
import {exportMaterial, getDictionaryList} from '@apis';
import {
  useContextSection,
  useQueryTableList,
  useTableExportEvent,
} from '@hooks';
import {DictionaryParamsType} from '@models';

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

  const [{data, count, isFetching}, {refetch}] = useQueryTableList({
    params,
    queryFn: getDictionaryList,
    pageContext,
    searchContext,
  });
  const [{columns, editId, visible}, {onClose}] = useHandle();
  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    fn: exportMaterial,
    context,
    parseParams(state) {
      return {...state, type: '物料字典' as DictionaryParamsType};
    },
  });

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

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

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

export default TableList;
