$(document).ready(function () {
    $("body").on("click", ".clickable-row", clickRow);
    $(".searchModal").on("click", ".clickable-row", clickRow);
    $("body").on("keyup", ".ssearch", getSearchData);
    $("body").on("change", ".soperator", getSearchData);
    $("body").on("click", ".sload", getSearchData);
    $("body").on("click", "#search", getSearchData);

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    function clickRow() {
        window.document.location = $(this).data("href");
    }
    $(".snoRecord").hide();
    $(".sloader").hide();

    var imrangeFrom = 0,
            imrangeTo = 40;

    $(".ssearch").focus();
//    $(".ssearch").val("");
//    $(".sfield").val("1");
    getSearchData();

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


                    $.each(response, function () {
                        values += "<tr class=''>" +
                                "<td align='left'>" + this.manualno + "</td>" +
                                "<td align='left'>" + this.visatype + "</td>" +
                                "<td align='left'>" + this.partyname + "</td>" +
                                "<td align='left'>" + this.passport + "</td>" +
                                "<td align='left'>" + this.sponsor + "</td>" +
                                "<td align='left'>" + this.broker + "</td>" +
                                "<td align='left'>" + this.idno + "</td>" +
                                "<td align='left'>" + this.visano + "</td>" + 
                                "<td align='left'>" + this.visaqty + "</td>" + 
                                "<td align='left'>" + this.vouchno + "</td>" +
                                "<td align='left'>" + this.vouchdt + "</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 () {
        $(".searchModal").on('shown.bs.modal', function () {
            $(".ssearch").focus();
            $(".ssearch").val("");
            $(".sfield").val("8");
            getSearchData();
        });

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


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