$(document).ready(function () {
    $("body").on("keyup", ".grsearch", getSearchData);
    $("body").on("click", ".grload", getSearchData);
    $("body").on("click", "#search", getSearchData);

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".grnoRecord").hide();
    $(".grloader").hide();

    $(".searchModal").on('shown.bs.modal', function () {
        $(".grsearch").focus();
        $(".grsearch").val("");
        $(".grfield").val("1");
        getSearchData();
    });

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".grloader").show();

        if (pagination == 'prev') {
            if (imrangeFrom > 0) {
                imrangeFrom = (imrangeFrom - 40);
                imrangeTo = (imrangeTo - 40);
            }
        }
        if (pagination == 'next') {
            imrangeFrom = (imrangeFrom + 40);
            imrangeTo = (imrangeTo + 40);
        }

        var series = $(".series").val(),
                vouchno = $(".vouchno").val(),
                grsearch = $(".grsearch").val(),
                grfield = $(".grfield").val(),
                groperator = $(".groperator").val(),
                data = {grsearch: grsearch, grfield: grfield, groperator: groperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "group_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".grnoRecord").show();
                    $(".grnext").hide();
                } else {
                    $(".grnext").show();
                    $(".grnoRecord").hide();

                    $.each(response, function () {
                        values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                                "<td algrn='left'>" + this.name + "</td>" +
                                "<td algrn='left'>" + this.group + "</td>" +
                                "<td algrn='left'>" + this.initial + "</td>" +
                                "<td algrn='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                                "</tr>";
                    });
                    $(".grloader").hide();
                    $(".grTbody").empty();
                    $(".grTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $("#search").click(function () {
        getSearchData();
    });
    $(".grprev").click(function () {
        getSearchData('prev');
    });
    $(".grnext").click(function () {
        getSearchData('next');
    });


    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////
});