const $ = require("jquery");
const { openNewWin,imageUrlPrefix } = require('./promise.js');
const { bindTipsEvent } = require('./popupEdit.js');

function renderRecommendInfo(showNum,className,title,data, hasInfo, type, position) {
    let showNumCopy = showNum || 5
    const dataLen = data.length
    const str = `<div class="recommendInfoWrapper ${className}">
                    <h4><img class="titleIcon" src="./images/knzd.png" alt="icon" />${title}</h4>
                    <div class="recommendInfoBox">
                        <div class="clearfix recommendInfoItemBox ${className + 'box'}">
                        </div>
                    </div>
                </div>`
    $('.recommendWrap').append(str)
    let dataShort
    if(showNumCopy&& dataLen > showNumCopy) {
        dataShort = data.slice(0, showNumCopy)
    } else {
        dataShort = data
    }
    let childrenNodeStrShort = ''
    let childrenNodeStrLong = ''
    if(hasInfo) {
        for (let i = 0; i < dataShort.length; i++) {
            childrenNodeStrShort += `<span class="recommendInfoItem recommendInfoItemHasInfo">
                                    ${dataShort[i].name}
                                    <span  title="点击i图标可查看详细说明" class="infoMsg" data-name=${dataShort[i].name}></span>
                                </span>`
        }
        for (let i = 0; i < data.length; i++) {
            childrenNodeStrLong += `<span class="recommendInfoItem recommendInfoItemHasInfo">
                                    ${data[i].name}
                                    <span  title="点击i图标可查看详细说明" class="infoMsg" data-name=${data[i].name}></span>
                                </span>`
        }
    } else {
        for (let i = 0; i < dataShort.length; i++) {
            childrenNodeStrShort += `<span class="recommendInfoItem" data-name="${dataShort[i].name}">
                                    ${dataShort[i].name}
                                </span>`
        }
        for (let i = 0; i < data.length; i++) {
            childrenNodeStrLong += `<span class="recommendInfoItem" data-name="${data[i].name}">
                                    ${data[i].name}
                                </span>`
        }
    }
    if(!childrenNodeStrShort) {
        childrenNodeStrShort='<span class="recommendNull">暂无推荐</span>'
    }
    
    $('.'+className + 'box').append(childrenNodeStrShort)
    $('.'+className + 'box' +' .infoMsg').on('click', function(){
        const infoMsgName = $(this).attr('data-name')
        openNewWin(`information.html?type=${encodeURIComponent(type)}&position=${encodeURIComponent(position)}&name=${encodeURIComponent(infoMsgName)}`)
    })
    if(dataLen > showNumCopy) {
        slideToggle($("."+className+"box"), childrenNodeStrShort,childrenNodeStrLong)
    }
}

function renderMultRecommendInfo(className,title,data, hasInfo, type, position) {
    const str = `<div class="recommendInfoWrapper ${className}">
                    <h4><img class="titleIcon" src="./images/inspect.png" alt="icon" />${title}</h4>
                    <div class="recommendInfoBox ${className + 'box'}"></div>
                </div>`
    $('.recommendWrap').append(str)
    let hasDataNum = 0
    for(let i = 0; i < data.length; i++) {
        const showNum = data[i].showNum || 5
        const dataLen = data[i].data.length
        if(dataLen === 0) {
            continue
        }
        hasDataNum++
        if(hasDataNum > 1) {
            $('.'+className + 'box').append('<div class="line"></div>')
        }
        let childrenNodeBoxStr  = `<div class="labAndPacsBox ${data[i].className} clearfix"> </div>`
        let childrenNodeStrShort =  `<span class="labAndPacsTitle">${data[i].title}<i></i></span>`
        let childrenNodeStrLong =  `<span class="labAndPacsTitle">${data[i].title}<i></i></span>`
        let dataShort
        if(showNum&& dataLen > showNum) {
            dataShort = data[i].data.slice(0, showNum)
        } else {
            dataShort = data[i].data
        }
        for(let j = 0; j < dataShort.length; j++) {
            childrenNodeStrShort += `<span class="labAndPacsItem">${dataShort[j].name}</span>`
        }
        for(let j = 0; j < data[i].data.length; j++) {
            childrenNodeStrLong += `<span class="labAndPacsItem">${data[i].data[j].name}</span>`
        }
        if(data[i].data.length == 0) {
            childrenNodeStrShort +='<span class="recommendNull">暂无推荐</span>'
        }
        $('.'+className + 'box').append(childrenNodeBoxStr)
        $('.'+data[i].className).append(childrenNodeStrShort)
        if(dataLen > showNum) {
            slideToggle($("."+data[i].className), childrenNodeStrShort,childrenNodeStrLong)
        }
    }

}

