<?php
$page_title = 'Add Production Schedule';
require_once('includes/load.php');
page_require_level(3); // Pastikan level pengguna yang diperlukan

// Variabel untuk menyimpan pesan dan status
$msg = '';
$success = false;

// Proses form ketika disubmit
if (isset($_POST['add_schedule'])) {
    $no_order = $db->escape($_POST['no_order']);
    $client = $db->escape($_POST['client']);
    $part_number = $db->escape($_POST['part_number']);
    $quantity_order = (int)$db->escape($_POST['quantity_order']);
    $delivery_date = $db->escape($_POST['delivery_date']);
    
    // Validasi input
    if (empty($no_order) || empty($client) || empty($part_number) || empty($quantity_order) || empty($delivery_date)) {
        $msg = "Please fill out all fields.";
    } else {
        // Query untuk menyimpan data jadwal produksi
        $query = "INSERT INTO production_schedule (no_order, client, part_number, quantity_order, delivery_date) VALUES ('{$no_order}', '{$client}', '{$part_number}', {$quantity_order}, '{$delivery_date}')";
        if ($db->query($query)) {
            $msg = "Production schedule added successfully.";
            $success = true;
        } else {
            $msg = "Failed to add production schedule.";
        }
    }
}
?>

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

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Add Production Schedule</h3>
            </div>
            <div class="panel-body">
                <form method="post" action="add_production_schedule.php">
                    <div class="form-group">
                        <label for="no_order">Order Number:</label>
                        <input type="text" class="form-control" id="no_order" name="no_order" required>
                    </div>
                    <div class="form-group">
                        <label for="client">Client:</label>
                        <input type="text" class="form-control" id="client" name="client" required>
                    </div>
                    <div class="form-group">
                        <label for="part_number">Part Number:</label>
                        <input type="text" class="form-control" id="part_number" name="part_number" required>
                    </div>
                    <div class="form-group">
                        <label for="quantity_order">Quantity Ordered:</label>
                        <input type="number" class="form-control" id="quantity_order" name="quantity_order" required>
                    </div>
                    <div class="form-group">
                        <label for="delivery_date">Delivery Date:</label>
                        <input type="date" class="form-control" id="delivery_date" name="delivery_date" required>
                    </div>
                    <button type="submit" name="add_schedule" class="btn btn-primary">Add Schedule</button>
                    <a href="production_schedule.php" class="btn btn-default">Back to List</a>
                </form>
            </div>
        </div>
    </div>
</div>

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