<?php
$page_title = 'Create Paket Bundling';
require_once('includes/load.php');
page_require_level(15);

$products = find_all('products');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $bundling_code = $db->escape($_POST['bundling_code']);
    $date_active = $db->escape($_POST['date_active']);

    $errors = [];

    if (empty($_POST['part_number'])) {
        $errors[] = "Please enter at least one part number.";
    } else {
        foreach ($_POST['part_number'] as $key => $part_number) {
            $part_number = $db->escape($part_number);
            $quantity = (int)$_POST['quantity'][$key];

            $sql = "INSERT INTO paket_bundling (bundling_code, part_number, quantity, date_active) 
                    VALUES ('$bundling_code', '$part_number', '$quantity', '$date_active')";
            $result = $db->query($sql);

            if (!$result) {
                $errors[] = "Failed to add bundling for part number: $part_number";
            }
        }
    }

    if (empty($errors)) {
        $session->msg('s', 'Paket Bundling added successfully!');
        redirect('paket_bundling.php', false);
    } else {
        $session->msg('d', implode("<br>", $errors));
    }
}

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

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Create Paket Bundling</h3>
                <a href="list_bundling.php" class="btn btn-info pull-right">List Bundling</a>
            </div>
            <div class="panel-body">
                <form method="post" action="paket_bundling.php" class="clearfix">
                    <div class="form-group">
                        <label for="bundling_code">Bundling Code:</label>
                        <input type="text" class="form-control" name="bundling_code" required>
                    </div>
                    <div class="form-group">
                        <label for="date_active">Date Active:</label>
                        <input type="date" class="form-control" name="date_active" required>
                    </div>
                    <div id="part_numbers_container">
                        <!-- Dynamic part number entries will be added here -->
                    </div>
                    <button type="button" id="add_part_number" class="btn btn-secondary">Add Another Part Number</button>
                    <button type="submit" class="btn btn-primary">Create Bundling</button>
                </form>
            </div>
        </div>
    </div>
</div>

<style>
    .form-group {
        margin-bottom: 15px;
    }

    .form-row {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }
</style>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<script>
$(document).ready(function() {
    const addPartNumberButton = document.getElementById('add_part_number');
    const partNumbersContainer = document.getElementById('part_numbers_container');

    function addPartNumberEntry() {
        const newPartNumberEntry = document.createElement('div');
        newPartNumberEntry.classList.add('form-row', 'part_number_entry');
        newPartNumberEntry.innerHTML = `
            <div class="col">
                <label for="part_number">Part Number:</label>
                <input type="text" class="form-control" name="part_number[]" required>
            </div>
            <div class="col">
                <label for="quantity">Quantity:</label>
                <input type="number" class="form-control" name="quantity[]" step="1" required>
            </div>
        `;
        partNumbersContainer.appendChild(newPartNumberEntry);
    }

    addPartNumberButton.addEventListener('click', addPartNumberEntry);
});
</script>

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