require('../css/staticSearch.less');
require('./../images/del_nor.png')
var $ = require("jquery");
var { post,config,getUrlArgObject,openNewWin,isIe8 } = require('./promise.js');
let checkOffImg = require('./../images/check_off.png')
let checkOnImg = require('./../images/check_on.png')
require('./../images/empty3.png').replace( /undefined/g, '')
require('./../images/icon_search.png').replace( /undefined/g, '')

let searchType = getUrlArgObject("searchType") || ""


let medicalTypesList = []
let first = true
const is8Ie = isIe8() //判断是否为IE8
if(is8Ie) {
  let reg = /undefined/g
  checkOffImg = checkOffImg.replace(reg, '')
  checkOnImg = checkOnImg.replace(reg, '')
}
if(searchType == '1'){
    $(".staticSearchB>ul").css({
        padding: "0px",
        left:"0px"
    })
    document.title = "医学知识搜索"
}
$(".staticSearchT input").keyup(function(e) {
    const ev = e||window.event;
    const val = $(this).val();
    $(this).parents(".staticSearchT").find("i").css({display:val==""?"none":"inline-block"})
    if(val == ''){
        $(".searchKnowledge .staticSearchB .empty").css({display:'none'})
        $(".searchKnowledge .staticSearchB .tip").css({display:'none'})
        $(".searchKnowledge .staticSearchB ul").html('')
        $(".searchKnowledge .staticSearchB .inputWarning").css("display","block")
       
    }else{
        $(".searchKnowledge .staticSearchB .inputWarning").css("display","none")
        
    }
    val!=''&&ev.keyCode == 13&&getKnowledgeData(val);
})
// document.title = "医学知识搜索"
$(".searchScale").css({display:'none'})
$(".searchKnowledge").css({display:'block'})
$(".searchKnowledge .staticSearchT").on('click','button',function() {
    var val = $(this).parents(".staticSearchT").find("input").val()
    if(val.trim() == ''){
        $(".searchKnowledge .staticSearchB ul").html("");
        $(".searchKnowledge .staticSearchB .tip").hide();
        $(".searchKnowledge .staticSearchB .inputWarning").css("display","block")
    }else{
        getKnowledgeData(val)
    }
})


