import {
  useContextSection,
  useFilterField,
  useRangeDate,
  useTableSearchToolEvents,
  useFilterDB,
} from '@hooks';
import {FC} from 'react';
import {context, searchContext} from '../content';
import {FilterSelectorModal, FilterTool} from '@components';
import {fixedMap, sourceMap} from './state';

const Filter: FC = function () {
  const [fields, {onChange, resetState}] = useFilterField(
    {
      warehouseTransferCode: '',
      startTime: '',
      endTime: '',
      materialName: '',
      askGoodsWarehouseaId: '',
      supplyWarehouseId: '',
      wbs: '',
      entryNumber: '',
      warehouseTransferType: '',
      materialCode: '',
      companyCode: '',
      askGoodsWarehouseId: '',
    },
    true,
  );
  const [{dates, start, end}, onDatesChange] = useRangeDate();
  const {isSearching} = useContextSection(searchContext, state => state[0]);
  const [visible, {onShowModal, onCloseModal, onReset, onSearch}] =
    useTableSearchToolEvents(
      context,
      {...fields, startTime: start, endTime: end},
      {
        resetCallback() {
          resetState();
          onDatesChange([null, null]);
        },
      },
    );
  const [filterList, {set}] = useFilterDB();

  return (
    <>
      <FilterTool
        onSearch={onSearch}
        onReset={onReset}
        isSearching={isSearching}
        onChange={onChange}
        onDatesChange={onDatesChange}
        fields={fields}
        dates={dates}
        sourceMap={sourceMap}
        fixedMap={fixedMap}
        onFilter={onShowModal}
        filterData={filterList}
        testId='relocation_filter'
      />

      <FilterSelectorModal
        visible={visible}
        onClose={onCloseModal}
        onConfirm={set}
        filtermap={sourceMap}
        source={filterList}
      />
    </>
  );
};

export default Filter;
