import axios from '@utils/ajax';
import store from '@store';
import {  Notify } from '@commonComp';

import { searchList,resetLabel } from '../actions/inspect';
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.inspect.shType;
    return (dispatch) => {
        axios.json('/demo/retrieval/index',{
            "age": formatFormParmas('patientAge',baseList.patInfo.patInfoData),
            "inputStr": val.trim(),
            "sex": formatFormParmas('patientSex',baseList.patInfo.patInfoData),
            "hospitalId":baseList.homePage.curHos.hosId,
            "type": type
        })
        .then((res)=>{
            if(res.data.code==0){
                const data = res.data.data;let curDate=[];
                if(type==2){
                    curDate = (data && data.lisDetailNames) ||[]
                    for (let i = 0;i < curDate.length;i++) {
                        curDate[i]['detailName'] = curDate[i]['uniqueName']
                        curDate[i]['flg'] = 0;
                    }
                }else{
                    let arr = data.lisNames||[],tmp=[];
                    for (var key in arr) {
                        let obj = {}
                        obj['uniqueName'] = arr[key];
                        obj['detailName'] = arr[key];
                        obj['value'] = '';
                        obj['otherValue']='';
                        obj['name'] = arr[key];
                        obj['flg'] = 1;
                        tmp.push(obj)
                    }
                    curDate=tmp
                }
                dispatch(searchList(curDate,val.trim()));
            }else{
                Notify.error(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));
            }
        })
    }
};
