<?php
require_once('includes/load.php');
page_require_level(3); // Memastikan level akses sesuai

if (isset($_GET['id'])) {
    $std_time_id = (int)$_GET['id'];
    $std_time = find_by_id('std_time', $std_time_id);
    if (!$std_time) {
        $session->msg("e", "Data std time tidak ditemukan.");
        redirect('std_time_list.php');
    }
} else {
    $session->msg("e", "Parameter ID tidak ditemukan.");
    redirect('std_time_list.php');
}

if (isset($_POST['update_std_time'])) {
    $req_fields = ['time_filling', 'time_label', 'time_seal', 'time_packing', 'time_shrink', 'description'];
    validate_fields($req_fields);

    if (empty($errors)) {
        $time_filling = (float)$_POST['time_filling'];
        $time_label = (float)$_POST['time_label'];
        $time_seal = (float)$_POST['time_seal'];
        $time_packing = (float)$_POST['time_packing'];
        $time_shrink = (float)$_POST['time_shrink'];
        $description = $db->escape($_POST['description']);

        $sql  = "UPDATE std_time SET ";
        $sql .= "time_filling = '{$time_filling}', ";
        $sql .= "time_label = '{$time_label}', ";
        $sql .= "time_seal = '{$time_seal}', ";
        $sql .= "time_packing = '{$time_packing}', ";
        $sql .= "time_shrink = '{$time_shrink}', ";
        $sql .= "description = '{$description}' ";
        $sql .= "WHERE id = '{$std_time_id}' ";
        $sql .= "LIMIT 1";

        $result = $db->query($sql);
        if ($result && $db->affected_rows() === 1) {
            $session->msg('s', "Waktu standar telah diperbarui.");
            redirect('std_time_list.php', false);
        } else {
            $session->msg('d', 'Gagal memperbarui waktu standar.');
            redirect('std_time_list.php', false);
        }
    } else {
        $session->msg("e", $errors);
        redirect('edit_std_time.php?id=' . $std_time_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-6 col-md-offset-3">
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>
                    <span class="glyphicon glyphicon-pencil"></span>
                    <span>Edit Std Time</span>
                </strong>
            </div>
            <div class="panel-body">
                <form method="post" action="edit_std_time.php?id=<?php echo (int)$std_time_id; ?>">
                    <div class="form-group">
                        <label for="time_filling">Waktu Filling</label>
                        <input type="text" class="form-control" name="time_filling" value="<?php echo remove_junk($std_time['time_filling']); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="time_label">Waktu Label</label>
                        <input type="text" class="form-control" name="time_label" value="<?php echo remove_junk($std_time['time_label']); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="time_seal">Waktu Seal</label>
                        <input type="text" class="form-control" name="time_seal" value="<?php echo remove_junk($std_time['time_seal']); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="time_packing">Waktu Packing</label>
                        <input type="text" class="form-control" name="time_packing" value="<?php echo remove_junk($std_time['time_packing']); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="time_shrink">Waktu Shrink</label>
                        <input type="text" class="form-control" name="time_shrink" value="<?php echo remove_junk($std_time['time_shrink']); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="description">Deskripsi</label>
                        <textarea class="form-control" name="description"><?php echo remove_junk($std_time['description']); ?></textarea>
                    </div>
                    <button type="submit" name="update_std_time" class="btn btn-primary">Simpan</button>
                    <a href="std_time_list.php" class="btn btn-default">Batal</a>
                </form>
            </div>
        </div>
    </div>
</div>

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