import React from 'react';
import Search from '../../extra/associationSearch';
import { approvalBusinessData } from '../../../../services/globalSearchApi';
import s from './index.less';
import { message } from 'antd';
import { SearchProps } from '../editInterface';

export default function(props: SearchProps) {
    const { value = [], onChange, maxCount, isMultiple, paramsJson = {}, type, tableId } = props;
    const changeValue = (...arg) => {
        const currentValue = arg[0] || [];
        if (maxCount && maxCount <= value.length && currentValue.length > value.length) {
            message.warn(`最多选择${maxCount}`);
            return;
        }
        if (typeof onChange === 'function') {
            onChange(...arg);
        }
    };
    if (tableId) {
        paramsJson.tableId = tableId;
    }
    return (
        <div className={s.searchContainer}>
            <Search
                mode={isMultiple ? 'multiple' : undefined}
                maxCount={2}
                request={(val) => {
                    return approvalBusinessData({
                        name: val,
                        fieldValueName: type,
                        paramsJson: JSON.stringify(paramsJson),
                    });
                }}
                fieldNames={{ value: 'fieldValueValue', label: 'fieldValueName' }}
                {...props}
                onChange={changeValue}
                initDataType="onfocus"
            />
        </div>
    );
}
