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


export const getSearchList = (val,flag) => {
    if(val.trim() == ''){
        // const data = [];
        if(flag){//历史病历
            return (dispatch) =>  dispatch({
                type: GET_SEARCH,
                searchResult: []
            })
        }
        return (dispatch) =>  dispatch({//诊断
            type: GET_SEARCH_RESULT,
            searchResult: []
        })
    }
   
    return (dispatch, getState) => {
        const state = getState();
        const diagnosticList = flag?state.emergencyHis.searchData:state.diagnosticList.diagnosticList;
        let noIds = []
        for(let i = 0; i < diagnosticList.length; i++) {
            if(diagnosticList[i].conceptId) {
                noIds.push(diagnosticList[i].conceptId)
            }
        }
        json('/retrieval/getTagInfos',{
            type:'7',
            age:state.patInfo.message.patientAge,
            inputStr:val,
            sexType: state.patInfo.message.sex,
            inputIds: noIds
        })
        .then((res)=>{
            const data = res.data.data;
            if(flag){ //历史病历
                dispatch({
                    type: GET_SEARCH,
                    searchResult: data
                })
            }else{//诊断
                dispatch({
                    type: GET_SEARCH_RESULT,
                    searchResult: data
                })
            }
        })
    }
};

