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

include 'includes/header_pelajar.php';
include 'includes/sidebar_pelajar.php';
include 'includes/config.php';

$id_pelajar = (int) $_SESSION['id_pelajar'];

$sql = "SELECT * FROM aktiviti ORDER BY tarikh_mula DESC";
$result = mysqli_query($conn, $sql);

$sql_daftar = "SELECT aktiviti_id FROM penyertaan WHERE pelajar_id = $id_pelajar";
$result_daftar = mysqli_query($conn, $sql_daftar);
$aktiviti_saya = [];
if ($result_daftar) {
    while ($r = mysqli_fetch_assoc($result_daftar)) {
        $aktiviti_saya[] = (int)$r['aktiviti_id'];
    }
}

$pesan = isset($_GET['msg']) ? htmlspecialchars($_GET['msg'], ENT_QUOTES, 'UTF-8') : '';
?>
<style>
/* sama style sebelum ini */
.alert {
    padding: 12px 20px;
    margin-bottom: 20px;
    border-radius: 8px;
    font-weight: 500;
}
.alert-success { background-color: #d1e7dd; color: #0f5132; }
.alert-danger { background-color: #f8d7da; color: #842029; }
</style>

<div class="main-content">
    <h2 class="page-title">Senarai Aktiviti</h2>

    <?php if ($pesan): ?>
        <div class="alert <?php echo strpos($pesan, 'batal') !== false ? 'alert-danger' : 'alert-success'; ?>">
            <?php echo $pesan; ?>
        </div>
    <?php endif; ?>

    <div class="card-container">
        <table class="table table-bordered table-custom">
            <thead>
                <tr>
                    <th>Nama Aktiviti</th>
                    <th>Penganjur</th>
                    <th>Peringkat</th>
                    <th>Tempat</th>
                    <th>Tarikh Mula</th>
                    <th>Tarikh Tamat</th>
                    <th style="width: 160px;">Tindakan</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($result && mysqli_num_rows($result) > 0): ?>
                    <?php while ($row = mysqli_fetch_assoc($result)): ?>
                        <?php
                            $aid = (int) $row['id'];
                            $nama = htmlspecialchars($row['nama_aktiviti'], ENT_QUOTES, 'UTF-8');
                            $penganjur = htmlspecialchars($row['penganjur'], ENT_QUOTES, 'UTF-8');
                            $peringkat = htmlspecialchars($row['peringkat'], ENT_QUOTES, 'UTF-8');
                            $tempat = htmlspecialchars($row['tempat'], ENT_QUOTES, 'UTF-8');
                            $tarikh_mula = $row['tarikh_mula'] ? date("d/m/Y", strtotime($row['tarikh_mula'])) : '-';
                            $tarikh_tamat = $row['tarikh_tamat'] ? date("d/m/Y", strtotime($row['tarikh_tamat'])) : '-';
                        ?>
                        <tr>
                            <td><?php echo $nama; ?></td>
                            <td><?php echo $penganjur; ?></td>
                            <td><?php echo $peringkat; ?></td>
                            <td><?php echo $tempat; ?></td>
                            <td><?php echo $tarikh_mula; ?></td>
                            <td><?php echo $tarikh_tamat; ?></td>
                            <td>
                                <?php if (in_array($aid, $aktiviti_saya)): ?>
                                    <a href="batal_aktiviti.php?id=<?php echo $aid; ?>" class="btn-batal" onclick="return confirm('Batal penyertaan aktiviti ini?');">Batal</a>
                                <?php else: ?>
                                    <a href="daftar_aktiviti.php?id=<?php echo $aid; ?>" class="btn-daftar">Daftar</a>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="7" class="text-center">Tiada rekod aktiviti.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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