<?php
session_start();
include 'config.php';

if ($_SESSION['role'] != 'admin') {
    die("Akses ditolak");
}

$id = (int)$_GET['id'];

$data = mysqli_fetch_assoc(
    mysqli_query(
        $conn,
        "SELECT * FROM absensi WHERE id='$id'"
    )
);

if (!$data) {
    die("Data tidak ditemukan");
}

if (isset($_POST['update'])) {

    $tanggal = $_POST['tanggal'];
    $status = $_POST['status'];
    $jam_masuk = $_POST['jam_masuk'];
    $jam_pulang = $_POST['jam_pulang'];

    mysqli_query($conn, "
        UPDATE absensi
        SET
            tanggal='$tanggal',
            status='$status',
            jam_masuk='$jam_masuk',
            jam_pulang='$jam_pulang'
        WHERE id='$id'
    ");

    echo "
    <script>
        alert('Absensi berhasil diupdate');
        window.location='absensi_manual.php?id=".$data['user_id']."';
    </script>
    ";
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Edit Absensi</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container mt-4">

    <div class="card">

        <div class="card-header">
            Edit Absensi
        </div>

        <div class="card-body">

            <form method="post">

                <div class="mb-3">
                    <label>Tanggal</label>

                    <input type="date"
                           name="tanggal"
                           class="form-control"
                           value="<?= $data['tanggal']; ?>"
                           required>
                </div>

                <div class="mb-3">
                    <label>Status</label>

                    <select name="status"
                            class="form-control">

                        <option value="Hadir"
                        <?= $data['status']=='Hadir'?'selected':'' ?>>
                            Hadir
                        </option>

                        <option value="Sakit"
                        <?= $data['status']=='Sakit'?'selected':'' ?>>
                            Sakit
                        </option>

                        <option value="Izin"
                        <?= $data['status']=='Izin'?'selected':'' ?>>
                            Izin
                        </option>

                        <option value="Cuti"
                        <?= $data['status']=='Cuti'?'selected':'' ?>>
                            Cuti
                        </option>

                        <option value="Alpha"
                        <?= $data['status']=='Alpha'?'selected':'' ?>>
                            Alpha
                        </option>

                    </select>
                </div>

                <div class="mb-3">
                    <label>Jam Masuk</label>

                    <input type="time"
                           name="jam_masuk"
                           class="form-control"
                           value="<?= $data['jam_masuk']; ?>">
                </div>

                <div class="mb-3">
                    <label>Jam Pulang</label>

                    <input type="time"
                           name="jam_pulang"
                           class="form-control"
                           value="<?= $data['jam_pulang']; ?>">
                </div>

                <button type="submit"
                        name="update"
                        class="btn btn-success">

                    Update

                </button>

                <a href="absensi_manual.php?id=<?= $data['user_id']; ?>"
                   class="btn btn-secondary">

                    Kembali

                </a>

            </form>

        </div>

    </div>

</div>

</body>
</html>