import { get, post, json } from "@utils/ajax";
import { BILLING_ADVICE, SET_TIPS, SET_TIPS_DETAILS ,SET_CHRONIC_TABLELIST,SET_SCALE_INFO,SET_CHRONIC_PUSHS,SHOW_TABLE_LIST, SHOW_TIPS_DETAILS,TOGGLE_CHRONIC_INFO} from '@store/types/pushMessage';
import { SET_DRUG_INFO, SHOW_DRUG_INFO } from '@store/types/treat';
import { SET_CLICK_DIAG } from '../types/diagnosticList';
import {storageLocal,getEMRParams} from '@utils/tools';
import {getBigPush} from '@utils/utils';
import {SET_IMPORT_CHECKBODY_LABEL,PRESET} from "../types/checkBody";
import dataLis from '@components/EmergencyProcedure/emergency';
import  Notify from '@commonComp/Notify';
import {pregetCheckbodyData} from '@store/async-actions/fetchModules';
import {tabChange} from '@store/actions/tabTemplate';
const api={
  getTableList:'/scale/getList', //获取量表列表
  getTableInfo:'/scale/getContent', //获取量表明细
  getConceptDetail:'/conceptDetail/getConceptDetail', //获取静态提示信息
}

//获取右侧推送信息
export const billing = (mdata,boxMark) => {
 return (dispatch, getState) =>{
  const state = getState();
   const checkBody = state.checkBody.data;
   let diagMain = state.diagnosticList.diagnosticList.length>0&&state.diagnosticList.diagnosticList[0]
   let getCheck = boxMark==2&&!(checkBody&&checkBody.length>0&&!checkBody[0].full);
  const emrData = getEMRParams();
  //mdata为主诉无标签直接输入时的内容，symptom参数传主诉+现病史文本
  const mainData =  mdata?(emrData.current + mdata):(emrData.current + emrData.main);
  const feaType = getCheck?"42,41,5,6,7":"42,5,6,7";    //41只返回查体模板，4只返回查体高亮
   getBigPush(feaType,mainData,true).then((data) => {
        let {dis, lab, pacs,vitalIds,moduleVital} = data.data.data||{};
        lab = lab||[];
        pacs = pacs||[];
        // console.log('推送数据', data.data.data);
        let vigilant=[], //警惕
             doubt=[],    //疑似诊断
            possible = [],  //可能诊断
            determine=[], //确诊
            setPushEmergency={}, //急诊
            setPushEmergencyIdx='',//急诊对应本地数据
            likely=[];//鉴别诊断
            doubt = dis&&dis['拟诊'],
            possible = dis&&dis['可能诊断'];
            vigilant = dis&&dis['警惕'];
            determine = dis&&dis['确诊']; //确诊
            likely = dis&&dis['鉴别诊断'];
        //急诊显示规则，主诊断有急诊优先显示，没有则显示推送的，推送也没有就不显示
        if(dis&&dis['页面急诊']&&(dis['页面急诊'].length>0)&&(diagMain&&dis['页面急诊'][0].conceptId == diagMain.conceptId)){
          setPushEmergency=dis['页面急诊'][0]
          let name = dis['页面急诊'][0].name || ''
          for(let i = 0;i < dataLis.length;i++){
            if(name == dataLis[i].emergencyillname){
              setPushEmergencyIdx=i
            }
          }
        }else if(dis&&dis['急诊']&&dis['急诊'].length>0){
          setPushEmergency=dis['急诊'][0]
          let name = dis['急诊'][0].name || ''
          for(let i = 0;i < dataLis.length;i++){
            if(name == dataLis[i].emergencyillname){
              setPushEmergencyIdx=i
            }
          }
        }
        if(lab) {
            for(let i = 0; i < lab.length; i++) {
                lab[i].checked = false
            }
        }
        if(pacs) {
            for(let i = 0; i < pacs.length; i++) {
                pacs[i].checked = false
            }
        }

        dispatch({
          type:TOGGLE_CHRONIC_INFO,
          flag:false
        });
        dispatch({
            type: BILLING_ADVICE,
            determine: determine || [],
            doubt: doubt||[],
            possible: possible||[],
            vigilant: vigilant||[],
            likely:likely||[],
            lab: lab||[],
            pacs: pacs||[],
            setPushEmergency: setPushEmergency||[],
            setPushEmergencyIdx:setPushEmergencyIdx
        });
        dispatch({
          type:PRESET,
          data:moduleVital
        });
        //查体高亮标签
        dispatch({
          type:SET_IMPORT_CHECKBODY_LABEL,
          labels:vitalIds
        })
  }).catch((e) =>{
      console.log(e)
  });
   //单独调指标
   getBigPush('22',mainData).then((data) => {
     let {medicalIndications} = data.data.data||{};
     //慢病推送模块数据
     dispatch({
       type:SET_CHRONIC_PUSHS,
       data:medicalIndications
     });
   }).catch((e) =>{
     console.log(e)
   });

   //调查体,现病史触发推送且查体无数据时获取查体模板
   /*const checkBody = state.checkBody.data;
   if(boxMark==2&&!(checkBody&&checkBody.length>0&&!checkBody[0].full)){
     dispatch(pregetCheckbodyData());
   }*/
 }
};

