import { get, post, json } from "@utils/ajax";
import { BILLING_ADVICE, SET_TIPS } from '../types/pushMessage';
import { SET_CLICK_DIAG } from '../types/diagnosticList';
import {storageLocal,getEMRParams} from '@utils/tools';

const api={
  push:'/api/icss/push/pushInner',
  textPush:'/api/icss/push/pushText'
}

//获取右侧推送信息
export const billing = (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: "5,6,7",
    diag: emrData.dis,
    lis: emrData.lis,
    other: emrData.other,
    pacs: emrData.pacs,
    sex: emrData.sex,
    vital:emrData.vital,
    symptom: emrData.current + emrData.main
  };
  storageLocal.set('emrParam',params);      //推送数据存储，用作推送前对比是否有变，有变才推送
    json(url, params).then((data) => {
        let {dis, lab, pacs} = 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||[],
        });
  }).catch((e) =>{
      console.log(e)
  }) 
};

export const getTips = (diagItem) =>{
    return (dispatch, getState) =>{
        dispatch({
            type: SET_CLICK_DIAG,
            clickDiag: diagItem
        })
        const url = '/api/icss/introduceInfo/getByQuestionId';
        const params = {
            questionId: diagItem.id,
            type: 7,
            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}`);
    }
}