/**
 * Created by zhang on 2018/9/3.
 */
let router = "engineerFeature";
$(document).ready(function() {
    // 保存按钮
    $("#addLibs").click(async function() {
        let name = $('#name').val();
        if(name==''){
            $("#nameError").show();
            return;
        }else {
            $("#addLibs").attr("disabled",true);//防止重复提交
            $("#addLibForm").submit();
        }
    });

    $("#rename").click(async function() {
        let libID = $("#libID").val();
        let name = $('#renameText').val();
        if(libID!=''){
            if(name ==''){
                $("#renameError").show();
                return;
            }else {
                try {
                    let newInfo = await ajaxPost(`/${router}/saveLib`,{query:{ID:libID},data:{name:name}});
                    $("#"+libID).children("a").text(newInfo.name);
                    $("#edit").modal('hide');
                }catch(err) {
                    console.log(err);
                }
            }
        }
    });

    $("#delete").click(async function() {
        let libID = $("#libID_del").val();
        let delCount = parseInt($("#delCount").val());
        delCount = delCount+1;
        $("#delCount").val(delCount);
        if(delCount == 3){
            if(libID!=""){
                try {
                    let result = await ajaxPost(`/${router}/deleteLibByID`,{ID:libID});
                    if(result.ok){
                        $("#"+libID).parent(".libTr").remove();
                    }
                    $("#del").modal('hide');
                }catch (err){
                    console.log(err);
                }
            }
        }
    });

    // 锁定、解锁
    $('.lock').click(function () {
        lockUtil.handleLockClick($(this));
    });
});

async function getLibInfo (ID) {
    try {
        let lib = await ajaxPost(`/${router}/findLib`,{ID:ID});
        if(lib){
            $("#renameText").val(lib.name);
            $("#libID").val(ID);
            $("#edit").modal({show:true});
        }else {
            alert("没有找到基本信息库");
        }
    }catch (err){
        console.log(err);
    }
}

function showDeleteModal(ID){
    $("#libID_del").val(ID);
    $("#delCount").val(0);
    $("#del").modal({show:true});
}