import {
  useContextSection,
  useFilterField,
  useRangeDate,
  useTableSearchToolEvents,
} from '@hooks';
import {Card} from 'antd';
import {FC} from 'react';
import {
  FilterButtonGroup,
  FilterDatePicker,
  FilterField,
  FilterFieldWrapper,
} from '@components';
import {context, searchContext} from '../context';

const Filter: FC = function () {
  const [fields, {onChange, resetState}] = useFilterField(
    {title: '', userName: ''},
    true,
  );
  const [{dates, start, end}, onDatesChange] = useRangeDate();
  const [, {onSearch, onReset}] = useTableSearchToolEvents(
    context,
    {...fields, startTime: start, endTime: end},
    {
      resetCallback() {
        resetState();
        onDatesChange([null, null]);
      },
    },
  );
  const {isSearching} = useContextSection(searchContext, state => state[0]);

  return (
    <Card>
      <FilterFieldWrapper onSearch={onSearch} testId='inventory_filter'>
        <FilterField
          label='盘点标题'
          name='inventory'
          value={fields.title}
          onChange={onChange('title')}
        />
        <FilterField
          label='盘点人'
          name='userName'
          value={fields.userName}
          onChange={onChange('userName')}
        />
        <FilterDatePicker
          label='盘点时间'
          name='inventoryDates'
          value={dates}
          onChange={onDatesChange}
        />
        <FilterButtonGroup
          onReset={onReset}
          onSearch={onSearch}
          isSearching={isSearching}
        />
      </FilterFieldWrapper>
    </Card>
  );
};

export default Filter;
