import React, { useState } from 'react';
import InputSearch from '../../extra/dataEntry/textSelect';
import { ApolloInputSearchProps } from '../editInterface';
import { antiAssign } from '../../../../utils/utils';
import s from './index.less';

export const ApolloInputSearch = (props: ApolloInputSearchProps) => {
    const { onChange, request, onEmitChange, origin, value } = props;
    const selfProps = antiAssign(props, [
        'columnConfig',
        'onEmitChange',
        'value',
        'onChange',
        'request',
        'tableId',
        'rowId',
        'rowData',
        'cellRenderProps',
        'getCalendarContainer',
        'events',
        'isMultiple',
        'options',
        'disableOptions',
        'maxPopHeight',
        'form',
        'getInstanceDetail',
    ]);
    const [curValue, setCurValue] = useState(value);
    const changeValue = (value: any) => {
        setCurValue(value);
        if (typeof onChange === 'function') {
            onChange(value, value);
        }
        // 表格中的单选，选择后直接触发更新
        if (origin === 'table' && selfProps.mode !== 'multiple') {
            onBlur(value);
        }
    };
    const onBlur = (value: any) => {
        if (typeof onEmitChange === 'function') {
            onEmitChange(value, value);
        }
    };
    return (
        <InputSearch
            className={s.select}
            request={request}
            fieldNames={{ value: 'fieldValueValue', label: 'fieldValueName' }}
            {...selfProps}
            onChange={changeValue}
            initDataType="onfocus"
            value={curValue}
        />
    );
};
