$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $(".tbody").on("keyup change blur focus", ".qty, .rate, .amount", calDetail);

    //    $("body").on("change keyup blur", ".qty, .rate,.totbillamt,.discperc,.roundoff,.freightperc,.packing,.courier", caltran);
    $("body").on("blur ", ".rate", autoadd);
    //    $("body").on("click", "#submit ,#cancel", hide_but);

    function autoadd() {
        if ($(this).closest('tr').is(":last-child")) {
            AddRows();
        }
    }
  function reIndexInputs() {
        let index = 0;
        $('input:not([type=hidden]):not([readonly]), select, textarea, button')
            .filter(':visible')
            .each(function () {
                $(this).attr('data-index', index++);
            });
    }

    // Assign index
    let index = 0;

    $('#inputform')
        .find('input:not([type=hidden]):not([readonly]), select, textarea, button')
        .filter(':visible')
        .each(function () {
            $(this).attr('data-index', index++);
        });

    // Handle Enter key
    $('#inputform').on('keydown', 'input, select, textarea', function (event) {
        if (event.which == 13) {
            event.preventDefault();

            let $this = $(this);
            let index = parseInt($this.attr('data-index'));
            let $next = $('[data-index="' + (index + 1) + '"]');

            if ($next.length) {
                $next.focus();

                // Only select text for input/textarea
                if ($next.is('input, textarea')) {
                    $next.select();
                }
            } else {
                // Optional: submit form on last field
                $('#inputform').submit();
            }
        }
    });



    function calDetail() {
        var tr = $(this).closest("tr"),
            cartqty = tr.find(".cartqty").val(),
            qty = tr.find(".qty").val(),
            totqty = cartqty * qty,
            rate = tr.find(".rate").val(),
            amount = (rate * qty).toFixed(2);

        tr.find(".totqty").val(totqty);
        tr.find(".amount").val(amount);
        var totamt = 0,
            totalqty = 0;

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

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



    $('#addnew').click(function () {
        AddRows();
        //        caltran();
    });
    function remove() {
        $(this).closest("tr").remove();
        calDetail();
        //        caltran();
    }


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

            //            $(detail).select2();
            $(focusFld).focus();
            reIndexInputs();
            //            caltran();
        });
    }




});