<?php
session_start();
if (!isset($_SESSION['id_admin'])) {
    header('Location: login_admin.php');
    exit;
}

include 'includes/config.php';
include 'includes/header_admin.php';
include 'includes/sidebar_admin.php';

// Ambil semua aktiviti untuk dropdown
$aktiviti_sql = "SELECT id, nama_aktiviti, tarikh_mula, tarikh_tamat 
                 FROM aktiviti 
                 ORDER BY tarikh_mula DESC";
$aktiviti_result = mysqli_query($conn, $aktiviti_sql);

$aktiviti_id = isset($_GET['aktiviti_id']) ? intval($_GET['aktiviti_id']) : 0;
$pelajar_result = null;

if ($aktiviti_id > 0) {
    $sql_pelajar = "
        SELECT pen.id AS penyertaan_id, pel.nama, pel.no_ic, pen.status_kehadiran
        FROM penyertaan pen
        JOIN pelajar pel ON pen.pelajar_id = pel.id
        WHERE pen.aktiviti_id = $aktiviti_id
        ORDER BY pel.nama ASC
    ";
    $pelajar_result = mysqli_query($conn, $sql_pelajar);
}

// Proses simpan kehadiran
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['kehadiran'])) {
    foreach ($_POST['kehadiran'] as $penyertaan_id => $status) {
        $status = mysqli_real_escape_string($conn, $status);
        mysqli_query($conn, "UPDATE penyertaan SET status_kehadiran = '$status' WHERE id = $penyertaan_id");
    }
    echo "<script>alert('Kehadiran telah disimpan!'); window.location.href='pengurusan_kehadiran.php?aktiviti_id={$aktiviti_id}';</script>";
    exit;
}
?>

<div style="margin-left:200px; padding:80px 20px;">
    <h3>Pengurusan Kehadiran Pelajar</h3>

    <form method="GET" id="aktivitiForm">
        <label>Pilih Aktiviti:</label>
        <select name="aktiviti_id" class="form-control" style="width: 50%;" onchange="document.getElementById('aktivitiForm').submit();">
            <option value="">-- Sila Pilih Aktiviti --</option>
            <?php while ($aktiviti = mysqli_fetch_assoc($aktiviti_result)): ?>
                <option value="<?= $aktiviti['id'] ?>" <?= ($aktiviti_id == $aktiviti['id']) ? 'selected' : '' ?>>
                    <?= htmlspecialchars($aktiviti['nama_aktiviti']) ?> 
                    (<?= date('d/m/Y', strtotime($aktiviti['tarikh_mula'])) ?> - <?= date('d/m/Y', strtotime($aktiviti['tarikh_tamat'])) ?>)
                </option>
            <?php endwhile; ?>
        </select>
    </form>

    <?php if ($aktiviti_id > 0 && $pelajar_result && mysqli_num_rows($pelajar_result) > 0): ?>
        <form method="POST">
            <table class="table table-bordered table-striped mt-4">
                <thead class="table-dark">
                    <tr>
                        <th>Nama Pelajar</th>
                        <th>No. Kad Pengenalan</th>
                        <th>Status Kehadiran</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = mysqli_fetch_assoc($pelajar_result)): ?>
                        <tr>
                            <td><?= htmlspecialchars($row['nama']) ?></td>
                            <td><?= htmlspecialchars($row['no_ic']) ?></td>
                            <td>
                                <select name="kehadiran[<?= $row['penyertaan_id'] ?>]" class="form-control">
                                    <option value="Hadir" <?= ($row['status_kehadiran'] === 'Hadir') ? 'selected' : '' ?>>Hadir</option>
                                    <option value="Tidak Hadir" <?= ($row['status_kehadiran'] === 'Tidak Hadir') ? 'selected' : '' ?>>Tidak Hadir</option>
                                </select>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
            <button type="submit" class="btn btn-primary">Simpan Kehadiran</button>
        </form>
    <?php elseif ($aktiviti_id > 0): ?>
        <p class="mt-4">Tiada pelajar yang mendaftar untuk aktiviti ini.</p>
    <?php endif; ?>
</div>

<?php include 'includes/footer.php'; ?>
