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

const options = [
  {label: '质检单', value: '质检单'},
  {label: '生产领料单', value: '生产领料'},
  {label: '非生产领料单', value: '非生产领料'},
  {label: '报工单', value: '报工单'},
  {label: '交货单', value: '交货单'},
  {label: '移库单', value: '移库单'},
  {label: '其他入库', value: '其他入库'},
  {label: '其他出库', value: '其他出库'},
];

const Filter: FC = function () {
  const [{dates, start, end}, onDatesChange] = useRangeDate();
  const [fields, {onChange, resetState}] = useFilterField(
    {documentType: '', data: '', userName: ''},
    true,
  );
  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='order_log_filter'>
        <FilterField
          name='orderLogData'
          label='单据内容'
          value={fields.data}
          onChange={onChange('data')}
        />
        <FilterSelect
          name='orderLogType'
          label='单据类型'
          value={fields.documentType}
          onChange={onChange('documentType')}
          options={options}
        />
        <FilterField
          name='userName'
          label='工号'
          value={fields.userName}
          onChange={onChange('userName')}
        />
        <FilterDatePicker
          label='操作时间'
          value={dates}
          name='orderLogDate'
          onChange={onDatesChange}
        />
        <FilterButtonGroup
          onSearch={onSearch}
          isSearching={isSearching}
          onReset={onReset}
        />
      </FilterFieldWrapper>
    </Card>
  );
};

export default Filter;
