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

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".otnoRecord").hide();
    $(".otloader").hide();

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".otloader").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(),
                otsearch = $(".otsearch").val(),
                otfield = $(".otfield").val(),
                otoperator = $(".otoperator").val(),
                data = {otsearch: otsearch, otfield: otfield, otoperator: otoperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "otherac_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".otnoRecord").show();
                    $(".otnext").hide();
                } else {
                    $(".otnext").show();
                    $(".otnoRecord").hide();

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

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


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