/**
 * Created by Zhong on 2017/12/22.
 */

let compleRationMain = {
    getRationLibs: function () {
        let scFunc = function (rstData) {
            $("#comple_ration_table").empty();
            for(let i = 0; i < rstData.length; i++){
                if(rstData[i].gljLib !== undefined && rstData[i].gljLib && rstData[i].gljLib != -1){
                    let id = rstData[i].ID;
                    let libName = rstData[i].dispName;
                    $("#comple_ration_table").append(
                        "<tr id='tempId'>" +
                        "<td><a href='javascript:void(0);'>"+libName+"</a></td></tr>"
                    );
                    $("#tempId td:first a").click(function () {
                        $('#comple-ration').modal('hide');
                        setTimeout(function () {
                            let newHref = "/complementaryRation/ration?repository="+id;
                            let newTab = window.open('about:blank');
                            CommonAjax.get(newHref, [], function () {
                                newTab.location.href = newHref;
                            });
                        }, 200);
                    });
                    $("#tempId").attr("id", id);
                }
            }
        };
        let ration_lib = projectObj.project.projectInfo.engineeringInfo.ration_lib;
        let ids = [];
        for(lib of ration_lib){
            ids.push(parseInt(lib.id));
        }
        CommonAjax.post('/complementaryRation/api/getRationLibs', {user_id: userID, ids: ids}, scFunc);
    }
};

$(document).ready(function () {
    $('#comple-ration').on('shown.bs.modal', function () {
        compleRationMain.getRationLibs();
    });
});