$(document).ready(function() {
    //load_activity(1);
});
function OpenDetail(loc_url){
    window.location.href = loc_url;
}
function moreData(){
    var cur_page = $("#tpage-act").val();
    var new_page = eval(cur_page) + eval(1);
    load_activity(new_page);
}
function load_activity(page){
    if(page != 1){
        $("#tloader-act").fadeIn();
    } else {
        $('#tlist-act').html("");
        $("#tloader-act").show();
    }
    get_activity(page);
    $("#tpage-act").val(page);
}
function get_activity(page){
    var item_perpage = 10;//item perpage

    var url = api_activity_dashboard;
    var session_id = $("#session-id").val();
    
    var data = {
        user_id : session_id,
        item : item_perpage,
        page : page
    }
    $.ajax({type: "POST",url: url,data : data, success:function(result){
        if(result.code == 200){
            $("#tpage-act").val(page);
            var total_data = result.total_data;
            var remaining = result.remaining;
            if(total_data > 0){
                var total_page = result.total_page;
                setTimeout(function() {
                    var datas = result.data;
                    $("#tloader-act").hide();
                    $('#tlist-act').append(show_data(datas, page, item_perpage));
                    if(remaining < 1){
                        $("#tmore-act").hide();
                    } else {
                        $("#tmore-act").show();
                    }
                }, 300);
            } else {
                setTimeout(function() {
                    emptyItem();
                    $("#tmore-act").hide();
                    $("#tloader-act").fadeOut();
                }, 300);
            }
        } else {
            setTimeout(function() {
                emptyItem();
                $("#tmore-act").hide();
                $("#tloader-act").fadeOut();
            }, 300);
        }
    }});
};
function emptyItem(){
    resultHTML = "<div class='text-center' style='font-weight:bold;'>There is no data right now.</div>";
    $('#tlist-act').html(resultHTML);
}

function show_data(datas,page,item_perpage){
    var resultHTML='';

    if(datas != null){
        var obj = datas;
        var num = 1;
        for(var i=0;i < obj.length;i++){
            var number = (page-1)*item_perpage+num;
            resultHTML += "<li class='item'>";
            resultHTML += "<div class='product-img'>";
            if(obj[i].admin_img != null){
            resultHTML += "<img src='"+url_global+obj[i].admin_img+"' class='img-circle' alt='User Image'>";
            } else {
            resultHTML += "<img src='"+url_global+"img/dummy.jpg' class='img-circle' alt='User Image'>";    
            }
            resultHTML += "</div>";
            resultHTML += "<div class='product-info'>";
            resultHTML += "<a href='javascript:void(0)' class='product-title'>";
            if(obj[i].admin_name != null){
                resultHTML += "<strong>"+obj[i].admin_name+"</strong><br>"+obj[i].activity_ref_shipment;
            } else {
                if(obj[i].activity_ref_user != 0){
                    resultHTML += "<strong>Admin Warehouse</strong><br>"+obj[i].activity_ref_shipment;
                } else {
                    resultHTML += "<strong>Warehouse System</strong><br>"+obj[i].activity_ref_shipment;
                }
            }
            resultHTML += "</a>";
            resultHTML += "<span class='product-description'>";
            resultHTML += "Update Status "+change_notes(obj[i].activity_type);
            resultHTML += "</span>";
            resultHTML += "<span class='product-description'>";
            resultHTML += dateFormat(obj[i].activity_create_date, "dd mmmm yyyy H:MM:ss")
            resultHTML += "</span>";
            resultHTML += "</div>";
            resultHTML += "</li>";

            num++;
        }
    }
    return resultHTML;
}
function change_notes(type){
    var result = "";

    if(type == "open"){
        result = "Shipment <strong>Scheduled</strong>";
    } else if (type == "progress"){
        result = "Shipment <strong>In Progress</strong>";
    } else if (type == "prepared"){
        result = "Shipment <strong>Prepare</strong>";
    } else if (type == "complete"){
        result = "Shipment <strong>Complete</strong>";
    } else if (type == "cancel"){
        result = "Shipment <strong>Canceled</strong>";
    }

    return result;
}