import {json} from '@utils/ajax.js';
import {SET} from '@types/checkBody.js';
import {fullfillText,_fullfillText} from '@common/js/func';
import {SETDATA} from '@store/types/otherHistory';
import store from '@store';
import {getEMRParams} from '@utils/tools.js';
import {Notify} from '@commonComp';
import {SETOTHERHISTORY} from "../types/homePage";
import config from "@config/index";

const api={
  getSpreadModule:'/questionInfo/getByIds',
  getModule:'/questionInfo/getById',
  searchURL: '/retrieval/getTagInfos',
  getOtherHisRecord: '/inquiryInfo/getLastOther',
  getBigPush:'/push/pushInner',
  saveMode:'/doctorPageMode/saveDoctorPageModes',
  getSymptomFeature:'/feature/getSymptomFeature',
  getAssess:'/evaluationModuleMapping/getEvaluationModules',    //获取管理评估-慢病
  getIndexData:'/indexData/getIndexDatas',    //获取指标数据-图表-慢病
};

export const getFeature = (item)=>{
  const datas = json(api.getSymptomFeature+'?text='+item,{});
  return datas;
}

//获取模板，多个
export const getModules = (ids)=>{
  const {patInfo} = store.getState();
  const param = {
    "age": patInfo.message.patientAge,
    "ids": ids,
    "sexType": patInfo.message.sex
  };
  const modules = json(api.getSpreadModule,param);
  return modules;
};
//获取单个模板
export const getModule = (id)=>{
  const {patInfo} = store.getState();
  const param = {
    "age": patInfo.message.patientAge,
    "id": id,
    "sexType": patInfo.message.sex
  };
  return json(api.getModule,param);
};
//搜索接口
export const getSearch = (param)=>{
  const {patInfo} = store.getState();
    const {inpStr,boxMark,mainIds,itemType} = param;
    let params = {};
    if(itemType==0){//主诉，仅限症状
      params = {
        "age": patInfo.message.patientAge,
        "inputIds":mainIds&&mainIds.length>0?mainIds:[],//主诉去重
        "inputStr": inpStr.trim(),
        "sexType": patInfo.message.sex,
        "type": boxMark, //1为主诉现病史
        "itemType":itemType
      };
    }else{
      params = {
        "age": patInfo.message.patientAge,
        "inputIds":mainIds&&mainIds.length>0?mainIds:[],//主诉去重
        "inputStr": inpStr.trim(),
        // "inputStr": inpStr,
        "sexType": patInfo.message.sex,
        "type": boxMark //1为搜症状
      };
    }
    return json(api.searchURL,params);
};

//其他史最近记录获取
export const getOtherHisRecord = ()=>{
  return (dispatch,getStore)=>{
    const state = getStore();
    const {message} =state.patInfo;
    const mode = state.typeConfig.typeConfig;
    const param = {
      hospitalCode:message.hospitalCode,
      patientCode:message.patientCode,
      sign:mode
    };
    json(api.getOtherHisRecord,param).then((res)=>{
      if(res.data.code=='0'){
        const data = res.data.data;
        const obj = JSON.parse(data.dataJson||'{}');
        const objStr = JSON.parse(data.otherStr||'[]');
        let arr = [];//console.log(obj)
        if((!obj||!obj.other||obj.other.length==0)&&!objStr[0]){        //无其他史历史记录用默认模板
          if(mode=='1'){      //文本模式
            dispatch({
              type:SETOTHERHISTORY,
              data:[],
              selecteds:[],
              save:[]
            });
          }

          //console.log('其他史最近数据无')
          //arr = state.homePage.initData.otherHis;
        }else{
          arr = obj.other;
          if(mode =='1'){     //文本模式
            dispatch({
              type:SETOTHERHISTORY,
              data:[],
              selecteds:[],
              save:[objStr[0]]
            });
          }else{
            dispatch({
              type:SETOTHERHISTORY,
              data:arr,
              period:{
                yjs_1: obj.yjs_1,
                yjs_2: obj.yjs_2,
                yjs_3: obj.yjs_3,
                yjs_4: obj.yjs_4,
              },
              selecteds:obj.otherHistorySelecteds,
              save:objStr
            });
          }
        }
      }
    });
  }
};

//查体模板数据获取
export function getInitData(){
  return (dispatch)=>{
    const emrData = getEMRParams();
    const param = {
      age: emrData.age,
      featureType: "1,4,7",
      diag: emrData.dis,
      lis: emrData.lis,
      other: emrData.other,
      pacs: emrData.pacs,
      sex: emrData.sex,
      vital:emrData.vital,
      symptom: emrData.current+emrData.main
    };
    json(api.getBigPush,param).then((res)=>{
      if(+res.data.code === 0){
        const data = res.data.data&&res.data.data.vital;
        //const str = JSON.stringify(data);
        //const arr = fullfillText(JSON.parse(str),false,false,false).newArr;
        dispatch({
          type:SET,
          data:[...data],
          isEmpty:false
        });
      }else{
        const block = Object.assign(JSON.parse(config.textLabel),{full:true});      //无数据时保留一个自由文本标签可输入
        dispatch({
          type:SET,
          data:[block],
          isEmpty:true
        });
        Notify.error(res.data.msg);
      }
    });
  }
}

//保存切换模式
export function saveMode(mode){
  return (dispatch,getStore)=>{
    const {patInfo} = getStore();
    const param = {
      doctorId:patInfo.message.doctorId,
      modeClassify:0,
      modeValue:mode
    };
    return json(api.saveMode,param);
  }
}

//慢病-获取管理评估
export function getAssessData(){
  return (dispatch,getStore)=>{
    const {patInfo} = getStore();
    const param = {
      diseaseId:21773,
      disType:1,
      diag:'糖尿病',
      featureType:11,
      lis:[],
      sex:patInfo.message.sex,
      age:patInfo.message.patientAge
    };
    return json(api.getAssess,param);
  }
}

export function getIndexData(){
  return (dispatch,getStore)=>{
    const {patInfo} = getStore();
    const param = {
      diseaseId:21773,
      startTime:'2017-08-01',
      endTime:'2019-04-10',
      patientId:1001,//patInfo.message.patientId,
    };
    return json(api.getIndexData,param);
  }
}