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

const Filter: FC = function () {
  const [{dates, start, end}, onDatesChange] = useRangeDate();
  const [fields, {onChange, resetState}] = useFilterField(
    {
      type: '',
      wllbCode: '',
      startTime: '',
      endTime: '',
      partType: '半成品' as const,
      materialName: '',
      companyNumber: '',
      noticeCode: '',
      noticeId: '',
      wbs: '',
      entryNumber: '',
      productionCode: '',
      moveType: '',
      sourceType: '',
    },
    true,
  );
  const {isSearching} = useContextSection(searchContext, state => state[0]);
  const [visible, {onSearch, onShowModal, onCloseModal, onReset}] =
    useTableSearchToolEvents(
      context,
      {...fields, startTime: start, endTime: end},
      {
        resetCallback() {
          resetState();
          onDatesChange([null, null]);
        },
      },
    );
  const [filterList, {set}] = useFilterDB();

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

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

export default Filter;
