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

const Filter: FC = function () {
  const [fields, {onChange, resetState}] = useFilterField(
    {wllbCode: '', name: '', accountSleeve: ''},
    true,
  );
  const uuid = useContextSection(context, state => state[0].uuid);
  const [, {onSearch, onReset}] = useTableSearchToolEvents(
    context,
    {...fields, uuid},
    {
      resetCallback: resetState,
    },
  );
  const {isSearching} = useContextSection(searchContext, state => state[0]);

  return (
    <Card>
      <FilterFieldWrapper onSearch={onSearch} testId='inventory_detail_filter'>
        <FilterField
          label='物料编号'
          name='wllbCode'
          onChange={onChange('wllbCode')}
          value={fields.wllbCode}
        />
        <FilterField
          label='物料名称'
          name='name'
          onChange={onChange('name')}
          value={fields.name}
        />
        <FilterKeySelect
          label='所属公司'
          selectKey='公司'
          name='accountSleeves'
          onChange={onChange('accountSleeve')}
          value={fields.accountSleeve}
        />
        <FilterButtonGroup
          onSearch={onSearch}
          onReset={onReset}
          isSearching={isSearching}
        />
      </FilterFieldWrapper>
    </Card>
  );
};

export default Filter;
