import {post,json} from '@utils/ajax.js';
import {SETINITDATA} from '@store/types/homePage.js';
import {SET_CURRENT} from '@store/types/currentIll';
import {SET_MAINSUIT} from '@store/types/mainSuit';
import config from '@config/index.js';
import store from '@store';
import {fullfillText} from '@common/js/func';
import {getOtherHisRecord,getInitData} from '@store/async-actions/fetchModules.js';
import {initHistoryDetails} from '@store/async-actions/patInfo';

const api = {
  getModules:'/api/icss/moduleInfo/getAll',
  clickCount:'/api/icss/buriedSomeStatistical/saveBuriedSomeStatisticals' //埋点统计
};

function modulesParseJson(data){
  let json = {},obj={},saveTextJson={},moduleName={};
  data.map((it)=>{
    moduleName[config.moduleCP[it.id]] = it.name;//添加病情变化/病程变化子模板需要使用模板名称
    obj = fullfillText(it.moduleDetailDTOList);
    json[config.moduleCP[it.id]] = obj.newArr;
    saveTextJson[config.moduleCP[it.id]] = obj.saveText;
  });
  return {json,saveTextJson,moduleName};
}

export const getInitModules= (dispatch,getStore)=>{
  const state = getStore();
  const {message} = state.patInfo;
  return json(api.getModules,{age:message.patientAge,sexType:message.sex}).then((res) => {
      if (res.data.code == '0') {
        const newObj = modulesParseJson(res.data.data);
        const initData = newObj.json;
        const initSaveText = newObj.saveTextJson;
        const initModuleName = newObj.moduleName;
        dispatch({
          type: SETINITDATA,
          data: initData
        });
        dispatch({
          type:SET_MAINSUIT,
          // data:fullfillText(initData.mainSuit).newArr,
          // addSymptom:fullfillText(initData.addSymptom).newArr,
          data:initData.mainSuit,
          addSymptom:initData.addSymptom,
          addModuleName:initModuleName.addSymptom,//模板名称
		      save:initSaveText.mainSuit
        });
        dispatch({
          type:SET_CURRENT,
          data:initData.current,
          save:initSaveText.current,
          processModule:initData.process,//病程变化
          processModuleName:initModuleName.process,//病程变化模板名称
          saveProcess:initSaveText.process,
          emptyData:initData.currentEmpty,//现病史空模板
          saveEmpty:initSaveText.currentEmpty,
        });
        dispatch(getOtherHisRecord());          //获取其他史最近记录
        initHis(dispatch)
      }else{
        initHis(dispatch)
      }
    }).catch(()=>{
        initHis(dispatch)
    });
};

export const initHis = (dispatch)=> {
    let timer = setInterval(()=>{
        let baseDate = store.getState().patInfo.message;
        //console.log(baseDate)
        if(JSON.stringify(baseDate) != '{}') {
            dispatch(initHistoryDetails(baseDate));
            clearInterval(timer)
        }
    },500)
}

// 埋点事件，点击保存时调用
export const saveClickNum = (dispatch,getState)=>{
  const state = getState();
  const patientInfo = state.patInfo.message;
  const data = state.homePage.clickData;//所有点击事件储存
  //console.log(999,state,data);
  const params = {
    "doctorId": patientInfo.doctorId,         //医生id
    "hospitalDeptId":patientInfo.hospitalDeptId ,   //部门ID
    "hospitalId": patientInfo.hospitalId,       //医院id
    "inquiryCode": patientInfo.recordId,//门诊号
    "patientId": patientInfo.patientId,        //病人ID
    "taggeds": data
  }

 return json(api.clickCount,params).then((res) => {
    if (res.data.code == '0') {
      console.log("埋点请求成功");
      // 重置clickData
    }else{
      console.log("埋点请求失败",res);
    } 
  }) 
}