$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $("body").on("focus", '.total', autoAddRows);
    $("body").on("keyup focus blur click", '.remove, .qty, .rate, .amount', itemCal);
    $("body").on("keyup focus blur change", '.swchperc, .krshperc, .stperc', calculateTotal);



    $('#addnew').click(function () {
        AddRows();
    });

    function autoAddRows() {
        if ($(this).closest("tr").is(":last-child")) {
            AddRows();
        }
    }

    $("#getchallan").click(function () {
        var fromdate = $(".fromdate").val(),
                todate = $(".todate").val(),
                vouchno = $(".vouchno").val(),
                party = $("#party").val();
        if (party != '' && fromdate != '0000-00-00' && todate != '0000-00-00') {
            $.post('invgarbage_Get_TranDtl.php', {fromdate: fromdate, todate: todate, party: party, vouchno: vouchno}, function (data) {
                $(".showchaldata").html(data);
            });
        } else {
            alert("Party Cannot Be Empty");
//            alert("Party / From Date / To Date Cannot Be Empty");
        }
    });
    function remove() {
        $(this).closest("tr").remove();
    }

    function AddRows() {
        var ctr = $('#items').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var detail = ".fetchpart_" + ctr;
        $.post('invgarbage_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            $('#items').val(ctr);
            $(detail).select2();
        });
    }

    function itemCal() {
        var tr = $(this).closest("tr"),
                val = 0, val1 = 0;

        var trqty = tr.find(".qty").val();
        var nos = tr.find(".rate").val();
        var total = trqty * nos;
        tr.find(".amount").val(total);

        $(".qty").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                val1 += parseFloat(this.value);
            }
        });
        var totq = parseFloat(val1);
        $('.totqty').val(totq);

        $(".amount").each(function () {
            if (!isNaN(this.value) && this.value.length != 0) {
                val += parseFloat(this.value);
            }
        });
        var tot = parseFloat(val);
        $('.subtotal').val(tot);
//        mainbCal();
        calculateTotal();
    }
    function calculateTotal() {
        var subtotal = $(".subtotal").val(),
                stperc = $(".stperc").val(),
                swchperc = $(".swchperc").val(),
                krshperc = $(".krshperc").val(),
                stamt = 0,
                swchamt = 0,
                krshamt = 0,
                nettotal = 0;

        stamt = stperc * subtotal / 100,
                swchamt = swchperc * subtotal / 100,
                krshamt = krshperc * subtotal / 100,
                nettotal = parseFloat(subtotal) + parseFloat(stamt) + parseFloat(swchamt) + parseFloat(krshamt);
        
        $(".stamt").val(stamt);
        $(".swchamt").val(swchamt);
        $(".krshamt").val(krshamt);
        $(".nettotal").val(nettotal);
    }

});