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

const Filter: FC = function () {
  const [fields, {onChange, resetState}] = useFilterField(
    {roleName: '', roleCode: '', modifyUser: ''},
    true,
  );
  const [, {onSearch, onReset}] = useTableSearchToolEvents(context, fields, {
    resetCallback: resetState,
  });

  const {isSearching} = useContextSection(searchContext, state => state[0]);

  return (
    <Card>
      <FilterFieldWrapper onSearch={onSearch} testId='role_filter'>
        <FilterField
          label='角色编号'
          name='roleCode'
          value={fields.roleCode}
          onChange={onChange('roleCode')}
        />
        <FilterField
          label='角色名称'
          name='roleName'
          value={fields.roleName}
          onChange={onChange('roleName')}
        />
        <FilterField
          label='修改人'
          name='modifyUser'
          value={fields.modifyUser}
          onChange={onChange('modifyUser')}
        />
        <FilterButtonGroup
          onSearch={onSearch}
          isSearching={isSearching}
          onReset={onReset}
        />
      </FilterFieldWrapper>
    </Card>
  );
};

export default Filter;
