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

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

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

$user = mysqli_fetch_assoc(
    mysqli_query(
        $conn,
        "SELECT * FROM users WHERE id='$id_user'"
    )
);

if (!$user) {
    die("User tidak ditemukan");
}



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

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

    $cek = mysqli_query($conn,"
        SELECT id
        FROM absensi
        WHERE user_id='$id_user'
        AND tanggal='$tanggal'
    ");

    if(mysqli_num_rows($cek) > 0){

        echo "
        <div class='alert alert-danger'>
            Data tanggal tersebut sudah ada
        </div>
        ";

    } else {

        mysqli_query($conn,"
            INSERT INTO absensi
            (
                user_id,
                tanggal,
                jam_masuk,
                jam_pulang,
                status
            )
            VALUES
            (
                '$id_user',
                '$tanggal',
                '$jamMasuk',
                '$jamPulang',
                '$status'
            )
        ");

        echo "
        <script>
            alert('Absensi berhasil ditambahkan');
            location.href='absensi_manual.php?id=$id_user';
        </script>
        ";
    }
}
?>



<h3>
Kelola Absensi :
<?= $user['nama']; ?>
</h3>

<form method="post">

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

        <input type="date"
               name="tanggal"
               class="form-control"
               required>
    </div>

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

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

            <option value="Hadir">Hadir</option>
            <option value="Sakit">Sakit</option>
            <option value="Izin">Izin</option>
            <option value="Cuti">Cuti</option>
            <option value="Alpha">Alpha</option>

        </select>
    </div>

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

        <input type="time"
               name="jam_masuk"
               class="form-control">
    </div>

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

        <input type="time"
               name="jam_pulang"
               class="form-control">
    </div>

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

        Simpan

    </button>

</form>

<!-- LETAK LANGKAH 6 DIMULAI DARI SINI -->
<hr>

<h4>Riwayat Absensi</h4>

<?php
$data = mysqli_query($conn,"
    SELECT *
    FROM absensi
    WHERE user_id='$id_user'
    ORDER BY tanggal DESC
");
?>

<table class="table table-bordered">

<tr>
    <th>Tanggal</th>
    <th>Status</th>
    <th>Masuk</th>
    <th>Pulang</th>
    <th>Aksi</th>
</tr>

<?php while($r = mysqli_fetch_assoc($data)){ ?>

<tr>

    <td><?= $r['tanggal']; ?></td>
    <td><?= $r['status']; ?></td>
    <td><?= $r['jam_masuk']; ?></td>
    <td><?= $r['jam_pulang']; ?></td>

    <td>
        <a href="edit_absensi.php?id=<?= $r['id']; ?>"
           class="btn btn-warning btn-sm">
            Edit
        </a>

        <a href="hapus_absensi.php?id=<?= $r['id']; ?>&user=<?= $id_user; ?>"
           class="btn btn-danger btn-sm"
           onclick="return confirm('Hapus absensi ini?')">
            Hapus
        </a>
    </td>

</tr>

<?php } ?>

</table>