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

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".gnoRecord").hide();
    $(".gloader").hide();

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".gloader").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(),
                gsearch = $(".gsearch").val(),
                gfield = $(".gfield").val(),
                goperator = $(".goperator").val(),
                data = {gsearch: gsearch, gfield: gfield, goperator: goperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "godown_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".gnoRecord").show();
                    $(".gnext").hide();
                } else {
                    $(".gnext").show();
                    $(".gnoRecord").hide();

                    $.each(response, function () {
                        values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                                "<td align='left'>" + this.code + "</td>" +
                                "<td align='left'>" + this.name + "</td>" +
                                "<td align='left'>" + this.add1 + "</td>" +
                                "<td align='left'>" + this.city + "</td>" +
                                "<td align='left'>" + this.tel + " </td>" +
                                "<td align='left'>" + this.email + " </td>" +
                                "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                                "</tr>";
                    });
                    $(".gloader").hide();
                    $(".gTbody").empty();
                    $(".gTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
            },
            error: function (request, textStatus, errorThrown) {

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


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