import axios from '@utils/ajax';
import store from '@store';
import { searchList,resetLabel } from '../actions/newAdvice';
import {  Notify } from '@commonComp';
import { formatFormParmas } from '@utils/tools';

export const getSearchList = (val) => {
    if(val.trim() == ''){
        const data = [];
        return (dispatch) => dispatch(searchList(data,''));
    }
    let baseList = store.getState();
    let type = baseList.newAdvice.shType;
    return (dispatch) => {
        axios.json('/demo/retrieval/index',{
            "inputStr": val.trim(),
            "hospitalId": 2,
            "age": formatFormParmas('patientAge',baseList.patInfo.patInfoData),
            "sex": formatFormParmas('patientSex',baseList.patInfo.patInfoData),
            "type": type,
            "hospitalId":baseList.homePage.curHos.hosId
        })
        .then((res)=>{
            if(res.data.code==0){
                const data = res.data.data;let curDate=[];
                //药：阿替乐尔；手术：髂动脉造影
                let arr = type==5?(data.drugNames||[]):type==6?(data.operationNames||[]):(data.transfusionNames||[]),tmp=[];
                for(let i = 0;i < arr.length;i++){
                    let obj = {}
                    if(type==8){
                        obj['uniqueName'] = arr[i];
                        obj['name'] = arr[i];
                        obj['detailName'] = arr[i];
                        obj['flg'] = type;
                    }else{
                        obj['uniqueName'] = arr[i].name;
                        obj['detailName'] = arr[i].name;
                        obj['name'] = arr[i].name;
                        obj['flg'] = type;
                    }
                    tmp.push(obj)
                }
                curDate=tmp
                dispatch(searchList(curDate,val.trim()));
            }else{
                Notify.success(res.data.msg)
            }
        })
    }
};
export const getSublableList = (id,idx) => {//getById
    let baseList = store.getState();
    let sex = baseList.patInfo.message.patientSex;
    return (dispatch) => {
        axios.json('/questionInfo/getById',{
            age:baseList.patInfo.message.patientAge,
            sexType:sex == '男'?'1' : sex == '女'?'2':'3',
            id: id
        })
        .then((res)=>{
            const data = res.data.data;
            if (data.controlType == 0) {
                dispatch(resetLabel(data.questionMapping,data.uniqueName||''));
            }else if(data.controlType == 1||data.controlType == 6){
                let arr = [];
                arr[0] = data;
                dispatch(resetLabel(arr));
            }
        })
    }
};