function getMedicalTypes() {
    post(config.dictionaryInfo,{}).then((res) =>{
        const data = res.data
        if(data.code == 0) {
            medicalTypesList = res.data.data[7]
            medicalTypesList[0].checked = true
            renderMedicalType(medicalTypesList)
        } else {
            console.log(res)
        }
    })
}
function renderMedicalType(medicalTypesList) {
    let str = ''
    for(let i = 0; i < medicalTypesList.length; i++) {
        str += `<span class='medicalTypeItem' data-index=${i}><img src = ${medicalTypesList[i].checked? checkOnImg:checkOffImg}>${medicalTypesList[i].name}</span>`
    }
    $('.searchTypeBox').html(str)
    bindMedicalTypeClick();
}
function bindMedicalTypeClick() {
    $('.searchTypeBox .medicalTypeItem').on("click", function(){
        const index = $(this).attr('data-index')
        if(index == 0) {
            if(medicalTypesList[0].checked) {
                medicalTypesList[0].checked = false
            }else {
                for(let i = 0; i < medicalTypesList.length; i++) {
                    medicalTypesList[i].checked = false
                }
                medicalTypesList[0].checked = true
            }
            renderMedicalType(medicalTypesList)
        } else {
            if(medicalTypesList[0].checked) {
                medicalTypesList[0].checked = false
            }
            medicalTypesList[index].checked = !medicalTypesList[index].checked
        }
        renderMedicalType(medicalTypesList)
    })
}
getMedicalTypes()
function getKnowledgeData(value) {
    $(".searchKnowledge .staticSearchB .inputWarning").css("display","none")
    $(".searchKnowledge .staticSearchB .empty").css({display:'none'})
    $(".searchKnowledge .staticSearchB ul").html('')
    if(first) {
        adjustHeight()
        first =false
    }
    let medicalTypes = []
    for(let i = 0; i < medicalTypesList.length; i++) {
        if(medicalTypesList[i].checked) {
            medicalTypes.push(medicalTypesList[i].val)
        }
    }
    $(".searchKnowledge .staticSearchB .searching").css("display","block")
    post(config.getStaticKnowledge,{
        inputStr: value.replace(/\\/g,'\\\\').replace(/%/g,'\\%').replace(/\_/g,'\\_').trim(),
        types:medicalTypes
        }).then((res) => {
            $(".searchKnowledge .staticSearchB .searching").css("display","none")
        const data = res.data
        if(data.code == 0) {
            var knowledgeLis = data.data;
            var lisStr = ''
            if(knowledgeLis&&knowledgeLis.length>0){
                for(var i = 0;i<knowledgeLis.length;i++){
                    var tmpPart = knowledgeLis[i]
                    var tmpLi = `<li data-name='${tmpPart.name}' data-uname='${tmpPart.uniqueName}' data-type='${tmpPart.libTypeId}' data-sign='${tmpPart.type}'>
                        <p>${tmpPart.name}<i>${tmpPart.typeName?(' ( '+tmpPart.typeName+' )'):''}</i> <span>${tmpPart.code||''}</span></p>
                        ${tmpPart.retrievalName?('<span>• '+tmpPart.retrievalName+'</span>'):''}
                    </li>`
                    lisStr += tmpLi
                }
                $(".searchKnowledge .staticSearchB .empty").css({display:'none'})
                $(".searchKnowledge .staticSearchB .tip").css({display:'block'})
                $(".searchKnowledge .staticSearchB ul").html(lisStr)
            }else{
                $(".searchKnowledge .staticSearchB .empty").css({display:'block'})
                $(".searchKnowledge .staticSearchB .tip").css({display:'none'})
                $(".searchKnowledge .staticSearchB ul").html('')
            }
        }else{
            console.log(res)
        }
    })
}

function adjustHeight() {
    var ht = window.innerHeight;
    var containerHt = $(".medicalKonwledgeWrap .container").height()
    var ht2 = $(".searchTypeBox").outerHeight(true)
    $(".staticSearchB ul").height(ht - 120-10-40 +20- ht2 + "px");
}
$(window).on('resize', function(){
    adjustHeight()
})

$(".staticSearchB ul").on("click","li",function(){
    const infoMsgName = $(this).attr('data-name')
    const infoMsgUName = $(this).attr('data-uname')
    const type = $(this).attr('data-type')
    const sign = $(this).attr('data-sign')
    if(type != 48){
        openNewWin(`staticInfo.html?type=${encodeURIComponent(sign)}&position=0&name=${encodeURIComponent(infoMsgName)}`)
        // openNewWin(`information.html?type=${encodeURIComponent(sign)}&position=0&name=${encodeURIComponent(infoMsgName)}&uname=${encodeURIComponent(infoMsgUName)}`)
    }else{
        let msg = JSON.parse(getUrlArgObject("msg"));
        const msg1 = JSON.parse(JSON.stringify(msg))
        // console.log('scaleName', msg,infoMsgName)
        msg1.scaleName = infoMsgName
        msg1.featureType = 21
        openNewWin(`scale.html?msg=${encodeURIComponent(JSON.stringify(msg1))}`)
    }
})

  
$(".staticSearchT").on('click','#clearKnowledge',function() {
    $(".staticSearchT input").val("")
    $(".searchKnowledge .staticSearchB ul").html('')
    $(".searchKnowledge .staticSearchB .tip").css({display:'none'})
    $(".searchKnowledge .staticSearchB .empty").css({display:'none'})
    $(this).prev('.ipt').find('input').focus()
    $(this).css({display:"none"})
})
$(function(){
    $(".staticSearchT .ipt").find("input").focus()
})