/**
 * Created by Syusuke on 2017/3/17.
 */
$(function(){
    getRationLibs();
})

function addEvent_Addressdirect(){
    $("tr:gt(0)").each(function() {
        var td, a, tdId = $("td:eq(3)",$(this));
        td = $("td:eq(0)", $(this));
        a = $('a', td);
        a.attr('href', "/rationRepository/ration" + "?repository=" + tdId.text());
    })
}
function addEvent_DeleteLib(){
    $("tr:gt(0)").each(function() {
        var td0, a1,td2,a2;
        td2 = $("td:eq(2)", $(this));
        td0 = $("td:eq(0)", $(this));
        a1 = $('a', td0);
        var str = a1.text();
        a2 = $("a:eq(1)",td2);
        a2.click(function(){
            $("#did").val(str)
        })
        // a.attr('href', "/ration/rationLib" + "?rationname=" + a.text());
    })
}

function addEvent_EditLib(){
    $("tr:gt(0)").each(function() {
        var td0, a1,td2,a2;
        td2 = $("td:eq(2)", $(this));
        td0 = $("td:eq(0)", $(this));
        a1 = $('a', td0);
        var str = a1.text();
        a2 = $("a:eq(0)",td2);
        a2.click(function(){
            $("#did").val(str)
        })
    })
}

$("#deleteLib").click(function(){
    var rationLibName = $("#did").val();
    $.ajax({
        type:"POST",
        url:"api/deleteRationLibs",
        dataType:"json",
        data:{"rationName": rationLibName},
        cache:false,
        timeout:50000,
        success:function(result){
            getRationLibs();
        },
        error:function(iqXHR,textStatus,errorThrown){

        }
    })


});
function getRationLibs(){
    $.ajax({
        type:"POST",
        url:"api/getRationDisplayNames",
        dataType:"json",
        cache:false,
        timeout:5000,
        success:function(result){
            $("tbody tr").html("");
            for(var i=0;i<result.data.length;i++){
                addLibTag(result.data[i].dispName, result.data[i].ID);
                storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data[i].ID, result.data[i].dispName);
            }
            addEvent_Addressdirect();
            addEvent_DeleteLib();
            addEvent_EditLib();
        },
        error:function(iqXHR,textStatus,errorThrown){
            alert("error "+textStatus+" "+errorThrown);
        }
    })
}

function addLibTag(string, ID){
    var $tr = $('<tr><td><a href="#"></a></td><td></td><td><a href="javacript:void(0);" data-toggle="modal" data-target="#edit" title="编辑"><i class="fa fa-pencil-square-o"></i></a> <a href="javacript:void(0);" data-toggle="modal"     data-target="#del" class="text-danger" title="删除"><i class="fa fa-remove"></i></a></td><td style="display:none"></td></tr>')
    var $td0 = $("td:eq(0)",$tr);
    var $td1 = $("td:eq(1)",$tr);
    var $tdId = $("td:eq(3)",$tr);
    var a = $("a",$td0);
    var str = string;
    a.text(str);
    $tdId.text(ID);
    var date = new Date().Format("yyyy-MM-dd");
    $td1.text(date);
    $tr.appendTo("tbody");
}

function addRationRepository(rationObj, $tdId) {
    $.ajax({
        type:"POST",
        url:"api/addRationRepository",
        dataType:"json",
        data:{"rationRepObj": JSON.stringify(rationObj)},
        cache:false,
        timeout:5000,
        success:function(result){
            $tdId.text(result.data.ID);
            storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data.ID, result.data.dispName);
            addEvent_Addressdirect();
            addEvent_DeleteLib();
        },
        error:function(iqXHR,textStatus,errorThrown){
            alert("error "+textStatus+" "+errorThrown);
        }
    })

}

$("#rationAdd").click(function(){
    var $tr = $('<tr><td><a href="#"></a></td><td></td><td><a href="javacript:void(0);" data-toggle="modal" data-target="#edit" title="编辑"><i class="fa fa-pencil-square-o"></i></a> <a href="javacript:void(0);" data-toggle="modal" data-target="#del" class="text-danger" title="删除"><i class="fa fa-remove"></i></a></td><td style="display:none"></td></tr>')
    var $td0 = $("td:eq(0)",$tr);
    var $td1 = $("td:eq(1)",$tr);
    var $tdId = $("td:eq(3)",$tr);
    var a = $("a",$td0);
    var str = $("#addInput").val();
    a.text(str);
    var date = new Date().Format("yyyy-MM-dd");
    $td1.text(date);
    $tr.appendTo("tbody");
    var newRationRepObj = {};
    newRationRepObj.dispName = str;
    newRationRepObj.appType = "建筑";
    newRationRepObj.descr = "";
    addRationRepository(newRationRepObj, $tdId);
});

$("#edtOK").click(function(){
    var str = $("#did").val();
    var newStr = $("#inputRation").val();
    $.ajax({
        type:"POST",
        url:"api/editRationLibs",
        dataType:"json",
        data:{"rationName": str,"newName":newStr},
        cache:false,
        timeout:50000,
        success:function(result){
            getRationLibs();
        },
        error:function(jqXHR){
            var err = JSON.parse(jqXHR.responseText);
            alert(err.error);
        }
    })

});

autoFlashHeight();