$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $("body").on("keyup change blur focus", ".qty, .freeqty, .tcsamt, .amount,.disperc,.discamt,.vat, .vatadd,.licenfee, .dealcomm,.othchgs", calDetail);
    $("body").on("blur", '.prodcode', getProd);
    $("body").on("change", '.prodtype', chkProd);
    $("body").on("keyup change blur focus", '.discamt', getTds);

    function getTds() {
        var totTds = 0;
        $(".tbody").find('tr').each(function () {
            var row = $(this);
            var amount = row.find('.amount').val();
            var discount = row.find('.discamt').val();
            totTds += parseFloat(amount) - parseFloat(discount);
        });
        var finaltds = totTds * 0.001;
        $('.tdsamt').val(finaltds);
    }

    function chkProd() {
        var tr = $(this).closest("tr"),
                prodtype = tr.find(".prodtype").val();

        if (prodtype == "P") {
            tr.find(".prodtd").show();
            tr.find(".parttd").hide();
            tr.find(".prodcode").removeAttr("readonly", true);
            tr.find(".qty").removeAttr("readonly", true);
            tr.find(".freeqty").removeAttr("readonly", true);
            tr.find(".totalqty").removeAttr("readonly", true);
            tr.find(".disperc").attr("readonly", true);
            tr.find(".discamt").attr("readonly", true);
            tr.find(".disperc").attr("tabindex", "-1");
            tr.find(".discamt").attr("tabindex", "-1");
        } else {
            tr.find(".prodtd").hide();
            tr.find(".parttd").show();
            tr.find(".disperc").removeAttr("readonly", true);
            tr.find(".discamt").removeAttr("readonly", true);
            tr.find(".prodcode").attr("readonly", true);
            tr.find(".prodcode").attr("tabindex", "-1");
            tr.find(".qty").attr("readonly", true);
            tr.find(".qty").attr("tabindex", "-1");
            tr.find(".freeqty").attr("readonly", true);
            tr.find(".freeqty").attr("tabindex", "-1");
            tr.find(".totalqty").attr("readonly", true);
            tr.find(".totalqty").attr("tabindex", "-1");
        }
    }

    function getProd() {
        var tr = $(this).closest("tr"),
                prodtype = tr.find(".prodtype").val(),
                prodcode = tr.find(".prodcode").val(),
                data = {prodcode: prodcode};
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "getproddetail.php",
            data: data,
            success: function (response) {
                if (response != '') {
                    $.each(response, function () {
                        var success = this.success;
                        if (success == false) {
                            alert("No Product Found");
                            tr.find(".prodcode").val("");
                            tr.find(".product").val("");
                            tr.find(".unit").val("");
                            tr.find(".prodcode").focus();
                        } else {
                            if (prodtype == "P") {

                                tr.find(".particulars").val(prodcode).trigger("change");
                            } else {

                                tr.find(".particulars").val(this.name);
                            }

                            tr.find(".unit").val(this.unit);
                            tr.find(".hsncode").val(this.hsncode);
                            tr.find(".rate").val(this.rate);
                        }
                    });
                } else {
                    alert('No Record Found for this Prodcode');
                    $('.prodcode').focus();
                }

            }
        });
    }


    function calDetail() {
        var tr = $(this).closest("tr"),
                qty = tr.find(".qty").val(),
                freeqty = tr.find(".freeqty").val(),
                totqty = parseFloat(qty) + parseFloat(freeqty),
                amount = tr.find(".amount").val(),
                tcsamt = tr.find(".tcsamt").val(),
                discperc = tr.find(".disperc").val(),
                licenfee = $(".licenfee").val(),
                othchgs = $(".othchgs").val(),
                dealcomm = $(".dealcomm").val(),
                discamt = 0,
                vat = tr.find(".vat").val(),
                vatadd = tr.find(".vatadd").val(),
                total = 0;

        if (discperc > 0) {
            discamt = ((discperc * amount) / 100).toFixed(2);
        } else {
            discamt = tr.find(".discamt").val();
        }

        total = parseFloat(tcsamt) + parseFloat(amount) - parseFloat(discamt) + parseFloat(vat) + parseFloat(vatadd);

        tr.find(".totalqty").val(totqty);
        tr.find(".amount").val(amount);
        tr.find(".tcsamt").val(tcsamt);
        tr.find(".discamt").val(discamt);
        tr.find(".total").val(total);
        var totamt = 0,
                totdisc = 0,
                totvat = 0,
                totvatadd = 0,
                tottcs = 0,
                totmat = 0,
                totprod = 0,
                tottotal = 0,
                totbal = 0,
                fintot = 0,
                otadv = 0;

        $(".tbody").find('tr').each(function () {
            var row = $(this);
            var prodtype = row.find('.prodtype').val();
            var amount = row.find('.amount').val();
            if (prodtype == "P") {
                totprod += parseFloat(amount);
            } else {
                totmat += parseFloat(amount);
            }
        });

        $(".matamt").val(totprod.toFixed(2));
        $(".labamt").val(totmat.toFixed(2));
        $(".discamt").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                totdisc += parseFloat(this.value);
            }
        });
        $(".discount").val(totdisc.toFixed(2));
        $(".vat").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                totvat += parseFloat(this.value);
            }
        });
        $(".totvat").val(totvat.toFixed(2));

        $(".vatadd").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                totvatadd += parseFloat(this.value);
            }
        });
        $(".totvatadd").val(totvatadd.toFixed(2));

        $(".amount").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                tottotal += parseFloat(this.value);
            }
        });
        $(".subtot").val(tottotal);
        $(".tcsamt").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                tottcs += parseFloat(this.value);
            }
        });
        $(".tottcs").val(tottcs);

        var fintot1 = parseFloat(tottotal) - parseFloat(totdisc) + parseFloat(tottcs) + parseFloat(totvat) + parseFloat(totvatadd) + parseFloat(licenfee) + parseFloat(dealcomm) + parseFloat(othchgs);
        fintot = Math.round(fintot1);
        $(".billamt").val(fintot);
//        $(".tottotal").val(tottotal);
    }


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

    function remove() {
        $(this).closest("tr").remove();
        calDetail();
        caltran();
        mainCal();
    }

    function AddRows() {
        var ctr = $('#items').val();
        var prodtype = $('.prodtype').val();
        var addflag = $('.addflag').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var focusFld = ".focusFld_" + ctr;
        ctr++;
        $('#items').val(ctr);
        $.post('purfuel_add.php', {ctr: ctr, hide: hide_check, prodtype: prodtype, addflag: addflag}, function (data) {
            $(data).appendTo('.tbody');

            $(focusFld).focus();
        });
    }
});