<?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 a.* 
        FROM aktiviti a
        INNER JOIN penyertaan p ON a.id = p.aktiviti_id
        WHERE p.pelajar_id = $id_pelajar
        ORDER BY a.tarikh_mula DESC";
$result = mysqli_query($conn, $sql);
?>

<style>
.main-content {
    margin-left: 180px;
    margin-top: 100px;
    padding: 30px 40px;
    background-color: #f8f9fc;
    min-height: calc(100vh - 100px);
    font-family: 'Rubik', sans-serif;
}

.page-title {
    font-size: 32px;
    font-weight: 700;
    color: #3b0a77;
    margin-bottom: 20px;
}

.card-container {
    background: white;
    padding: 25px 30px;
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.08);
}

.table-custom {
    width: 100%;
    border-collapse: separate !important;
    border-spacing: 0 8px !important;
    font-size: 15px;
}

.table-custom thead tr {
    background-color: #3b0a77;
    color: white;
}

.table-custom th,
.table-custom td {
    padding: 12px 18px;
    vertical-align: middle !important;
}

.table-custom tbody tr {
    background-color: white;
    box-shadow: 0 2px 6px rgba(0,0,0,0.04);
    transition: transform 0.1s ease;
}

.table-custom tbody tr:hover {
    transform: translateY(-2px);
}

.btn-secondary { 
    background-color: #6c757d; 
    color: white; 
    margin-bottom: 15px; 
    padding: 6px 12px; 
    border-radius: 6px; 
    font-weight: 600; 
    text-decoration: none; 
}
.btn-secondary:hover { background-color: #5a6268; }
.text-center { text-align: center; color: #999; }
</style>

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

    <!-- Butang Kembali ke Dashboard -->
    <a href="dashboard_pelajar.php" class="btn btn-secondary mb-3">
        <i class="bi bi-arrow-left-circle"></i> Kembali ke Dashboard
    </a>

    <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>
                </tr>
            </thead>
            <tbody>
                <?php if ($result && mysqli_num_rows($result) > 0): ?>
                    <?php while ($row = mysqli_fetch_assoc($result)): ?>
                        <tr>
                            <td><?= htmlspecialchars($row['nama_aktiviti'], ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($row['penganjur'], ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($row['peringkat'], ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($row['tempat'], ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= date("d/m/Y", strtotime($row['tarikh_mula'])) ?></td>
                            <td><?= date("d/m/Y", strtotime($row['tarikh_tamat'])) ?></td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="6" class="text-center">Anda belum mendaftar sebarang aktiviti.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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