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} from '../types/pushMessage';
import { SET_CLICK_DIAG } from '../types/diagnosticList';
import {storageLocal,getEMRParams} from '@utils/tools';
import {SET_IMPORT_CHECKBODY_LABEL} from "../types/checkBody";
import { Notify} from '@commonComp';

const api={
  push:'/push/pushInner',
  textPush:'/push/pushText',
  getTableList:'/scale/getList', //获取量表列表
}

//获取右侧推送信息
export const billing = (mdata) => {
 return (dispatch, getState) =>{
  const state = getState();
  let url = api.push;
  if(+state.typeConfig.typeConfig===1){
    url=api.textPush;
  }
  const emrData = getEMRParams();
  const params = {
    age: emrData.age,
    featureType: "4,5,6,7,22",
    // featureType: "22",
    diag: emrData.dis,
    lis: emrData.lis,
    other: emrData.other,
    pacs: emrData.pacs,
    sex: emrData.sex,
    vital:emrData.vital,
    symptom: mdata?(emrData.current + mdata):(emrData.current + emrData.main)
  };
  storageLocal.set('emrParam',params);      //推送数据存储，用作推送前对比是否有变，有变才推送
    json(url, params).then((data) => {
        let {dis, lab, pacs,markedVitalIds,medicalIndications} = data.data.data||{};
        lab = lab||[];
        pacs = pacs||[];
        // console.log('推送数据', data.data.data);
        let vigilant=[], //警惕
             doubt=[],    //疑似诊断
            possible = [],  //可能诊断
            determine=[]; //确诊
            doubt = dis&&dis['疑似诊断'],
            possible = dis&&dis['可能诊断'];
            vigilant = dis&&dis['警惕'];
            determine = dis&&dis['确诊']; //确诊
        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: BILLING_ADVICE,
            determine: determine || [],
            doubt: doubt||[],
            possible: possible||[],
            vigilant: vigilant||[],
            lab: lab||[],
            pacs: pacs||[],
        });
        //慢病推送模块数据
        dispatch({
          type:SET_CHRONIC_PUSHS,
          data:medicalIndications
        });
        //查体高亮标签
        dispatch({
          type:SET_IMPORT_CHECKBODY_LABEL,
          labels:markedVitalIds
        })
  }).catch((e) =>{
      console.log(e)
  }) 
 }
};


export const getTips = (diagItem) =>{
    return (dispatch, getState) =>{
        dispatch({
            type: SET_CLICK_DIAG,
            clickDiag: diagItem
        })
        const url = '/introduceInfo/getByQuestionId';
        const params = {
            questionId: diagItem.id,
            type: diagItem.type,
            position: 1
        }
        json(url, params)
        .then((data)=>{
                dispatch({
                    type: SET_TIPS,
                    tips: data.data.data
                })
        }).catch((e) => {
            console.log(e)
        })

    }
}

export const getTipsDetails = () => {
    return(dispatch, getState) => {
        const state = getState();
        // 诊断指南新窗口展示
        // window.open(`/static/pages/information.html?type=7&&questionId=${state.diagnosticList.clickDiag.id}`);

        //弹窗显示
        const url ='/introduceInfo/getByQuestionId'
        const params = {
            type: state.diagnosticList.clickDiag.type,
            questionId: state.diagnosticList.clickDiag.id,
            position:2
        }
        json(url, params)
        .then((data)=>{
                dispatch({
                    type: SET_TIPS_DETAILS,
                    tipsDetails: data.data.data
                })
        }).catch((e) => {
            console.log(e)
        })
    }
}

// 量表列表
export const getTableList = (id)=>{
    return (dispatch,getState)=>{
        json(api.getTableList, {disId:id})
        .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.id,
            scaleName:it.name,
            other: emrData.other,
            pacs: emrData.pacs,
            vital:emrData.vital,
            symptom: emrData.current + emrData.main,
            // indications:'' //指标结果
          };
        json(api.push, params)
        .then((res)=>{
            const result = res.data;
            if(result.code==0 && result.data.scale && result.data.scale.length>0){
                dispatch({
                    type: SET_SCALE_INFO,
                    data: result.data.scale,
                    id:it.id
                })
                dispatch({
                    type:SHOW_TABLE_LIST,
                    name:'showTable',
                    value:true
                })
            }else{
                Notify.info(result.msg||'暂无量表信息')
            }
        }).catch((e) => {
            console.log(e)
        })
    }
}