$(document).ready(function () {
    $("body").on("keyup", ".ssearch", getSearchData);
    $("body").on("click", ".sload", getSearchData);
    $("body").on("click", "#search", getSearchData);
    $("body").on("change", ".state", getStateCode);

    function getStateCode() {
        var state = $(".state").val();
        $.get('get_stcode.php', {state: state}, function (data) {
            $(".statecode").val(data);
        });
    }


    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".snoRecord").hide();
    $(".sloader").hide();

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


    var imrangeFrom = 0,
            imrangeTo = 40;


    function getSearchData(pagination) {

        $(".sloader").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(),
                ssearch = $(".ssearch").val(),
                sfield = $(".sfield").val(),
                soperator = $(".soperator").val(),
                orderAs = $(".orderAs").val(),
                sortBy = $(".sortBy").val(),
                data = {ssearch: ssearch, sfield: sfield, sortBy: sortBy, soperator: soperator, orderAs: orderAs,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "supp_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".snoRecord").show();
                    $(".snext").hide();
                } else {
                    $(".snext").show();
                    $(".snoRecord").hide();

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

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


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