/**
 * Created by zhang on 2018/7/12.
 */

$(document).ready(function() {
    $('#add').on('show.bs.modal', function () {
        $('#compilationSels').empty();
        mainAjax.getCompilationList();
    });

    // 保存按钮
    $("#addTemplate").click(function() {
        let name = $('#name').val();
        if(name==''){
            $("#nameError").show();
            return;
        }else {
            $("#addTemplate").addClass("disabled");//防止重复提交
            $("#addTemplateForm").submit();
        }
    });

    $("#rename").click(function() {
        let libID = $("#libID").val();
        let name = $('#renameText').val();
        if(libID!=''){
            if(name ==''){
                $("#renameError").show();
                return;
            }else {
                CommonAjax.post("/billsTemplate/saveLib",{query:{ID:libID},data:{name:name}},function (data) {
                    $("#"+libID).children("a").text(data.name);
                    $("#edit").modal('hide');
                });
            }
        }
    });

    $("#delete").click(function() {
        let libID = $("#libID_del").val();
        let delCount = parseInt($("#delCount").val());
        delCount = delCount+1;
        $("#delCount").val(delCount)
        if(delCount == 3){
           if(libID!=""){
               CommonAjax.post("/billsTemplate/deleteLibByID",{ID:libID},function (data) {
                   if(data.ok){
                       $("#"+libID).parent(".libTr").remove();
                   }
                   $("#del").modal('hide');
               });
           }
        }
    });

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

    //复制库
    $('#copyTemplate').click(function () {
        const libID = $('#libID').val();
        const name = $('#copyName').val();
        if (!name) {
            $('#copyNameError').show();
            return;
        }
        $.bootstrapLoading.start();
        CommonAjax.post('/billsTemplate/copyLib', {libID, name}, function () {
            $.bootstrapLoading.end();
            window.location.reload();
        }, function () {
            $.bootstrapLoading.end();
        });
    });
});

function getTemplateLib (ID) {
    CommonAjax.post("/billsTemplate/getLibByID",{libID:ID},function (data) {
        $("#renameText").val(data.name);
        $("#libID").val(ID);
        $("#edit").modal({show:true});
    });
}

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

function showCopyModal(ID) {
    $('#libID').val(ID);
    $('#copy').modal('show');
}