<?php
$page_title = 'Add BOM';
require_once('includes/load.php');
page_require_level(2);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Ambil data yang dikirimkan melalui formulir
    $parent_bom = $db->escape($_POST['parent_bom']);
    $child_bom = $db->escape($_POST['child_bom']);
    $qty = $db->escape($_POST['qty']);

    // Validasi data jika diperlukan

    // Insert data ke dalam database
    $insert_result = add_bom($parent_bom, $child_bom, $qty);

    if ($insert_result) {
        // BOM berhasil ditambahkan
        $session->msg('s', 'BOM added successfully.');
        redirect_to('view_bom.php'); // Redirect to BOM View page
    } else {
        // Handle error jika gagal menambahkan BOM
        $session->msg('d', 'Error adding BOM.');
    }
}

include_once('layouts/header.php');
?>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Add BOM</h3>
            </div>
            <div class="panel-body">
                <form action="add_bom.php" method="post">
                    <div class="form-group">
                        <label for="parent_bom">Parent BOM:</label>
                        <input type="text" class="form-control" name="parent_bom" id="parent_bom" required>
                        <div id="parent_suggestions" class="suggestions"></div> <!-- Tambahkan container untuk saran -->
                    </div>
                    <div class="form-group">
                        <label for="child_bom">Child BOM:</label>
                        <input type="text" class="form-control" name="child_bom" id="child_bom" required>
                        <div id="child_suggestions" class="suggestions"></div> <!-- Tambahkan container untuk saran -->
                    </div>
                    <div class="form-group">
                        <label for="qty">Quantity:</label>
                        <input type="number" step="0.01" class="form-control" name="qty" id="qty" required>
                    </div>
                    <!-- Tambahkan input lain jika diperlukan -->

                    <button type="submit" name="add_bom" class="btn btn-primary">Add BOM</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const parentBomInput = document.getElementById('parent_bom');
    const childBomInput = document.getElementById('child_bom');
    const parentSuggestionsContainer = document.getElementById('parent_suggestions');
    const childSuggestionsContainer = document.getElementById('child_suggestions');

    function fetchSuggestions(inputElement, suggestionsContainer) {
        const query = inputElement.value;
        if (query.length >= 2) {
            fetch(`autocomplete.php?query=${encodeURIComponent(query)}`)
                .then(response => response.json())
                .then(products => {
                    suggestionsContainer.innerHTML = '';

                    if (products.length > 0) {
                        products.forEach(product => {
                            const suggestionItem = document.createElement('div');
                            suggestionItem.classList.add('suggestion');
                            suggestionItem.textContent = `${product.part_number} - ${product.name}`;
                            suggestionItem.addEventListener('click', function () {
                                inputElement.value = product.part_number;
                                suggestionsContainer.innerHTML = '';
                            });
                            suggestionsContainer.appendChild(suggestionItem);
                        });
                    } else {
                        suggestionsContainer.innerHTML = '<div class="suggestion">No suggestions</div>';
                    }
                })
                .catch(error => console.error(error));
        } else {
            suggestionsContainer.innerHTML = '';
        }
    }

    parentBomInput.addEventListener('input', function () {
        fetchSuggestions(parentBomInput, parentSuggestionsContainer);
    });

    childBomInput.addEventListener('input', function () {
        fetchSuggestions(childBomInput, childSuggestionsContainer);
    });
});
</script>

<?php include_once('layouts/footer.php'); ?>
