const $ = require('jquery');
require("../css/moduleManager.less");
require('./../resource/jquery-ui/jquery-ui.min.js');
require('./../resource/jquery-ui/jquery-ui.min.css');
require('./modal.js');

// const  layui = require('layui-src');
const {api} = require('./api.js')
const {post,getCookie,emptyBox,isMac} = require('./utils.js')
function setTbaleInfoHei(){
 
    $(".tableInfo").css({
        height:$(window).height()-140+'px'
    })
}
setTbaleInfoHei()

//判断有无某一权限
function hasData(data){
    let lis = JSON.parse(getCookie("codeLis"))||[]
    if(lis.indexOf(data)>-1){//有权限
        return true
    }
    return false;
  }
let data = {}
function getTabData(){
    post(api.getQcCases).then(res =>{
        if(res.data.code == '0'){
            data = res.data.data
            renderTab()
        }else{}
    }).catch((e) =>{
    
    })
}
getTabData()

function renderTab(){
    let  str = ``;
    const cls = hasData("FUNC000004")?"moduleOperaItem":"moduleOperaItemDis";
    for(let i = 0; i < data.length; i++){
    
        str += `
        <tr data-index=${i}>
            <td class="moduleNumber">${i+1}</td>
            <td class="moduleName">${data[i].name}</td>
            <td class="moduleScore">${data[i].score}</td>
            <td class="moduleOpera moduleOperaItem textCenter" style="opacity:${hasData('FUNC000004')?'1':'0.5'}">修改</td>
        </tr>
        `
    }
    // str += `
    // <tr>
    //     <td class="moduleNumber"></td>
    //     <td class="moduleName">总分</td>
    //     <td class="totalSum"></td>
    // </tr>
    // `
    $('.tableInfo table').html(str?str:emptyBox())
    const tableInfoBox =  $(".tableInfo").height()
    const tableInfoHei =  $(".tableInfo table").height()
    const mac = isMac()
    if(mac&&tableInfoHei > tableInfoBox){
        $(".tableTitle").css("paddingRight", 15+'px')
    }
    // bindInput()
    bindModuleOpera()
    getTotalSum()

}

function bindModuleOpera(){
    if(!hasData('FUNC000004')){
        return
    }
    $(".moduleOperaItem").on("click", function(e){
        const index = $(this).parent().attr('data-index')
        const item = data[index]
        midifyMsgBox(item)
    })
}
function getTotalSum(){
    let sum = 0
    console.log('data', data)
    for(let i = 0; i < data.length; i++){
        if(data[i].score){
            sum += Number(data[i].score)
        }
    }
    $('.totalSum').html(sum)
}

  
function midifyMsgBox(item){
    const itemCopy = JSON.parse(JSON.stringify(item))
    let msgBox =`<div class="midifyMsgBox">
        <div class="mask"></div>
        <div class="midifyMsgContent">
            <p class="midifyMsgTitle">修改 <img class="iconClose" src=${require('./../images/icon_close_default.png')} alt="关闭" /></p>
            <div class="itemMsgInfoBox"><span class="itemMsgLab">模块名称：</span> <span class="itemInfoRight">${itemCopy.name}</span></div>
            <div class="itemMsgInfoBox"><span class="itemMsgLab">分值：</span> <span class="itemInfoRight"> <input class="itemScore" type="number" min="0" value=${itemCopy.score} /></span></div>
            <div  class="itemMsgInfoTips">支持≥0且≤100的数字输入，最多保留小数点后1位~</div>
            <div class="midifyMsgBtn">
                <span class="midifyEdit">保存</span>
            </div>
        </div>
    </div>`
    $("#mainBox", parent.document).append(msgBox)
    $("#mainBox .midifyMsgBox .iconClose", parent.document).on("click", function(e){
        $("#mainBox .midifyMsgBox", parent.document).remove()
    })
    $("#mainBox .midifyMsgBox .itemScore", parent.document).on("keypress", function(event){
        return (/[\d\.]/.test(String.fromCharCode(event.keyCode)))
     })
    $("#mainBox .midifyMsgBox .itemScore", parent.document).on("input", function(e){
       const val = $(this).val()
       itemCopy.score = val
    })
    $("#mainBox .midifyMsgBox .midifyEdit", parent.document).on("click", function(e){
        var reg = new RegExp(/^\d+(\.\d)?$/);
        if(reg.test(itemCopy.score) && itemCopy.score <= 100){
            saveModifyOne(itemCopy)
            $("#mainBox .midifyMsgBox .itemMsgInfoTips", parent.document).css("display","none")
            $("#mainBox .midifyMsgBox", parent.document).remove()
            return 
        }else{
            $("#mainBox .midifyMsgBox .itemMsgInfoTips", parent.document).css("display","block")
            
            return
        }
        
        $("#mainBox .midifyMsgBox", parent.document).remove()
    })
}
function saveModifyOne(item){
    const qcCasesSaveVOList = []
    qcCasesSaveVOList.push({
        id: item.id,
        score: item.score,
    })
    post(api.saveQcCases, {qcCasesSaveVOList}).then(res =>{
        if(res.data.code == '0'){
            $.alerModal({"message":"修改成功",type:"tip",time:'1000',isFather: true, fatherWrapper: $("#mainBox", parent.document)});
            getTabData()
        }else{}
    }).catch((e) =>{
    
    })

    
}

// function bindInput(){
//     $('input').on('input', function(e){
//         const val = $(this).val()
//         const index = $(this).attr('data-index')
//         $(this).attr('value', val)
//         data[index].score = val 
//         getTotalSum(data)
//     })
// }

//下方保存保存全部
// let msgBox =`<div class="moduleMsgBox">
//         <div class="mask"></div>
//         <div class="moduleMsgContent">
//             <p class="moduleMsgTitle">提示 <img class="iconClose" src=${require('./../images/icon_close_default.png')} alt="关闭" /></p>
//             <p class="moduleMsgInfo">总分不是100分，是否继续</p>
//             <div class="moduleMsgBtn">
//                 <span class="saveModuleEdit">保存</span>
//             </div>
//         </div>
//     </div>`

// $('.saveModule').on("click", function(){
//     const sum =  $('.totalSum').html()
//     if(sum != 100){
//         $("#mainBox", parent.document).append(msgBox)
//         $("#mainBox .moduleMsgBox .iconClose", parent.document).on("click", function(e){
//             $("#mainBox .moduleMsgBox", parent.document).remove()
//         })
//         $("#mainBox .moduleMsgBox .saveModuleEdit", parent.document).on("click", function(e){
//             saveModify()
//             $("#mainBox .moduleMsgBox", parent.document).remove()
//         })
//     }else{
//         saveModify()
//     }
    
    
    // const sum =  $('.totalSum').html()
    // if(sum != 100){
    //     alert('满分不是100')
    // }
// })
// function saveModify(){
//     const qcCasesSaveVOList = []
//     for(let i = 0; i < data.length; i++){
//         qcCasesSaveVOList.push({
//             id: data[i].id,
//             score: data[i].score,
//         })
//     }
//     post(api.saveQcCases, {qcCasesSaveVOList}).then(res =>{
//         if(res.data.code == '0'){
//             getTabData()
//         }else{}
//     }).catch((e) =>{
    
//     })

    
// }

