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

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".ttnoRecord").hide();
    $(".ttloader").hide();

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".ttloader").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(),
                ttsearch = $(".ttsearch").val(),
                ttfield = $(".ttfield").val(),
                ttoperator = $(".ttoperator").val(),
                data = {ttsearch: ttsearch, ttfield: ttfield, ttoperator: ttoperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "taxtype_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".ttnoRecord").show();
                    $(".ttnext").hide();
                } else {
                    $(".ttnext").show();
                    $(".ttnoRecord").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.perc + "</td>" +
                                "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                                "</tr>";
                    });
                    $(".ttloader").hide();
                    $(".ttTbody").empty();
                    $(".ttTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
            },
            error: function (request, textStatus, errorThrown) {

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


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