$(document).ready(function () {
    $('body').on('blur', '.setoff', setoff);
    $('body').on('keyup', '#recd, #discount, #dclaim', calculateRow);

    function showDays(firstDate, secondDate) {
        var date1 = firstDate;
        var date2 = secondDate.split("-").reverse().join("-");
        var mfirstday = new Date(date1);
        var mlastday = new Date(date2);
        var distance
        if (mfirstday > mlastday) {
            distance = mfirstday.getTime() - mlastday.getTime();
            distance = Math.ceil(distance / 1000 / 60 / 60 / 24);
        } else {
            distance = 0;
        }
        return distance;
    }

    $(".settotal").hide();

    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;
        }

        var newval = (mbilamt - amtadj).toFixed(2);
//        alert(newval);
        if (msetoff > newval) {
            $row.find('.setoff').val(0);
//            $row.find('.setoff').focus();
            msetoff = 0;
            alert("SETOFF AMOUNT CANNOT BE GREATER THAN BALANCE AMOUNT");
            $("#save").attr("disabled", "disabled");
            parseFloat($row.find('.balamt').val(mbalamt.toFixed(2)));

        } else {

            var balance = (mbilamt - amtadj - msetoff).toFixed(2);
            $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);
//                            alert(totdays);

                var totremain = mamt - val;

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


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

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

        });
    }

    $('#billsett').click(function () {
        billsett();
    });

    $('#amount').blur(function () {
//        var billwise = $(".billwise").val();
//
//        if (billwise == '1') {
        $(".billflag").val("1");

        $(".settotal").show();
        $("#settmodal").modal("show");
        $("#settmodal").on('shown.bs.modal', function () {
            $("#setoff_1").focus();
        });
        billsett();
//            $("#setoff_1").focus();
//        }
    });

    function calculateRow() {
        var fintot = 0;
        var subtot = parseFloat($('#recd').val());
        var discount = parseFloat($('#discount').val());
        var claim = parseFloat($('#dclaim').val());

        fintot = subtot + discount + claim;

        if (isNaN(fintot)) {
            $('#amount').val("0");
        } else {
            $('#amount').val(fintot.toFixed(1));
        }
    }
});