//获取提示信息（静态信息） 
export const getConceptDetail = (item,noTips,noDetails,noNotify) => {
    return(dispatch, getState) => {
        const params ={
            name: item.name,
            position: item.position,
            type: item.type
        }
        if(item.position == '1') { //点击i要展示详情和提示信息
          params.position = 0
        }

        json(api.getConceptDetail, params).then((res) => {
            if(res.data.code == '0'&& res.data.data.details && res.data.data.details.length > 0) {
              dispatch(tabChange(0));     //tab跳回推送
              if(item.position == '1') {  //右侧提示信息
                    dispatch({
                        type: SET_CLICK_DIAG,
                        clickDiag: item
                    })
                    if(item.type == '5' || item.type == '6' || item.type == '12') {
                        dispatch({
                            type: SET_TIPS,
                            tips: res.data.data,
                            tmpFlg:'midPart',
                            showPartName: item.showName
                        })
                    } else {
                        dispatch({
                            type: SET_TIPS,
                            tips: res.data.data,
                        });
                        dispatch({
                          type:TOGGLE_CHRONIC_INFO,
                          flag:true
                        });
                    }
                    document.getElementById("tipsMsg").scrollIntoView(true)
                    //点击i同时打开详情弹窗 点击诊断名称不打开弹窗
                    if(!noDetails) {
                      dispatch({
                        type: SET_TIPS_DETAILS,
                        tipsDetails: res.data.data,
                        showAllName: item.showName
                      })
                      dispatch({
                        type: SHOW_TIPS_DETAILS
                      })
                    }
                    
                } else if(item.position == '5' || item.position == '6'){    //药品或不良反应说明
                    dispatch({
                        type: SET_DRUG_INFO,
                        instroduce: res.data.data.details,
                        name: item.name,
                        tagType: item.type
                    })
                    dispatch({
                        type: SHOW_DRUG_INFO
                    })
                }
            } else {
              if(!noNotify) {
                Notify.info('提示信息未维护')
              }
            }
            
        })
    }
}

export const getAllConceptDetail = (item) => {

    return(dispatch, getState) => {
        const state = getState();
      let params = {
            type: item.type,
            name: item.name,
            position:item.position
      };
      if (item.type == 5 || item.type == 51) {
        params.type = 12;
        params.position = 0;
        params.name = item.uname;
      }
        json(api.getConceptDetail, params)
        .then((res)=>{
            if(res.data.code == '0') {
              //医学知识中打开弹窗，无详细内容显示未维护
              if(res.data.data&&res.data.data.details.length>0){
                dispatch({
                  type: SHOW_TIPS_DETAILS
                })
              }else{
                Notify.info('提示信息未维护');
              }
              dispatch({
                type: SET_TIPS_DETAILS,
                tipsDetails: res.data.data,
                showAllName: item.showName
              })
            } else {
                Notify.info('提示信息未维护');
            }
               
        }).catch((e) => {
            console.log(e)
        })
    }
}

// 量表列表
export const getTableList = (name)=>{
    return (dispatch,getState)=>{
        json(api.getTableList, {disName:name})
        .then((res)=>{
            if(res.data.code==0 && res.data.data && res.data.data.length>0){
                dispatch({
                    type: SET_CHRONIC_TABLELIST,
                    data: res.data.data
                })
                dispatch({
                    type:SHOW_TABLE_LIST,
                    name:'showList',
                    value:true
                })
            }else{
                Notify.info(res.data.msg||'无关联量表')
            }
        }).catch((e) => {
            console.log(e)
        })
    }
}

// 量表明细
export const getScaleInfo = (it)=>{
    return (dispatch,getState)=>{
        const emrData = getEMRParams();
          const params = {
            age: emrData.age,
            sex: emrData.sex,
            featureType: "21",
            diag: emrData.dis,
            lis: emrData.lis,
            scaleId:it.conceptId,
            scaleName:it.name,
            other: emrData.other,
            pacs: emrData.pacs,
            vital:emrData.vital,
            symptom: emrData.current + emrData.main,
            // indications:'' //指标结果
          };
        json(api.getTableInfo, params)
        .then((res)=>{
            const result = res.data;
            // if(result.code==0 && result.data.scale && result.data.scale.length>0){
            if(result.code==0 && result.data.length>0){
                dispatch({
                    type: SET_SCALE_INFO,
                    name: it.name,
                    data: result.data,
                    id:it.conceptId
                })
                dispatch({
                    type:SHOW_TABLE_LIST,
                    name:'showTable',
                    value:true,
                    item:Object.assign({},it)
                })
            }else{
                Notify.info(result.msg||'暂无量表信息')
            }
        }).catch((e) => {
            console.log(e)
        })
    }
}