import React from 'react';
import { message } from 'antd';
import Search from '../../extra/associationSearch';
import { ApolloSearchProps } from '../editInterface';
import { antiAssign } from '../../../../utils/utils';
import s from './index.less';

export const ApolloSearch = (props: ApolloSearchProps) => {
    const { onChange, maxCount, isMultiple, request, type } = props;
    const selfProps = antiAssign(props, ['columnConfig', 'onChange', 'isMultiple', 'options', 'request', 'maxCount']);
    const changeValue = (value) => {
        if (maxCount && maxCount < value.length) {
            message.warn(`最多选择${maxCount}个`);
            return;
        }
        if (typeof onChange === 'function') {
            onChange(value, value);
        }
    };

    if (isMultiple) {
        selfProps.mode = 'multiple';
    }
    return (
        <Search
            className={s.search}
            request={request}
            fieldNames={{ value: 'fieldValueValue', label: 'fieldValueName' }}
            {...selfProps}
            onChange={changeValue}
            initDataType="onfocus"
        />
    );
};
