$(document).ready(function () {
    $("body").on("keyup", ".stsearch", getSearchData);
    $("body").on("click", ".stload", 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//////////////////////////////////////////////
    $(".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: "staff_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 align='left'>" + this.name + "</td>" +
                                "<td align='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  //////////////////////////////////////////////
});