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

    function setoff() {
        var val = 0;
        var $row = $(this).closest("tr");
        var msetoff = parseFloat($row.find('.setoff').val()),
                mbalamt = parseFloat($row.find('.balamt').val()),
                mbilamt = parseFloat($row.find('.billamt').val()),
                amtadj = parseFloat($row.find(".amtadj").val()),
                mbilldate = $row.find('.billdate').val(),
                mvouchdt = $('#vouchdt').val();
        if (isNaN(msetoff)) {
            msetoff = 0;
        }

        if (msetoff > mbalamt && mbalamt < 0) {
            $row.find('.setoff').val(0);
            $row.find('.setoff').focus();
            msetoff = 0;
            alert("SETOFF AMOUNT CANNOT BE GREATER THAN BALANCE AMOUNT");
            $("#save").attr("disabled", "disabled");

        }

        var balance = mbilamt - amtadj - msetoff;
        $row.find(".balamt").val(balance);

//        var remain = $("#remain").val();



        var mamt = $('#amount').val();

        /*********Total Part************/

        $(".setoff").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                val += parseFloat(this.value);
            }
        });

        if (val > mamt) {
            $row.find('.setoff').val(0);
            $row.find('.setoff').focus();
            alert("TOTAL SETOFF AMOUNT CANNOT BE GREATER THAN TOTAL AMOUNT");
            $("#save").attr("disabled", "disabled");
        } else {
            $("#totsetoff").val(val);
            $("#save").removeAttr("disabled");

//            var totdays   = showDays(mvouchdt, mbilldate);
            var totremain = mamt - val;

//            $row.find('.days').val(totdays);
            $("#remain").val(totremain);
        }
    }

    function billsett() {
        $('#data').empty();
        var accode = $('#party').val();
        var vouchno = $('#vouchno').val();
        var vouchdt = $('#vouchdt').val();
        var amount = $('#amount').val();

        $.get('billsett.php',
                {accode: accode,
                    vouchno: vouchno,
                    vouchdt: vouchdt,
                    amount: amount,
                    bank: "",
                    dbf: "discgive",
                    pvar: "R",
                    source: "DG"
                }, function (data) {
            $('#data').html(data);
            $('.billflag').val(1);
        });
    }

    $('.amount').blur(function () {
        $(".settotal").show();
        $("#settmodal").modal("show");
        billsett();
    });


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

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

                    $.each(response, function () {
                        values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                                "<td align='left'>" + this.vouchno + "</td>" +
                                "<td align='left'>" + this.name + "</td>" +
                                "<td align='left'>" + this.amount + " </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();
    });
    $(".sprev").click(function () {
        getSearchData('prev');
    });
    $(".snext").click(function () {
        getSearchData('next');
    });


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