<?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';

// Dapatkan data penyertaan dengan join pelajar dan aktiviti
$sql = "SELECT p.id, pel.nama AS nama_pelajar, a.nama_aktiviti, a.tarikh_mula, a.tarikh_tamat
        FROM penyertaan p
        JOIN pelajar pel ON p.pelajar_id = pel.id
        JOIN aktiviti a ON p.aktiviti_id = a.id
        ORDER BY a.tarikh_mula DESC";

$result = $conn->query($sql);
?>

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

    <a href="tambah_penyertaan.php" class="btn btn-success mb-3"><i class="bi bi-plus-circle"></i> Tambah Penyertaan</a>

    <table class="table table-bordered table-striped">
        <thead class="table-dark">
            <tr>
                <th>Nama Pelajar</th>
                <th>Nama Aktiviti</th>
                <th>Tarikh Mula</th>
                <th>Tarikh Tamat</th>
                <th>Tindakan</th>
            </tr>
        </thead>
        <tbody>
            <?php if ($result && $result->num_rows > 0): ?>
                <?php while ($row = $result->fetch_assoc()): ?>
                    <tr>
                        <td><?= htmlspecialchars($row['nama_pelajar']) ?></td>
                        <td><?= htmlspecialchars($row['nama_aktiviti']) ?></td>
                        <td><?= date('d/m/Y', strtotime($row['tarikh_mula'])) ?></td>
                        <td><?= date('d/m/Y', strtotime($row['tarikh_tamat'])) ?></td>
                        <td>
                            <a href="kemaskini_penyertaan.php?id=<?= $row['id'] ?>" class="btn btn-primary btn-sm">
                                <i class="bi bi-pencil-square"></i> Kemaskini
                            </a>
                            <a href="padam_penyertaan.php?id=<?= $row['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Padam penyertaan ini?')">
                                <i class="bi bi-trash"></i> Padam
                            </a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr><td colspan="5" class="text-center">Tiada rekod penyertaan.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

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