<?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 = $_SESSION['id_pelajar'];

// Maklumat pelajar
$sql = "SELECT * FROM pelajar WHERE id = $id_pelajar LIMIT 1";
$result = mysqli_query($conn, $sql);
$pelajar = mysqli_fetch_assoc($result);
$gambar_url = !empty($pelajar['gambar']) ? 'uploads/' . $pelajar['gambar'] : 'images/default-avatar.png';

// Aktiviti terbaru/akan datang
$aktiviti_terbaru = $conn->query("SELECT * FROM aktiviti ORDER BY tarikh_mula DESC LIMIT 5");
?>

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

/* Menu shortcut full screen */
.menu-card {
    text-align: center;
    padding: 50px 20px;
    border-radius: 15px;
    color: white;
    font-weight: 600;
    transition: all 0.3s ease;
    cursor: pointer;
    box-shadow: 0 6px 25px rgba(0,0,0,0.15);
}
.menu-card i { font-size: 64px; margin-bottom: 15px; }
.menu-card:hover { transform: translateY(-5px); box-shadow: 0 8px 30px rgba(0,0,0,0.2); }

.bg-aktiviti { background: linear-gradient(135deg, #6f42c1, #9b59b6); }
.bg-laporan  { background: linear-gradient(135deg, #198754, #28a745); }
.bg-profil   { background: linear-gradient(135deg, #fd7e14, #ff8c42); }
.bg-logout   { background: linear-gradient(135deg, #dc3545, #e74c3c); }

.activiti-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0 10px;
    font-size: 16px;
}

.activiti-table th {
    background-color: #3b0a77;
    color: white;
    padding: 15px 20px;
    border-radius: 10px;
}

.activiti-table td {
    background: white;
    padding: 15px 20px;
    border-radius: 10px;
}
</style>

<div class="main-content">
    <h2 class="mb-5" style="color: #3b0a77;">Selamat datang, <?= htmlspecialchars($pelajar['nama']); ?>!</h2>

    <!-- Menu Shortcut 3 satu baris -->
    <div class="row g-5 mb-5">
        <div class="col-md-4">
            <a href="senarai_aktiviti_pelajar.php" class="text-decoration-none">
                <div class="menu-card bg-aktiviti">
                    <i class="bi bi-list-task"></i>
                    <div>Pengurusan Aktiviti</div>
                </div>
            </a>
        </div>
        <div class="col-md-4">
            <a href="laporan_saya.php" class="text-decoration-none">
                <div class="menu-card bg-laporan">
                    <i class="bi bi-file-earmark-text"></i>
                    <div>Laporan Saya</div>
                </div>
            </a>
        </div>
        <div class="col-md-4">
            <a href="profil_pelajar.php" class="text-decoration-none">
                <div class="menu-card bg-profil">
                    <i class="bi bi-person-circle"></i>
                    <div>Profil Pelajar</div>
                </div>
            </a>
        </div>
        <div class="col-md-4">
            <a href="kemaskini_katalaluan.php" class="text-decoration-none">
                <div class="menu-card bg-profil">
                    <i class="bi bi-key-fill"></i>
                    <div>Kata Laluan</div>
                </div>
            </a>
        </div>
        <div class="col-md-4">
            <a href="logout.php" class="text-decoration-none">
                <div class="menu-card bg-logout">
                    <i class="bi bi-box-arrow-right"></i>
                    <div>Log Keluar</div>
                </div>
            </a>
        </div>
    </div>

    <!-- Aktiviti Terbaru / Akan Datang -->
    <div class="card p-4" style="background: white; color: #333;">
        <h4 style="color: #3b0a77; margin-bottom: 25px;"><i class="bi bi-clock-history"></i> Aktiviti Terbaru</h4>
        <table class="activiti-table">
            <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 ($aktiviti_terbaru && $aktiviti_terbaru->num_rows > 0): ?>
                    <?php while ($row = $aktiviti_terbaru->fetch_assoc()): ?>
                        <tr>
                            <td><?= htmlspecialchars($row['nama_aktiviti']); ?></td>
                            <td><?= htmlspecialchars($row['penganjur']); ?></td>
                            <td><?= htmlspecialchars($row['peringkat']); ?></td>
                            <td><?= htmlspecialchars($row['tempat']); ?></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" style="color:#999;">Tiada aktiviti direkodkan</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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