function renderRecommendConditTips(className,title,data) {
    const str = `<div class="recommendInfoWrapper ${className}">
                    <h4><img class="titleIcon" src="./images/tips.png" alt="icon" />${title}</h4>
                    <div class="recommendInfoBox  ${className + 'box'}"></div>
                </div>`
    $('.recommendWrap').append(str)
    if(data.length == 0) {
        $("."+className+"box").append('<span class="recommendNull">暂无推荐</span>')
    };
    //let names = [];
    for(var k = 0;k < data.length;k++){
        let tmpBq = data[k];
        let hasSritical = false
        let tmpBqStr;
        if(tmpBq.details) {
            for(let i = 0; i < tmpBq.details.length; i++) {
                if(tmpBq.details[i].type == 4) {
                    hasSritical= true
                }
            }
        }
        if(hasSritical) {
            /*if(names.includes(tmpBq.name)){    //name相同去重
              tmpBqStr='';
            }else{
              names.push(tmpBq.name);*/
              tmpBqStr = `<p class="mayIllness">
              <span class="sriticalNameBox"><span class="sriticalName">${tmpBq.name}</span>
                </span>
            </span></p>`
            //}
        } else {
            tmpBqStr = `<p class="mayIllness">
                            患者可能有 <span class="diagNameBox"><span class="diagName">${tmpBq.name}:</span>
                                <span title="点击该名称可看详细说明" class="infoMsg" data-name=${tmpBq.name}></span>
                            </span>
                        </span></p>`
        }
         
        var tmpBqDetailStr = ''
        for(var m = 0;m < tmpBq.details.length;m++){
          var tmpBqDetail = tmpBq.details[m]
          if(tmpBqDetail.type == 1){
            tmpBqDetailStr+=`<span data-scaleName=${tmpBqDetail.content.name} class="illnessTable">【${tmpBqDetail.content.name}】</span>`
          }
          if(tmpBqDetail.type == 2){
            var deepDetailName = `<div class="modalTitle">${tmpBqDetail.content.name}<i class="closeModal"></i></div>`
            var deepDetailStr = ''
            var deepDetailResultStr = ''
            for(var n = 0;n < tmpBqDetail.content.details.length;n++){
              var deepDetail = tmpBqDetail.content.details[n]
              if(deepDetail.controlType == 2){//0-radio,1-checkbox,2-text,3-dropdownlist
                if(deepDetail.isShow == 0) {
                    deepDetailStr+=`<li class="radioCheck"><span class="modalMaintltle">${deepDetail.name}: </span>${deepDetail.value} ${deepDetail.uint}</li>`
                } else {
                    deepDetailStr+=`<li class="radioCheck"><span class="modalMaintltle">请输入${deepDetail.name}: </span><input type="text" class="${tmpBqDetail.content.name+deepDetail.name}" ${deepDetail.isShow=='0' ? 'readonly':''} placeholder="请输入" value=${deepDetail.value}><i class="uint">${deepDetail.uint}</i></li>`
                }
              }
              if(deepDetail.controlType == 0){
                let tooDeepDetailStr = ''
                for(var f = 0;f<deepDetail.details.length;f++){
                  var tooDeepDetail = deepDetail.details[f]
                  if(deepDetail.isShow == '0'){
                    if(tooDeepDetail.state == 1) {
                      tooDeepDetailStr+='<li class="radioChecks" data-select="'+(tooDeepDetail.state == 1?"select":"")+'"><span>'+tooDeepDetail.detailName+'</span></li>'
                    }
                  }else{
                    tooDeepDetailStr+='<li class="radioChecks"><i></i><span>'+tooDeepDetail.detailName+'</span>'+(tooDeepDetail.state == 1?"<a>（智能推荐）</a>":"")+'</li>'
                  }
                }
                
                
                deepDetailStr+=`<li class="radioCheck"><span class="modalMaintltle">${deepDetail.name}: </span>
                                <ul class="clearfix caculateLis" data-isShow = ${deepDetail.isShow}>
                                    ${tooDeepDetailStr}
                                </ul>
                            </li>`
                  
              }
            }
            for(var v = 0;v < tmpBqDetail.content.result.length;v++){
              deepDetailResultStr+='<p class="result"><span class="calculateName">'+tmpBqDetail.content.result[v].name+'：</span><span class="calculateResult"></span></p>'
            }
            tmpBqDetailStr+=`<div class="mayResult">
                计算公式结果：
                <span class="mayResultSelect" data-num="${k}">
                    <span class="makesureResult">请选择</span>
                    <span class="sanjiao"></span>
                    <div class="popupModal">
                        ${deepDetailName}
                        <div class="modalMainTwo">
                            <ul class="clearfix">
                                ${deepDetailStr}
                            </ul>
                            <div class="calculate" data-num="${k}">计算</div>
                            <p class="calculateError">请填写计算公式内容</p>
                            <div class="resultLis">
                                ${deepDetailResultStr}
                            </div>
                        </div>
                        <div class="btnWrap clearfix">
                            <div class="sureScore" data-type="2">关闭</div>
                        </div>
                    </div>
                </span>
            </div>`
            
          }
          if(tmpBqDetail.type == 3){
            var deepDetailName = '<span class="modalMaintltle">'+tmpBqDetail.content.name+': </span>'
            var deepDetailStr = ''
            for(var n = 0;n < tmpBqDetail.content.details.length;n++){
              var deepDetail = tmpBqDetail.content.details[n]
              deepDetailStr+='<li class="radioChecks"><i></i><span>'+deepDetail.detailName+'</span>'+(deepDetail.state == 1?"<a>（智能推荐）</a>":"")+'</li>'
            }

              tmpBqDetailStr+=`<div class="mayResult">
                可能结果：
                <span class="mayResultSelect" data-num="${k}"><span class="makesureResult">请选择</span><i class="sanjiao"></i>
                    <div class="popupModal">
                    <div class="modalTitle">
                        结果选择<i class="closeModal"></i>
                    </div>
                    <div class="modalMainOne">
                        ${deepDetailName}
                        <ul class="clearfix">
                            ${deepDetailStr}
                        </ul>
                    </div>
                    <div class="btnWrap clearfix">
                        <div class="sureScore" data-type="3" data-num="${k}">确定</div>
                    </div>
                </div>
                </span>
            </div>`
          }
          if(tmpBqDetail.type == 4){
            // tmpBqDetailStr+=`<div class="sriticalinfo">诊断要点：${tmpBqDetail.content.name}</div>`
          }
        }
        if(tmpBqStr != '' || tmpBqDetailStr != ''){
          $(".conditionHintTipsNull").css("display","none")
        }
        // $(".conditionHintTips").append('<div class="conditionMain clearfix">'+tmpBqStr+'<div class="alertModal">'+tmpBqDetailStr+'</div></div>')
        $("."+className+"box").append('<div class="conditionMain clearfix">'+tmpBqStr+'<div class="alertModal">'+tmpBqDetailStr+'</div></div>')
      }
      $('.mayIllness .infoMsg').on('click', function(){
        const infoMsgName = $(this).attr('data-name')
        openNewWin(`information.html?type=22&position=2&name=${encodeURIComponent(infoMsgName)}`)
      })
      $('.alertModal .illnessTable').on('click', function(){
        const msg1 = JSON.parse(JSON.stringify(msg))
        msg1.scaleName = $(this).attr('data-scaleName')
        msg1.featureType = 21
        openNewWin(`scale.html?msg=${encodeURIComponent(JSON.stringify(msg1))}`)
      })
      bindTipsEvent()
}



