<?php
$query12 = mysqli_query($conn, "SELECT * FROM config WHERE config_type_id = '5'");
while ($spec12 = mysqli_fetch_assoc($query12)) {
?>
    <section id="<?php echo $spec12["id"]; ?>" class="tab-panel">
        <?php include($_SERVER['DOCUMENT_ROOT'] . "/COMPONENTS/sub-pages/view-finished-product-warehouse.php"); ?>
    </section>
<?php } ?>

<div class="modals"></div>


<script>
    $(".modals").load(domain + "/COMPONENTS/sub-pages/modals.php");

    $(".addMoreItems").off().on("click", function() {
        var noOfColumns = $('.moreItems .col-md-12').length;
        $(".moreItems").append(
            '<div class="col-md-12 rowW" style="margin-top:0px;">' +
            '<div class="row">' +
            '<div class="col-md-12 mobilePad" style="padding-left:6px; padding-top:15px; text-align:right; padding-right:50px;"><a href="#" class="removeJobs" style="font-size:15px; color:red;"> <i class="fa fa-trash"></i> Remove</a></div>' +
            '<div class="col-md-12 mobilePad" style="padding-left:30px; padding-right:40px;"><div class="formfield"><select name="rawMaterial[]" class="rawMaterial" id="rawMaterial' + noOfColumns + '" required> </select><label alt="Raw Material" for="rawMaterial' + noOfColumns + '">Raw Material <span class="required">*</span></label></div></div><input type="text" name="category[]" id="category' + noOfColumns + '" placeholder="" style="display:none;">' +
            '<div class="col-md-12 mobilePad" style="padding-left:29px; padding-right:40px;"><div class="formfield"><input class="form-control" type="number" name="quantityNeeded[]" id="quantityNeeded' + noOfColumns + '" placeholder="" pattern="[0-9]+([.][0.00-9.00]+)" step="0.01" required><label alt="Quantity Needed" for="quantityNeeded' + noOfColumns + '" class="quantityNeeded">Quantity Needed <span class="required">*</span></label></div></div>' +
            '</div>' +
            '</div>'
        )

        $("#rawMaterial" + noOfColumns + "").empty();
        $("#rawMaterial" + noOfColumns + "").append('<option value="" selected></option>');
        $.get(domain_api + "/other-configs/configs.php", function(data) {
            var results = data;

            $.each(results.data.inventory_item_description, function(key, value) {
                $("#rawMaterial" + noOfColumns + "").append("<option value='" + value + "'>" + value + "</option>");
            });
        });


        // $("#unitofmeasurement" + noOfColumns + "").empty();
        // $("#unitofmeasurement" + noOfColumns + "").append('<option value="" selected></option>');
        // $.get(domain_api + "/other-configs/configs.php", function(data) {
        //     var results = data;

        //     $.each(results.data.unit_of_measurement, function(key, value) {
        //         $("#unitofmeasurement" + noOfColumns + "").append("<option value='" + value + "'>" + value + "</option>");
        //     });
        // });


        // $("#itemDescription" + noOfColumns + "").off().on("change", function() {
        //     var item = $(this).val();
        //     $.get(domain_api + "/other-configs/get-category.php?item=" + encodeURIComponent(item) + "", function(data) {
        //         var results = data;
        //         $.each(results.category[0], function(key, value) {
        //             $("#category" + noOfColumns + "").val(value);
        //         });
        //     })
        // });

    });


    $(".addProductionMix1").off().on("click", function() {
        var form_name = $(this).attr("data-form-name");
        var endpoint = $(this).attr("data-endpoint");
        $("#" + form_name).submit(function(e) {
            $.ajax({
                url: domain_api + endpoint,
                type: "POST",
                data: new FormData(this),
                contentType: false,
                processData: false,
                beforeSend: function() {
                    $(".addProductionMix1").attr("disabled", "disabled");
                    $(".submitLoader").html("<i class='fa fa-spinner fa-spin'></i>")
                },
                success: function(data) {
                    $(".submitLoader").html("Save");
                    if (data.code == 200) {
                        $("#" + form_name)[0].reset();
                        swal("Succes!", data.message, "success");
                        var table = $('#example').DataTable({
                            bDestroy: true,
                            responsive: true,
                            "ajax": {
                                "url": domain_api + "/production-mix/production-mix-list.php",
                                "dataSrc": "production_mix_list"
                            },
                            "columns": [{
                                    "data": "productionwarehouse"
                                },
                                {
                                    "data": "frametype"
                                },
                                {
                                    "data": "framesize"
                                },
                                {
                                    "data": "productionlevel"
                                },
                                {
                                    "data": null,
                                    render: function(data, type, row) {
                                        // if (data.status == "Pending") {
                                        return '<span style="font-size:15px;">' + data.TOTAL + '</span> items'
                                        // } else {
                                        //     return '<span style="color:green;">' + data.status + '</span>'
                                        // }
                                    }
                                },
                                {
                                    "data": null,
                                    render: function(data, type, row) {
                                        return '<div class="btn btnn btn-primary entryProfile" data-id="' + data.invoice_no + '" title="Profile"><i class="ii far fa-eye"></i></div>' +
                                            '<div class="btn btnn btn-danger action" data-id="' + data.invoice_no + '" data-url="/COMPONENTS/sub-pages/delete-grouped-stock.php" title="Delete" style="margin-left:5px; margin-right:5px;"><i class="ii fa fa-trash"></i></div>'

                                    }
                                }
                            ]
                        });

                        new $.fn.dataTable.FixedHeader(table);
                    } else {
                        swal("Oops!", data.message, "error");
                    }
                    $(".addProductionMix1").attr("disabled", false);
                },
                error: function(request, error) {
                    $(".submitLoader").html("Save");
                    swal("Oops!", "Saving was not successful, you should try again!", "error");
                },
            });
            e.preventDefault();
        });
        $(this).off("click");
    });
</script>