import { json } from "@utils/ajax";
import { GET_SEARCH_RESULT, SET_SEARCH_VALUE } from '@store/types/diagnosticSearch';

export const getSearchResult = (value) =>{
    
    return (dispatch, getState) => {
        const state = getState();
        const url = '/api/icss/retrieval/getTagInfos'
        const param={
            type: 7,
            age: 44,
            inputStr: value,
            sexType: 1
        }
        if(value === state.diagnosticSearch.searchValue) {
            return
        } else {
            dispatch({
                type: SET_SEARCH_VALUE,
                searchValue: value
            })
            json(url, param).then((data)=>{
                const searchRes = data.data.data;
                const diagList = state.diagnosticList.diagnosticList;
                let searchResult = [];
                if(searchRes) {
                    for(let i = 0; i < searchRes.length; i++) {
                        let repeat = false;
                        for (let j = 0; j < diagList.length; j++) {
                            if(searchRes[i].questionId === diagList[j].id) {
                                repeat = true;
                            } 
                        }
                        if(!repeat) {
                            searchResult.push(searchRes[i])
                        }
                    }
                }
                dispatch({
                    type: GET_SEARCH_RESULT,
                    searchResult: searchResult
                })
            }).catch((e)=>{
                console.log(e)
            })


        }
        
    }

}
