// JavaScript Document
$(document).ready(function(){
	loadDynamicTableData()
})
function loadDynamicTableData(){
	if ($("#mssresulttable").length > 0) {
		ViewAllRecord();
		var exportMe = "";
		$(".searchData").html("<div class='row-fluid'><div class='span6'></div><div class='span6 text-right'><label>Search by any columns: <input id='searchinput' class='form-control' type='search' placeholder='' onkeyup='ViewAllRecord()'></label></div></div>");
    }
}
function applyfilter() {
    $("#mssresulttable").removeAttr("data-extra");
    var QStringUrl = [];
    $(".searchFilter").each(function(index, element) {
        var key = $(this).attr("name");
        var val = $(this).val()
        if (val != "" && typeof val != 'undefined') {
            var strs = key + "=" + encodeURI(val);
            QStringUrl.push(strs);
        }
    });
    $("#mssresulttable").attr("data-extra", "&"+QStringUrl.join("&"));
    ViewAllRecord()
}
function ViewAllRecord() {
    var startV = $('#startV').val();
    var endV = $('#endV').val();
    var page_id = $('#mssresulttable').attr('data-page');
    var page_action = $('#mssresulttable').attr('data-action');
    var page_extra = $('#mssresulttable').attr('data-extra');
    if ($("#searchinput").length > 0) {
        var q = $("#searchinput").val();
    } else {
        var q = "";
    }
    $("#resultBody").html('<tr><td colspan="5"><div class="loader"><i class="fa fa-repeat fa-spin"></i> Loading Please wait...</td></div></tr>');
    $(".paginationData").html('')
    var PostedData = new Array;
    var pagging = '';
    var ii = "";
	var uid=0;
	//if(user)uid=user.id; if(store)uid=store.id;
		console.log('hellow');
    $.getJSON(URL_ROOT + page_id + page_extra + "&action=" + page_action, { "startV": startV, "endV": endV, "q": q }, function(data) {

        if (data.ncount > 0) {
		
			
            $.each(data.Result, function(key, val) {  //console.log(val);
                posts = "<tr>";
                $.each(val, function(key1, val1) {
                    posts += "<td>" + val1 + "</td>";
                })
                posts += "</tr>";
                PostedData.push(posts)
            });
            getPaging(data.totPost)
        } else {
			var norecord='<div style="padding:30px 0px 30px 0"><i class="fa fa-random fa-5x"></i><div style="font-weight: bold;"><br>Sorry no record found</div></div>';
            PostedData.push('<tr><td colspan="' + data.tcolumn + '" align="center">'+norecord+'</td></tr>');
        }
        $("#resultBody").html(PostedData.join(""));
       //mss_link()
    })
}

