require('./../css/reset.css');
require('./../css/indexHorizontal.less');
// require('./../css/popup.css');
// require('./popupEdit.js');
var Promise = require("bluebird");
let warningImg = require('./../images/warings.png')
let diagImg = require('./../images/yszd.png')
let symptomImg = require('./../images/inquire.png')
let physiExamImg = require('./../images/tgjc.png')
let labAndPacsImg = require('./../images/inspect.png')
let conditTipsImg = require('./../images/tips.png')
let treatImg = require('./../images/zhiliao.png')

const $ = require("jquery");
const { post,config,getUrlArgObject,openNewWin, isIe8 } = require('./promise.js');
const { transConf } = require('./util.js');
const { renderRecommendInfo, renderMultRecommendInfo, renderTreat, renderRecommendConditTips, renderTipsInfo ,renderKnowledgeInfo,renderScaleInfo} = require('./indexHorizontalDom.js');
const is8Ie = isIe8() //判断是否为IE8
if(is8Ie) {
  let reg = /undefined/g
  warningImg = warningImg.replace(reg, '')
  diagImg = diagImg.replace(reg, '')
  symptomImg = symptomImg.replace(reg, '')
  physiExamImg = physiExamImg.replace(reg, '')
  labAndPacsImg = labAndPacsImg.replace(reg, '')
  conditTipsImg = conditTipsImg.replace(reg, '')
  treatImg = treatImg.replace(reg, '')
}

let mrId = getUrlArgObject('mrId') 
let showTab = getUrlArgObject('showTab') || '0';
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
      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
  getPageInfo();
}

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]) =>{
    const configArr = res1&&res1.data&&res1.data.data || []
    const pushInfo = res2.data || {}
    const treatInfo = res3.data || {}
    const {confArr, showLabsNum, showPacsNum, labsAndPacMode} = transConf(configArr)
    const tipsInfo = res4.data || {}
    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;//计算的需要的相关数据
    let tipsNum = 0;
    for(let i = 0; i < confArr.length; i++) {
      switch(confArr[i].code) {
        case "inquiry_show":  //问诊症状
          symptomRecommend.length > 0 && renderRecommendInfo(confArr[i].showNum, symptomImg, 'symptomRecommend', '问诊症状', symptomRecommend, false)
          break; 
        case "health_show": //体格检查
          physiExamRecommend.length > 0 && renderRecommendInfo(confArr[i].showNum, physiExamImg, 'physiExamRecommend', '体格检查', physiExamRecommend, false)
          break;
        case "illness_show": //病情提示
          medicalIndications.length > 0 && renderRecommendConditTips(conditTipsImg, 'conditTips','病情提示', medicalIndications || [])
          break;
        case "vigilant_show": //警惕诊断
          diagWaring.length > 0 && renderRecommendInfo(confArr[i].showNum, warningImg, 'warning', '警惕诊断', diagWaring, true,7,2)
          break;
        case "lispacs_show": //检验检查
          (labRecommend.length > 0 || pacsRecommend.length >0) && renderMultRecommendInfo(labsAndPacMode,labAndPacsImg, 'labAndPacsRecommend', '检验检查', labAndPacsRecommend, false)
          break;
        case "cure_show": //治疗方案
          if(JSON.stringify(treatResult)!='{}'&&(treatResult.commonTreatment||treatResult.treatmentPlan&&treatResult.treatmentPlan.length > 0)) {
            tipsNum++;
            renderTreat(treatImg, 'treatRecommend','治疗方案',treatResult)
          }
          break;
        case "diagnose_show": //推荐诊断
          diagRecommend.length > 0 && renderRecommendInfo(confArr[i].showNum, diagImg, 'diagRecommend', '推荐诊断', diagRecommend, true,7,2)
          break;
        case "tip_show": //提示信息
          if(tipsResult.length > 0 ) {
            tipsNum++;
            renderTipsInfo('', 'tipsInfo','提示信息',tipsResult)
            moreInfo()
          }
          break;
        case "knowledge_show": //医学知识
          tipsNum++;
          renderKnowledgeInfo('','knowledgeInfo','医学知识','点击搜索医学知识')
          break;
        case "scale_show": //全部量表
          tipsNum++;
          renderScaleInfo('','scaleInfo','全部量表','点击搜索全部量表')
          break;
        default:
          break;
      } 
    }
    function bindTab() {
      $('.infoTabBox .infoTab').eq(showTab).addClass('activeTab').siblings().removeClass('activeTab')
      $('.informationBox').eq(showTab).siblings(".informationBox").css({"display":"none"})
      // if(showTab == '1') {
      //     $('.infoTabBox .tipsTab').addClass('activeTab')
      //     $('.infomationBox .treatRecommend').css("display","none")
      // } else {
      //   $('.infoTabBox .treatTab').addClass('activeTab')
      //   $('.infomationBox .tipsInfo').css("display","none")
      //   $('.infomationBox .staticWin').css("display","none")
      // }

      $('.infomationBox .infoTab').click(function() {
        $(this).addClass('activeTab').siblings().removeClass('activeTab')
        $('.informationBox').css("display","none")
        const boxNode = $(this).attr('data-box')
        $('.'+boxNode).css("display","block")
      })
    
    }
    if(tipsNum > 1) {
      bindTab();
    }else if(tipsNum == 0){
      $('.push').css({
        "width":"95%",
        "border-right":"0 none"
      })
      $('.itemAllBox').css({"width":"98%"})
    }
    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)}`)
      })
    }
    
    const pushHei = $('.push').height()
    if(pushHei == 0) {
      $('.infomationBox').css({"width":"90%"})
    }
  
    
  }).catch(function (err) {
    console.log(err);
  })

}
function getVersion() {
  post(config.getVersion, '').then((res) => {
    const data = res.data.data;
    const name = data.name;
    const ver = localStorage.getItem('versionTime-v');
    const time = data.refreshTime;
    if(!ver||ver===time+"=new"||time!==ver.replace("=new","")){       //判断版本是否更新
      $(".version").addClass('new-icon');
      localStorage.setItem('versionTime-v',time+"=new");     //保存版本更新时间
    }else{
      $(".version").removeClass('new-icon');
    }
    $(".version span").html(name);
  })
}
$(function(){
  getVersion();
  //打开版本信息
  $(".disclaimer .version").on("click",function() {
    const ver = localStorage.getItem('versionTime-v');
    $(".version").removeClass('new-icon');
    localStorage.setItem('versionTime-v',ver.replace("=new",""));
    openNewWin("version.html");
  });
 })
