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


    $("body").on("change blur", "#frombc, #tobc", checkDuplic);

    function checkDuplic() {
        var frmbc = $("#frombc").val(),
                tobc = $("#tobc").val();
        if (frmbc == tobc) {
            alert('From & To Cannot Be Same');
            $("#tobc").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(),
                orderAs = $(".orderAs").val(),
                data = {ssearch: ssearch, sfield: sfield, soperator: soperator, orderAs: orderAs,
                    rangeFrom: smrangeFrom, rangeTo: smrangeTo},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "contra_search.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();
//    });
    $(".sprev").click(function () {
        getSearchData('prev');
    });
    $(".snext").click(function () {
        getSearchData('next');
    });


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