import { json } from "@utils/ajax";
import { GET_SEARCH_RESULT, SHOWNODATATEXT } from '@store/types/diagnosticSearch';
import { GET_SEARCH } from '@store/types/emergencyHis';
import {  Notify } from '@commonComp';
import { formatFormParmas } from '@utils/tools';
import store from '@store';

export const getSearchList = (val,flag) => {
    const state = store.getState();
    //console.log(state)
    const showTcm = state.diagnosticSearch.showTcm;
    const showSym = state.diagnosticSearch.showSym;
    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();
        let baseList = state;
        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('/demo/retrieval/index',{
            "type":showTcm?'12':(showSym?'13':'4'),
            "age": formatFormParmas('patientAge',state.patInfo.patInfoData),
            "inputStr": val.trim(),
            "sex": formatFormParmas('patientSex',state.patInfo.patInfoData),
            "hospitalId":baseList.homePage.curHos.hosId
        })
        .then((res)=>{
            if(res.data.code==0){
                const data = res.data.data;let curDate=[];

                let arr = data[showTcm?'tcmdiseaseNames':(showSym?'tcmsyndromeNames':'diseaseNames')]||[];
                for(let i = 0;i < arr.length;i++){
                    let tmp = arr[i]
                    let obj = {}
                    obj['name'] = tmp.name;
                    obj['uniqueName'] = tmp.name;
                    obj['conceptId'] = '';
                    obj['icd10Code'] = tmp.icd10Code||tmp.code;
                    //obj['code'] = tmp.code;
                    curDate.push(obj)
                }
                if(flag){ 
                    dispatch({
                        type: GET_SEARCH,
                        searchResult: curDate
                    })
                }else{
                    dispatch({
                        type: GET_SEARCH_RESULT,
                        searchResult: curDate
                    })
                    // 是否进行过搜索
                    dispatch({
                        type: SHOWNODATATEXT,
                        isShowNoDataInfo: true
                    });
                }
            }else{
                Notify.error(res.data.msg)
            }
        })
    }
};

