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



    $("body").on("change blur", "#fromdept, #todept", checkDuplic);

    function checkDuplic() {
        var fromdept = $("#fromdept").val(),
                todept = $("#todept").val();
        if (fromdept == todept) {
            alert('From & To Cannot Be Same');
            $("#todept").select2("open");

        }
    }

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

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

    var smrangeFrom = 0,
            smrangeTo = 40;

    function getSearchData(pagination) {

        $(".sloader").show();

        if (pagination == 'prev') {
            if (smrangeFrom > 0) {
                smrangeFrom = (smrangeFrom - 40);
                smrangeTo = (smrangeTo - 40);
            }
        }
        if (pagination == 'next') {
            smrangeFrom = (smrangeFrom + 40);
            smrangeTo = (smrangeTo + 40);
        }

        var ssearch = $(".ssearch").val(),
                sfield = $(".sfield").val(),
                soperator = $(".soperator").val(),
                data = {ssearch: ssearch, sfield: sfield, soperator: soperator,
                    rangeFrom: smrangeFrom, rangeTo: smrangeTo},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "stktrnrg.php",
            data: data,
            success: function (response) {
                $.each(response, function () {
                    values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                            "<td align='left'>" + this.vouchno + "</td>" +
                            "<td align='left'>" + this.vouchdt + "</td>" +
                            "<td align='left'>" + this.from + "</td>" +
                            "<td align='left'>" + this.to + "</td>" +
                            "<td align='left'>" + this.chqno + "</td>" +
                            "<td align='right'>" + this.amount + "</td>" +
                            "<td align='left'>" + this.narr1 + "</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();
    });
//    $(".ssearch").change(function () {
//        getSearchData();
//    });
    $(".sprevious").click(function () {
        getSearchData('prev');
    });
    $(".snext").click(function () {
        getSearchData('next');
    });


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