/**
 * Created by zhang on 2018/8/22.
 */
$(document).ready(function() {
    $('#add').on('show.bs.modal', function () {
        $('#compilationSels').empty();
        $('#billLibs').empty();
        getCompilationOptions();
        getBillsLibOptions();

    });

    // 保存按钮
    $("#addLibs").click(async function() {
        let name = $('#name').val();
        if(name==''){
            $("#nameError").show();
            return;
        }else {
            let result = await validateLib($('#billLibs').val(),$('#compilationSels').val());
            if(result == true){//不存在则验证通过
                $("#addLibs").attr("disabled",true);//防止重复提交
                $("#addLibForm").submit();
            }else {
                alert('清单规则和定额库对应的材料库已存在，请重新选择');
            }
        }
    });

    $("#rename").click(async function() {
        let libID = $("#libID").val();
        let name = $('#renameText').val();
        if(libID!=''){
            if(name ==''){
                $("#renameError").show();
                return;
            }else {
                try {
                    let newMaterial = await ajaxPost("/materialReplace/saveLib",{query:{ID:libID},data:{name:name}});
                    $("#"+libID).children("a").text(newMaterial.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("/materialReplace/deleteLibByID",{ID:libID});
                    if(result.ok){
                        $("#"+libID).parent(".libTr").remove();
                    }
                    $("#del").modal('hide');
                }catch (err){
                    console.log(err);
                }
            }
        }
    });

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

})

//检查库是否已经存在，存在则返回false
async function validateLib(billLibID,compilationID){
    try {
        let lib = await ajaxPost("/materialReplace/findLib",{billLibID:billLibID,compilationID:compilationID});
        return lib?false:true;
    }catch (err){
        console.log(err);
        return false
    }
}

//取所有的定额并生成下拉框
async function getCompilationOptions() {
    try {
        let compilations = await ajaxPost("/stdBillsEditor/getCompilationList");
        for(let com of compilations){
            let $option =  $("<option >"+ com.name +"</option>");
            $option.val( com._id);
            $('#compilationSels').append($option);
        }
    }catch (err){
        console.log(err)
    }
}

//取所有的清单规则库并生成下拉框
async function getBillsLibOptions(){
    try {
        let libs = await ajaxPost("/stdBillsEditor/getStdBillsLib");
        for(let b of libs){
            let $option =  $("<option >"+ b.billsLibName +"</option>");
            $option.val( b.billsLibId);
            $('#billLibs').append($option);
        }
    }catch (err){
        console.log(err)
    }

}

async function getMaterialLib (ID) {
    try {
        let lib = await ajaxPost("/materialReplace/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});
}