import axios from '@utils/ajax';
import store from '@store';

import { searchList,resetLabel } from '../actions/inspect';
import { SET_TIPS } from '../types/pushMessage';

export const getSearchList = (val) => {
    if(val.trim() == ''){
        const data = [];
        return (dispatch) => dispatch(searchList(data,''));
    }
    let baseList = store.getState();
    let sex = baseList.patInfo.message.patientSex;
    return (dispatch) => {
        axios.json('/retrieval/getTagInfos',{
            type:'5',
            age:baseList.patInfo.message.patientAge,
            inputStr:val,
            sexType:sex == '男'?'1' : sex == '女'?'2':'3'
        })
        .then((res)=>{
            const data = res.data.data;
            dispatch(searchList(data,val.trim()));
        })
    }
};
export const getSublableList = (id,idx) => {
    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,idx));
            }else if(data.controlType == 1){
                let arr = [];
                arr[0] = data;
                dispatch(resetLabel(arr,idx));
            }else if(data.controlType == 6) {
                let arr =[];
                arr[0] = data;
                dispatch(resetLabel(arr,idx));
            } 
        })
    }
};

export const getInstroduce = (id,name,type)=>{    //获取化验的提示信息
  let url = '',param={};
  if(type == 'excel'){
    url = '/introduceInfo/getByQuestionName'
    param = {
      type: 5,
      tagName:name?name:'',
      position: 1
    }
  }else{
    url = '/introduceInfo/getByQuestionId'
    param = {
      questionId: id?id:'',
      type: 5,
      tagName:name?name:'',
      position: 1
    }
  }
  return (dispatch, getState) =>{
    axios.json(url, param)
    .then((data)=>{
        dispatch({
          type: SET_TIPS,
          tips: data.data.data,
          tmpFlg:'midPart'
        })          
    }).catch((e) => {
        console.log(e)
    })
  }
}