function getPaging(totPost) {

    var pagging = '';
    var ii = "";
    var adjacents = 3;
    var CurrentPage = $("#CurrentPage").val();
    var end = $("#endV").val();

    if (totPost > 0) {
        var paginationCount = parseInt(totPost / end);
        var paginationModCount = totPost % end;
        if (paginationModCount != "") {
            paginationCount++;
        }
        var lpm1 = paginationCount - 1;
        pagging += '<div class="pagination text-center"><ul><li><a href="javascript:void(0)" onclick="numberPage(1)" class="first">First</a></li>';
        if (CurrentPage > 1) {
            pagging += '<li class="previous"><a href="javascript:void(0)" onclick="previousPage()">Previous</a></li>';
        }
        if (paginationCount <= 7 + (adjacents * 2)) {
            for (ii = 1; ii <= paginationCount; ii++) {
                var cclass = "";
                if (CurrentPage == ii)
                    cclass = "active";
                pagging += '<li class="' + cclass + '"><a href="javascript:void(0)" onclick="numberPage(' + ii + ')">' + ii + '</a></li>';
            }
        } else if (paginationCount >= 5 + (adjacents * 2)) {
            if (CurrentPage < 1 + (adjacents * 2)) {
                for (var counter = 1; counter < 4 + (adjacents * 2); counter++) {
                    if (counter == CurrentPage)
                        pagging += "<li class='active'><a href=''>" + counter + "</a></li>";
                    else
                        pagging += "<li><a href='javascript:void(0)' onclick='numberPage(" + counter + ")'>" + counter + "</a>";
                }
                pagging += "<li><a>...</a></li>";

                pagging += "<li><a  href='javascript:void(0)' onclick='numberPage(" + lpm1 + ")'>" + lpm1 + "</a></li>";

                pagging += "<li><a  href='javascript:void(0)' onclick='numberPage(" + paginationCount + ")'>" + paginationCount + "</a></li>";
            } else if (paginationCount - (adjacents * 2) > CurrentPage && CurrentPage > (adjacents * 2)) {
                pagging += "<li><a href='javascript:void(0)'  onclick='numberPage(1)'>1</a></li>";
                pagging += "<li><a href='javascript:void(0)'  onclick='numberPage(2)'>2</a></li>";
                pagging += "<li><a>...</a></li>";
                var checkOutv = parseInt(CurrentPage) + parseInt(adjacents);

                for (var Mycounter = CurrentPage - adjacents; Mycounter <= checkOutv; Mycounter++) {
                    if (Mycounter == CurrentPage)
                        pagging += "<li class='active'><a href='javascript:void(0)'>" + Mycounter + "</a></li>";
                    else
                        pagging += "<li><a href='javascript:void(0)'  onclick='numberPage(" + Mycounter + ")'>" + Mycounter + "</a></li>";
                }
                pagging += "<li><a>...</a></li>";
                pagging += "<li><a href='javascript:void(0)'  onclick='numberPage(" + lpm1 + ")'>" + lpm1 + "</a></li>";
                pagging += "<li class='last'><a class='nextal' href='javascript:void(0)' onclick='numberPage(" + paginationCount + ")'>" + paginationCount + "</a></li>";
            } else {
                pagging += "<li><a href='javascript:void(0)'  onclick='numberPage(1)'>1</a></li>";
                pagging += "<li><a href='javascript:void(0)'  onclick='numberPage(2)'>2</a></li>";
                pagging += "<li><a>...</a></li>";
                for (var counter = paginationCount - (2 + (adjacents * 2)); counter <= paginationCount; counter++) {
                    if (counter == CurrentPage)
                        pagging += "<li class='active'><a href='javascript:void(0)'>" + counter + "</a></li>";
                    else
                        pagging += "<li><a href='javascript:void(0)' onclick='numberPage(" + counter + ")'>" + counter + "</a></li>";
                }
            }
        }
        if (CurrentPage < (paginationCount)) {
            pagging += '<li class="next"><a href="javascript:void(0)" onclick="nextPage()">Next</a></li>';
        }
        pagging += '<li class="last"><a href="javascript:void(0)" onclick="numberPage(' + paginationCount + ')">Last</a></li>';
		pagging += '</ul><span class="link">Showing '+((CurrentPage*5)+1)+' to '+((CurrentPage*5)+5)+' of ' + totPost + ' entries</span>';
        //pagging += '</ul><span class="link">' + CurrentPage + ' of ' + paginationCount + '  Pages Total Record ' + totPost + '</span>';
    }
    $(".paginationData").html(pagging);
}

function nextPage() {
    var nextStart = parseInt($("#endV").val()) + parseInt($("#startV").val());
    $("#startV").val(nextStart);
    $("#CurrentPage").val(parseInt($("#CurrentPage").val()) + 1);
    ViewAllRecord()
}

function previousPage() {
    var nextStart = parseInt($("#startV").val()) - parseInt($("#endV").val())
    $("#startV").val(nextStart);
    $("#CurrentPage").val(parseInt($("#CurrentPage").val()) - 1);
    ViewAllRecord()
}

function numberPage(no) {
    var nextStart = parseInt(no - 1) * parseInt($("#endV").val())
    $("#startV").val(nextStart);
    $("#CurrentPage").val(no);
    ViewAllRecord()
}
