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


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

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

                    $.each(response, function () {
                        values += "<tr >" +
                                "<td align='left' class='clickable-row' data-href='" + this.action + "'>" + this.accode + "</td>" +
                                "<td align='left' class='clickable-row' data-href='" + this.action + "'>" + this.name + "</td>" +
                                "<td align='left'>" + this.add1 + " " + this.add2 + " " + this.add3 + "</td>" +
                                "<td align='left'>" + this.age + "</td>" +
                                "<td align='left'>" + this.dob + "</td>" +
                                "<td align='left'>" + this.mobile + "</td>" +
                                "<td align='left'>" + this.email + "</td>" +
                                "<td align='left'>" + this.gender + "</td>" +
                                "<td align='right'>" + this.balance + "</td>" +
                                "<td align='left'><a class='btn text-center form-control bookapp' href='bookappoint.php?accode=" + this.accode + "'>Select</a></td>" +
                                "</tr>";
                    });
                    $(".sloader").hide();
                    $(".sTbody").empty();
                    $(".sTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
                $(".ssearch").focus();

            },
            error: function (request, textStatus, errorThrown) {

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

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


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