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

// Cek apakah ID dikirim melalui URL
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id === 0) {
    redirect('production_schedule.php', false);
}

// Ambil data jadwal produksi berdasarkan ID
$schedule = find_by_id('production_schedule', $id);
if (!$schedule) {
    $session->msg('d', 'Missing production schedule.');
    redirect('production_schedule.php', false);
}

// Inisialisasi variabel
$errors = array();
$no_order = remove_junk($schedule['no_order']);
$client = remove_junk($schedule['client']);
$part_number = remove_junk($schedule['part_number']);
$quantity_order = (int)$schedule['quantity_order'];
$delivery_date = date('Y-m-d', strtotime($schedule['delivery_date']));
$input_date = date('Y-m-d', strtotime($schedule['input_date']));

// Proses formulir saat disubmit
if (isset($_POST['update'])) {
    $no_order = remove_junk($db->escape($_POST['no_order']));
    $client = remove_junk($db->escape($_POST['client']));
    $part_number = remove_junk($db->escape($_POST['part_number']));
    $quantity_order = (int)$db->escape($_POST['quantity_order']);
    $delivery_date = $db->escape($_POST['delivery_date']);
    $input_date = $db->escape($_POST['input_date']);
    
    if (empty($no_order) || empty($client) || empty($part_number) || empty($quantity_order) || empty($delivery_date)) {
        $session->msg('d', 'Please fill all required fields.');
        redirect("edit_production_schedule.php?id={$id}", false);
    } else {
        $query = "UPDATE production_schedule SET no_order='{$no_order}', client='{$client}', part_number='{$part_number}', quantity_order='{$quantity_order}', delivery_date='{$delivery_date}', input_date='{$input_date}' WHERE id='{$id}'";
        if ($db->query($query)) {
            $session->msg('s', 'Production schedule updated.');
            redirect('production_schedule.php', false);
        } else {
            $session->msg('d', 'Sorry, update failed.');
            redirect("edit_production_schedule.php?id={$id}", false);
        }
    }
}
?>

<?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">Edit Production Schedule</h3>
            </div>
            <div class="panel-body">
                <form method="post" action="edit_production_schedule.php?id=<?php echo (int)$id; ?>">
                    <div class="form-group">
                        <label for="no_order">Order Number:</label>
                        <input type="text" class="form-control" name="no_order" value="<?php echo htmlspecialchars($no_order); ?>">
                    </div>
                    <div class="form-group">
                        <label for="client">Client:</label>
                        <input type="text" class="form-control" name="client" value="<?php echo htmlspecialchars($client); ?>">
                    </div>
                    <div class="form-group">
                        <label for="part_number">Part Number:</label>
                        <input type="text" class="form-control" name="part_number" value="<?php echo htmlspecialchars($part_number); ?>">
                    </div>
                    <div class="form-group">
                        <label for="quantity_order">Quantity Ordered:</label>
                        <input type="number" class="form-control" name="quantity_order" value="<?php echo (int)$quantity_order; ?>">
                    </div>
                    <div class="form-group">
                        <label for="delivery_date">Delivery Date:</label>
                        <input type="date" class="form-control" name="delivery_date" value="<?php echo htmlspecialchars($delivery_date); ?>">
                    </div>
                    <div class="form-group">
                        <label for="input_date">Input Date:</label>
                        <input type="date" class="form-control" name="input_date" value="<?php echo htmlspecialchars($input_date); ?>">
                    </div>
                    <button type="submit" name="update" class="btn btn-primary">Update Schedule</button>
                </form>
            </div>
        </div>
    </div>
</div>

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