require('./../css/reset.css');
require('./../css/homeStatic.less');
require('./../css/popup.css');
// require('./popupEdit.js');
var Promise = require("bluebird");


//tipsMode: 提示信息模式  1 为单个， 2 为多个(只有传2时为多个)

const $ = require("jquery");
const { post,config,getUrlArgObject,openNewWin } = require('./promise.js');
const { transConf } = require('./util.js');
const { renderRecommendInfo, renderMultRecommendInfo, renderTreat, renderRecommendConditTips, renderTipsInfo ,renderKnowledgeInfo,renderScaleInfo} = require('./indexVertDom.js');


let mrId = getUrlArgObject('mrId') 
const tipsMode = getUrlArgObject('tipsMode') || 1

let msg;
if(mrId) {
 post(config.getMr,{mrId:mrId}).then((res) => {
    const data = res.data
    if(data.code == 0) {
      msg = Object.assign(data.data, {plan:getUrlArgObject('plan') || '0',})
      window.msg = msg
      window.pushMessage = {} 
      getPageInfo();
    }
  })
} else {
  msg = {
    age: getUrlArgObject('age'),
    sex: getUrlArgObject('sex') == '男'?1:(getUrlArgObject('sex') == '女'?2:(getUrlArgObject('sex') == '通用'?3:getUrlArgObject('sex'))),
    symptom: getUrlArgObject('symptomJson'),
    vital: getUrlArgObject('vitalJson'),
    lis: getUrlArgObject('lisJson')? JSON.parse(getUrlArgObject('lisJson')) : [],
    pacs: getUrlArgObject('pacsJson'),
    diag: getUrlArgObject('diagJson'),
    diseaseName: getUrlArgObject('diseaseName'),
    other: getUrlArgObject('otherJson') + getUrlArgObject('pastJson'),
    pasts: getUrlArgObject('otherJson') + getUrlArgObject('pastJson'),
    hosCode:getUrlArgObject('hospitalCode') || '',
    plan:getUrlArgObject('plan') || '0',
  }
  window.msg = msg
  window.pushMessage = {} 
  getPageInfo();
}


// var msg = {
//     age: getUrlArgObject('age'),
//     sex: getUrlArgObject('sex') == '男'?1:(getUrlArgObject('sex') == '女'?2:(getUrlArgObject('sex') == '通用'?3:getUrlArgObject('sex'))),
//     symptom: getUrlArgObject('symptomJson'),
//     vital: getUrlArgObject('vitalJson'),
//     lis: getUrlArgObject('lisJson')? JSON.parse(getUrlArgObject('lisJson')) : [],
//     pacs: getUrlArgObject('pacsJson'),
//     diag: getUrlArgObject('diagJson'),
//     diseaseName: getUrlArgObject('diseaseName'),
//     other: getUrlArgObject('otherJson') + getUrlArgObject('pastJson'),
//     hosCode:getUrlArgObject('hospitalCode') || '',
//     plan:getUrlArgObject('plan') || '0',
// }//推送相关信息
// //   http://localhost:8082/indexVert.html?age=28&diseaseId=280&lisJson=&diagJson=%E6%80%A5%E6%80%A7%E8%83%B0%E8%85%BA%E7%82%8E&pastJson=%E6%97%A0%E5%BF%83%E8%84%8F%E7%97%85&otherJson=%E6%97%A0%E9%AB%98%E8%A1%80%E5%8E%8B&pacsJson=%E8%85%B9%E9%83%A8B%E8%B6%85:%E6%97%A0%E5%BC%82%E5%B8%B8,%20%E6%8A%A5%E5%91%8A%E6%97%A5%E6%9C%9F:2019-05-07&sex=%E7%94%B7&symptomJson=%E6%81%B6%E5%BF%83%E5%91%95%E5%90%90%E8%85%B0%E7%97%9B&vitalJson=%E8%A1%80%E5%8E%8B%E5%B7%A6%E4%B8%8A%E8%82%A213/12mmHg
// window.msg = msg
// window.pushMessage = {}
function getConf() {
  return post(config.getSysSetInfoDatas,{hospitalCode: msg.hosCode,plan:msg.plan})
}

function getPushInfo() {
  return  post(config.pushInner,Object.assign({featureType: '1,4,5,6,7,22',ruleType:config.ruleTypeMap['22']},msg))
  
}

function getTreatment() {
  return  post(config.pushInner,Object.assign({featureType: '8',ruleType:config.ruleTypeMap['8']},msg))
}

function getTipsInfo() {
  let tipsInfoList =  getUrlArgObject('tipsInfoList')&&JSON.parse(getUrlArgObject('tipsInfoList')) || []
  if(tipsMode == 2) {
    return post(config.informationMore, {conceptIntorduces:tipsInfoList})
  } else {
    return post(config.information,{name:getUrlArgObject('tipsName'),type:getUrlArgObject('tipsType'), position: 1})
  }
}


