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

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".exnoRecord").hide();
    $(".exloader").hide();

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".exloader").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(),
                exsearch = $(".exsearch").val(),
                exfield = $(".exfield").val(),
                exoperator = $(".exoperator").val(),
                data = {exsearch: exsearch, exfield: exfield, exoperator: exoperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "expense_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".exnoRecord").show();
                    $(".exnext").hide();
                } else {
                    $(".exnext").show();
                    $(".exnoRecord").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>";
                    });
                    $(".exloader").hide();
                    $(".exTbody").empty();
                    $(".exTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
            },
            error: function (request, textStatus, errorThrown) {

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


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