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

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".ignoRecord").hide();
    $(".igloader").hide();

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".igloader").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(),
                igsearch = $(".igsearch").val(),
                igfield = $(".igfield").val(),
                igoperator = $(".igoperator").val(),
                data = {igsearch: igsearch, igfield: igfield, igoperator: igoperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "igrp_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".ignoRecord").show();
                    $(".ignext").hide();
                } else {
                    $(".ignext").show();
                    $(".ignoRecord").hide();

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

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


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