function getPageInfo() {

  Promise.all([getConf(),getPushInfo(),getTreatment(),getTipsInfo()]).then(([res1, res2, res3, res4]) =>{
    // console.log({res1, res2, res3, res4})
    const configArr = res1&&res1.data&&res1.data.data || []
    const pushInfo = res2&&res2.data || {}
    const treatInfo = res3&&res3.data &&res3.data || {}
    const tipsInfo = res4&&res4.data || {}
    const {confArr, showLabsNum, showPacsNum, labsAndPacMode} = transConf(configArr)
    const pushResult = pushInfo.data || {};
    const treatResult = treatInfo&&treatInfo.data&&treatInfo.data.treat || {}
    let tipsResult = []
    if(tipsInfo.data) {
      if(tipsMode == '2') {
          tipsResult = tipsInfo.data
      } else {
          tipsResult.push(tipsInfo.data)
      }
    }

    const diagPossible =  pushResult.dis&&pushResult.dis['可能诊断'] || []//可能
    const diagDoubt = pushResult.dis&&pushResult.dis['拟诊'] || [] // 疑似
    const diagDeter = pushResult.dis&&pushResult.dis['确诊'] || []//确诊
    const diagWaring = pushResult.dis&&pushResult.dis['警惕'] || [] // 推荐警惕诊断
    const symptomRecommend = pushResult.symptom || []  //推荐问诊症状
    const physiExamRecommend  = pushResult.vital || [] //推荐体格检查
    const diagRecommend = diagDeter.concat(diagDoubt, diagPossible) //推荐诊断
    const labRecommend = pushResult.lab || []
    const pacsRecommend = pushResult.pacs || []
    const medicalIndications = pushResult.medicalIndications || []
    const labAndPacsRecommend = [
      {
          title: '化验',
          className: 'labRecommend',
          data:  labRecommend,
          showNum: showLabsNum
      },
      {
          title: '辅检',
          className: 'pacsRecommend',
          data:  pacsRecommend,
          showNum: showPacsNum
      }
    ]
    window.pushMessage = medicalIndications;//计算的需要的相关数据

    for(let i = 0; i < confArr.length; i++) {

      switch(confArr[i].code) {
        case "inquiry_show":  //问诊症状
        symptomRecommend.length > 0 && renderRecommendInfo(confArr[i].showNum,'symptomRecommend', '问诊症状', symptomRecommend, false)
          break; 
        case "health_show": //体格检查
        physiExamRecommend.length > 0&&  renderRecommendInfo(confArr[i].showNum,'physiExamRecommend', '体格检查', physiExamRecommend, false)
          break;
        case "illness_show": //病情提示
        medicalIndications.length > 0&& renderRecommendConditTips('conditTips','病情提示', medicalIndications || [])
          break;
        case "vigilant_show": //警惕诊断
          
        diagWaring.length > 0&& renderRecommendInfo(confArr[i].showNum,'warning', '警惕诊断', diagWaring, true,7,2)
          break;
        case "lispacs_show": //检验检查
          (labRecommend.length > 0 || pacsRecommend.length >0) && renderMultRecommendInfo('labAndPacsRecommend', '检验检查', labAndPacsRecommend, false)
          break;
        case "cure_show": //治疗方案
          JSON.stringify(treatResult)!='{}'&&(treatResult.commonTreatment||treatResult.treatmentPlan&&treatResult.treatmentPlan.length > 0)&&renderTreat('treatRecommend','治疗方案',treatResult)
          break;
        case "diagnose_show": //推荐诊断
        diagRecommend.length > 0 && renderRecommendInfo(confArr[i].showNum,'diagRecommend', '推荐诊断', diagRecommend, true,7,2)
          break;
        case "tip_show": //提示信息
          tipsResult.length > 0 && renderTipsInfo('tipsInfo','提示信息',tipsResult)
          moreInfo()
          break;
        case "knowledge_show": //医学知识
          renderKnowledgeInfo('knowledgeInfo','医学知识','点击搜索医学知识')
          break;
        case "scale_show": //全部量表
          renderScaleInfo('scaleInfo','全部量表','点击搜索全部量表')
          break;
        default:
          break
      } 
    }
  }).catch(function (err) {
    console.log(err);
  })


}
function moreInfo() {
  $('.moreInfo').click(function(){
    const name = $(this).attr('data-name')
    const type = $(this).attr('data-type')
    openNewWin(`information.html?type=${encodeURIComponent(type)}&position=0&name=${encodeURIComponent(name)}`)
  })
}

function getVersion() {
  post(config.getVersion, '').then((res) => {
    const data = res.data.data;
    const ver = localStorage.getItem('versionTime');
    const time = data.refreshTime;
    $(".version-tip a").text(data.name);
    if(!ver||ver===time+"=new"||time!==ver.replace("=new","")){       //判断版本是否更新
      $(".version-tip").addClass('new-icon');
      localStorage.setItem('versionTime',time+"=new");     //保存版本更新时间
    }else{
      $(".version-tip").removeClass('new-icon');
    }
  })
}
  function getDisclaimer() {
    post(config.disclaimer, '').then((res) => {
      const data = res.data.data.find(item => item.disclaimerCode == '2');
      $(".responsibility").html(data.description);
    })
  }


$(function(){
  getDisclaimer(); //获取免责声明
  getVersion();   //获取版本信息
  $(".disclaimerInfo").on("click", function(){
    openNewWin("disclaimer.html")
  });
  $(".version-tip a").on("click", function(){
    const ver = localStorage.getItem('versionTime');
    $(".version-tip").removeClass('new-icon');
    localStorage.setItem('versionTime',ver.replace("=new",""));
    openNewWin("version.html");
  });
});
