import {json} from '@utils/ajax.js';
import {SET} from '@types/checkBody.js';
import {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";

const api={
  getSpreadModule:'/api/icss/questionInfo/getByIds',
  getModule:'/api/icss/questionInfo/getById',
  searchURL: '/api/icss/retrieval/getTagInfos',
  getOtherHisRecord: '/api/icss/inquiryInfo/getLastOther',
  getBigPush:'/api/icss/push/pushInner',
  saveMode:'/api/icss/doctorPageMode/saveDoctorPageModes',
};

//获取模板，多个
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} = param;
    const 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 param = {
      hospitalCode:message.hospitalCode,
      patientCode:message.patientCode,
      sign:state.typeConfig.typeConfig
    };
    json(api.getOtherHisRecord,param).then((res)=>{
      if(res.data.code=='0'){
        const data = res.data.data;
        const obj = JSON.parse(data.dataJson);
        let arr = [];
        if(!obj||!obj.other||obj.other.length==0){        //无其他史历史记录用默认模板
          //arr = state.homePage.initData.otherHis;
        }else{
          arr = obj.other;
          dispatch({
            type:SETOTHERHISTORY,
            data:arr,
            save:JSON.parse(data.otherStr)
          });
        }
      }
    });
  }
};

//查体模板数据获取
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)).newArr;
        dispatch({
          type:SET,
          data:[...arr]
        });
      }else{
        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);
  }
}