$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
//    $("body").on("focus ", '.rqty', autoaddraw);
//    $("body").on("focus ", '.approxkg', autoaddrel);
//    $("body").on("focus ", '.pqty', autoaddpack);
    $("body").on("keyup blur change", ".rqty, .rcost, .ramount", calDetailR);
    $("body").on("change", ".rawmat", getrcost);
    $("body").on("keyup", ".rqty", cal100);
    $("body").on("change", ".packing", getpcost);
    $("body").on("change", ".prodcode", getprod_dtl);
    $("body").on("keyup blur change", ".pqty, .pcost, .pamount", calDetailP);
    $(".tbody").on("click", ".pack", getpacking);
    $("body").on("click", ".psave", savepacking);

    function savepacking(event) {
        event.preventDefault();
        var ctr = 1;

        $('.tbody4').find('tr').each(function () {
            var row = $(this),
                    code = $('.code').val(),
                    packing = row.find('.packing').val(),
                    unit = row.find('.unit').val(),
                    prodcode = row.find('.prodcode').val(),
                    pqty = row.find('.pqty').val();

            if (prodcode != '' && packing != '') {
                if (ctr == 1) {
                    $.post('del_finprodpack.php', {prodcode: prodcode, code: code});
                }

                $.post('save_finprodpack.php', {prodcode: prodcode, code: code, srno: ctr, packing: packing, unit: unit, qty: pqty}, function (data) {
//                    alert(data);
                });
                ctr++;
                setTimeout(3000);
            }
        });
        alert("Success");

    }

    function getpacking(event) {
        event.preventDefault();
        var row = $(this).closest('tr'),
                code = $('.code').val(),
                prodcode = row.find('.prodcode').val();
        if (prodcode != '') {
            $.post('get_finprodpack.php', {prodcode: prodcode, code: code}, function (data) {
                $('.divpack').css("display", "");
                $('.tbody4').empty();
                $('.tbody4').html(data);
                $('.tbody4').find('.packing').focus();
            });
        }

    }

    $("#fsubmit").submit(function (event) {
        var totperc = 0;
        $('.tbody3').find('tr').each(function () {
            var row = $(this),
                    qtyperc = row.find('.rqty').val();
            totperc += parseFloat(qtyperc);
        });
        if (totperc != 100) {
            alert('Sum Of Quantity Is Not 100% In Manufacturing Detail !!');
            event.preventDefault();
        }

    });

    function cal100() {
        var totqty = 0;

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

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

        $.post('getprod_dtl.php', {prodcode: prodcode}, function (data) {
            tr.find('.prodname').val(data.name);
            tr.find('.netwt').val(data.netwt);
        }, 'json');
    }

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

        $.post('getraw_cost.php', {rawmat: rawmat}, function (data) {
            tr.find('.rcost').val(data);
        });
    }

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

        $.post('getpack_cost.php', {packing: packing}, function (data) {
            tr.find('.pcost').val(data);
        });
    }

    function calDetailR() {
        var tr = $(this).closest("tr"),
                qty = tr.find(".rqty").val(),
                cost = tr.find(".rcost").val(),
                amount = parseFloat(cost) * parseFloat(qty);

        tr.find(".ramount").val(amount);

    }

    function calDetailP() {
        var tr = $(this).closest("tr"),
                qty = tr.find(".pqty").val(),
                cost = tr.find(".pcost").val(),
                amount = parseFloat(cost) * parseFloat(qty);
        tr.find(".pamount").val(amount);
    }

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

    function autoaddpack() {
        if ($(this).closest('tr').is(":last-child")) {
            AddRows2();
        }
    }

    function autoaddrel() {
        if ($(this).closest('tr').is(":last-child")) {
            AddRows3();
        }
    }

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


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

    function AddRows3() {
        var ctr = $('#items').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }

        $.post('finrelprod_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            $('#items').val(ctr);
        });
    }

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

//
//    $('.ninth').focus(function () {
//        if ($(this).closest("tr").is(":last-child")) {
//            AddRows2();
//        }
//    });

    function AddRows2() {
        var ctr = $('#items4').val();
        var prodcode = $(".tbody4 tr:last").find(".prodcode").val();
        var srno = $(".tbody4 tr:last").find(".srno4").val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
//        var detail = ".fetchpart_" + ctr;
        $.post('finprodpack_add.php', {ctr: ctr, hide: hide_check, prodcode: prodcode, srno: srno}, function (data) {
            $(data).appendTo('.tbody4');
            ctr++;
            $('#items4').val(ctr);
        });
    }

    $('#addnew4').click(function () {
        AddRows2();
    });

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


});