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

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

  return (
    <Card>
      <FilterFieldWrapper onSearch={onSearch} testId='container_scrap_filter'>
        <FilterField
          label='工号'
          name='operationName'
          value={fields.operationName}
          onChange={onChange('operationName')}
        />
        <FilterDatePicker
          label='日期选择'
          name='containerDate'
          value={dates}
          onChange={setDates}
        />
        <FilterButtonGroup
          onSearch={onSearch}
          isSearching={isSearching}
          onReset={onReset}
        />
      </FilterFieldWrapper>
    </Card>
  );
};

export default Filter;
