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

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".stnoRecord").hide();
    $(".stloader").hide();

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".stloader").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(),
                stsearch = $(".stsearch").val(),
                stfield = $(".stfield").val(),
                stoperator = $(".stoperator").val(),
                data = {stsearch: stsearch, stfield: stfield, stoperator: stoperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "saletype_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".stnoRecord").show();
                    $(".stnext").hide();
                } else {
                    $(".stnext").show();
                    $(".stnoRecord").hide();

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

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


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