/**
 * Created by zhang on 2018/9/11.
 */

$(document).ready(function() {
    $('#add').on('show.bs.modal',async function () {
        await initCompilationSelect();
    });

    // 保存按钮
    $("#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 newFeeRate = await ajaxPost("/feeRate/saveLib",{query:{ID:libID},data:{libName:name}});
                    $("#"+libID).children("a").text(newFeeRate.libName);
                    $("#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("/feeRate/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 getFeeRateLib (ID) {
    try {
        let lib = await ajaxPost("/feeRate/findLib",{ID:ID});
        if(lib){
            $("#renameText").val(lib.libName);
            $("#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});
}