function renderCaseStandard(className,title,data){
    const str = `<div class="recommendInfoWrapper ${className}">
        <h4><img class="titleIcon" src="./images/knzd.png" alt="icon" />${title}</h4>
        <div class="recommendInfoBox">
            <div class="clearfix recommendInfoItemBox ${className + 'box'}">
            </div>
        </div>
    </div>`
    let childStr = ""
    for(let i = 0; i < data.length; i++){
        childStr += `<p>${data[i]}</p>`
    }
    $('.recommendWrap').append(str)
    $(`.${className}box`).html(childStr)
}
function renderWarning(showNum,className,title,data, hasInfo, type, position){
    let showNumCopy = showNum || 5
    const dataLen = data.length
    const str = `<div class="recommendInfoWrapper ${className}">
                    
                    <div class="recommendInfoBox">
                       
                        <div class="clearfix recommendInfoItemBox ${className + 'box'}">
                        <h4><img class="titleIcon" src="./images/warings.png" alt="icon" />${title}</h4>
                        </div>
                    </div>
                </div>`
    $('.recommendWrap').append(str)
    let dataShort
    if(showNumCopy&& dataLen > showNumCopy) {
        dataShort = data.slice(0, showNumCopy)
    } else {
        dataShort = data
    }
    let childrenNodeStrShort = ''
    let childrenNodeStrLong = ''
    if(hasInfo) {
        for (let i = 0; i < dataShort.length; i++) {
            childrenNodeStrShort += `<span class="recommendInfoItem recommendInfoItemHasInfo">
                                    ${dataShort[i].name}
                                    <span  title="点击i图标可查看详细说明" class="infoMsg" data-name=${dataShort[i].name}></span>
                                </span>`
        }
        for (let i = 0; i < data.length; i++) {
            childrenNodeStrLong += `<span class="recommendInfoItem recommendInfoItemHasInfo">
                                    ${data[i].name}
                                    <span  title="点击i图标可查看详细说明" class="infoMsg" data-name=${data[i].name}></span>
                                </span>`
        }
    } else {
        for (let i = 0; i < dataShort.length; i++) {
            childrenNodeStrShort += `<span class="recommendInfoItem" data-name="${dataShort[i].name}">
                                    ${dataShort[i].name}
                                </span>`
        }
        for (let i = 0; i < data.length; i++) {
            childrenNodeStrLong += `<span class="recommendInfoItem" data-name="${data[i].name}">
                                    ${data[i].name}
                                </span>`
        }
    }
    if(!childrenNodeStrShort) {
        childrenNodeStrShort='<span class="recommendNull">暂无推荐</span>'
    }
    
    $('.'+className + 'box').append(childrenNodeStrShort)
    $('.'+className + 'box' +' .infoMsg').on('click', function(){
        const infoMsgName = $(this).attr('data-name')
        openNewWin(`information.html?type=${encodeURIComponent(type)}&position=${encodeURIComponent(position)}&name=${encodeURIComponent(infoMsgName)}`)
    })
    if(dataLen > showNumCopy) {
        slideToggle($("."+className+"box"), childrenNodeStrShort,childrenNodeStrLong)
    }
}

function slideToggle(domName,childrenNodeStrShort,childrenNodeStrLong){//展开收起
    domName.append('<span class="slideDown">更多 <i></i></span>')
    domName.on('click',".slideDown",function(){
        domName.html(childrenNodeStrLong)
        domName.append('<span class="slideup">收起 <i></i></span>')
        $(this).remove()
    })
    domName.on('click',".slideup",function(){
        domName.html(childrenNodeStrShort)
        domName.append('<span class="slideDown">更多 <i></i></span>')
        $(this).remove()
    })
}
module.exports ={
    renderCaseStandard,
    renderWarning,
    renderRecommendInfo,
    renderMultRecommendInfo,
    renderRecommendConditTips,
}