const $ = require('jquery');
require("../css/assertTypeDetail.less");
require('./modal.js');
const {api} = require('./api.js')
const {post,getCookie,getUrlArgObjectNew,getUrlArgObject} = require('./utils.js')
const iconCheck= require("./../images/icon_check.png")
const iconUnCheck = require("./../images/icon_unchecked.png")
const arrowUp = require("./../images/arrow_up22.png")
const arrowDown = require("./../images/arrow_down22.png")

let dataCopy,paramType,paramId,submitMsg="新增成功"
$(function(){
    let srcUrl = $("#contentIframe",parent.document).attr("src")
    paramType = getUrlArgObject("type")||""
    paramId = getUrlArgObject("id")||""
    getList(paramType)//获取条目列表

})
function setInpWidth(){
    const winWidth = $(window).width()
    $('.patientNumInp').css('width', winWidth-153-15 +'px')
}
setInpWidth()
$(window).resize(function(){
    setInpWidth()
})
function getList(type){
    if(!type){
        post(api.getEntryByHospital,{}).then(res =>{
            let result = res.data
            if(result.code == '0'){
                const data = result.data;
                renderData(data)
                
            }else{
                
            }
        }).catch((e) =>{
        
        })
    }else{
        if(type =='"edit"'){
            $('.partTitle p span').html('修改')
            submitMsg="修改成功"
        }else{
            $('.partTitle p span').html('复制')
            submitMsg="复制成功"
        }
        post(api.typeGetById,{id: paramId}).then(res =>{
            let result = res.data
            if(result.code == '0'){
                const data = result.data;
                const qcName = data.name
                const tabData = data.entryMap
                if(type =='"edit"'){
                    $('.patientNumInp').val(qcName)
                }
                renderData(tabData)
                
            }else{
                
            }
        }).catch((e) =>{
        
        })
    }
    
}

function renderData(data){
    dataCopy = data
    const dataKeys = Object.keys(dataCopy)
    let str = ""
    for(let i = 0; i < dataKeys.length; i++){
        str += `<div class="caseEntryBlcok ${i===0 ? 'borderTop':''}"><div class="caseEntryName">${dataKeys[i]} <img class="arrow" src=${arrowDown}></div>`
        if(dataCopy[dataKeys[i]].length > 0){
            str += `<div class="caseEntryDetail"><div class="tabTitle"><span class="opera">操作</span><span class="qcItem">质控条目</span><span class="qcUsed">是否启用</span></div>`
        }
        for(let j = 0; j < dataCopy[dataKeys[i]].length; j++){
            const dataItem = dataCopy[dataKeys[i]][j]
            str += `<div class="tabInfo"><span  data-pIndex=${dataKeys[i]} data-index=${j} class="opera">${dataItem.sel == '1' ? `<img  class="iconCheck" src=${iconCheck} />`:`<img class="iconCheck" src=${iconUnCheck} />`}</span><span class="qcItem">${dataItem.name}</span><span class="qcUsed"><span class="usedRes">${dataItem.isUsed == '1' ? "是":"否"}</span></span></div>`
        }
        if(dataCopy[dataKeys[i]].length > 0){
            str += `</div>`
        }
        str += `</div>`
    }
  
    $('.listTypeAll').html(str)
    bindBlockSlideToggle()
    bindCheck()
}
function bindBlockSlideToggle(){
 $('.caseEntryName').click(function(){
     const display =  $(this).parent().find('.caseEntryDetail').css("display");
     if(display === "none"){
        $(this).find('img').attr('src', arrowUp)
        $('.caseEntryDetail').slideUp()
        $(this).parent().find('.caseEntryDetail').slideDown()
     } else{
        $(this).find('img').attr('src', arrowDown)
        $('.caseEntryDetail').slideUp()
     }
    
 })
}

function bindCheck(){
    $('.iconCheck').click(function(){
        const checked =  $(this).attr('src') == iconCheck;
        const index = $(this).parent().attr('data-index')
        const pIndex = $(this).parent().attr('data-pIndex')
        if(checked){
            dataCopy[pIndex][index].sel = 0
            $(this).attr('src',iconUnCheck)
        } else{
            dataCopy[pIndex][index].sel = 1
            $(this).attr('src',iconCheck)
        }
    })
}

$('.submit').click(function(){
    const qcName = $('.patientNumInp').val().trim();
    if(!qcName){
        $.alerModal({"message":"质控类型不能为空",type:"tip",time:'1000',isFather: true, fatherWrapper: $("#mainBox", parent.document)});
        return
    }
    let qcTypeCasesEntryVOList = []
    const dataKeys = Object.keys(dataCopy)
    for(let i = 0; i < dataKeys.length; i++){
        for(let j = 0; j < dataCopy[dataKeys[i]].length; j++){
            const dataItem = dataCopy[dataKeys[i]][j]
            if(dataItem.sel == '1'){
                qcTypeCasesEntryVOList.push({
                    caseEntryId: dataItem.id
                })
            }
        }
      
    }
    const id = paramType == '"edit"' ? paramId:""
    const param = {
        id:id,
        name: qcName,
        qcTypeCasesEntryVOList
    }
   
    post(api.saveOrUpdate,param).then(res =>{
        let result = res.data
        if(result.code == '0'){
            $.alerModal({"message":submitMsg,type:"tip",time:'1000',isFather: true, fatherWrapper: $("#mainBox", parent.document)});
            setTimeout(function(){
                $(parent.document).find("#contentIframe").attr("src","./assertType.html")
            },1000)
        }else{
            $.alerModal({"message":res.data.msg,type:"tip",time:'1000',isFather: true, fatherWrapper: $("#mainBox", parent.document)});
        }
    }).catch((e) =>{
    
    })
})