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

// Pastikan connection UTF-8 untuk elak aksara pelik
mysqli_set_charset($conn, "utf8mb4");

$id_pelajar = (int) $_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);

// Kira markah merit BERDASARKAN KEHADIRAN (Hadir sahaja)
$sqlMerit = "
    SELECT COUNT(*) AS jumlah_hadir
    FROM penyertaan
    WHERE pelajar_id = $id_pelajar
      AND status_kehadiran = 'Hadir'
";
$resMerit   = mysqli_query($conn, $sqlMerit);
$dataMerit  = $resMerit ? mysqli_fetch_assoc($resMerit) : ['jumlah_hadir' => 0];
$markah_merit = ((int)($dataMerit['jumlah_hadir'] ?? 0)) * 5;

// Aktiviti akan datang (semua aktiviti selepas/ pada hari ini)
$sqlAktiviti = "
    SELECT *
    FROM aktiviti
    WHERE tarikh_mula >= CURDATE()
    ORDER BY tarikh_mula ASC
    LIMIT 5
";
$resultAktiviti = mysqli_query($conn, $sqlAktiviti);
?>
<div style="margin-left: 180px; margin-top: 100px; padding: 20px; min-height: calc(100vh - 100px);">

    <!-- Selamat Datang + Merit -->
    <div class="mb-4">
        <h3 class="fw-bold text-primary">
            Selamat Datang, <?= htmlspecialchars($pelajar['nama'] ?? 'Pelajar'); ?> ??
        </h3>
        <p class="text-muted mb-0">
            Markah Merit (Hadir): <strong><?= $markah_merit; ?></strong>
        </p>
    </div>

    <!-- Menu Shortcut -->
    <div class="row g-4 mb-4">
        <div class="col-md-3">
            <a href="senarai_aktiviti_pelajar.php" class="text-decoration-none">
                <div class="card text-center shadow-sm p-3 h-100 border-0 hover-shadow">
                    <i class="bi bi-calendar-check display-4 text-primary"></i>
                    <h6 class="mt-3">Senarai Aktiviti</h6>
                </div>
            </a>
        </div>
        <div class="col-md-3">
            <a href="aktiviti_saya_pelajar.php" class="text-decoration-none">
                <div class="card text-center shadow-sm p-3 h-100 border-0 hover-shadow">
                    <i class="bi bi-check2-square display-4 text-success"></i>
                    <h6 class="mt-3">Aktiviti Saya</h6>
                </div>
            </a>
        </div>
        <div class="col-md-3">
            <a href="profil_pelajar.php" class="text-decoration-none">
                <div class="card text-center shadow-sm p-3 h-100 border-0 hover-shadow">
                    <i class="bi bi-person-circle display-4 text-warning"></i>
                    <h6 class="mt-3">Profil Pelajar</h6>
                </div>
            </a>
        </div>
        <div class="col-md-3">
            <a href="laporan_saya.php" class="text-decoration-none">
                <div class="card text-center shadow-sm p-3 h-100 border-0 hover-shadow">
                    <i class="bi bi-bar-chart-fill display-4 text-info"></i>
                    <h6 class="mt-3">Status Aktiviti</h6>
                </div>
            </a>
        </div>
    </div>

    <!-- Aktiviti Akan Datang -->
    <div class="card shadow p-4">
        <h4 class="mb-3 text-secondary"><i class="bi bi-calendar-event"></i> Aktiviti Akan Datang</h4>
        <ul class="list-group list-group-flush">
            <?php if ($resultAktiviti && mysqli_num_rows($resultAktiviti) > 0): ?>
                <?php while ($aktiviti = mysqli_fetch_assoc($resultAktiviti)):
                    $tarikhMula = date('d M Y', strtotime($aktiviti['tarikh_mula']));
                    $tarikhTamat = date('d M Y', strtotime($aktiviti['tarikh_tamat']));
                    $tarikhPaparan = ($aktiviti['tarikh_mula'] === $aktiviti['tarikh_tamat'])
                                     ? $tarikhMula
                                     : "$tarikhMula - $tarikhTamat";
                ?>
                    <li class="list-group-item">
                        <strong><?= htmlspecialchars($aktiviti['nama_aktiviti']); ?></strong><br>
                        <small>
                            <i class="bi bi-geo-alt"></i>
                            <?= htmlspecialchars($aktiviti['tempat']); ?> | <?= $tarikhPaparan; ?>
                        </small>
                    </li>
                <?php endwhile; ?>
            <?php else: ?>
                <li class="list-group-item text-muted">Tiada aktiviti akan datang.</li>
            <?php endif; ?>
        </ul>
    </div>

</div>

<style>
    .hover-shadow:hover {
        box-shadow: 0 4px 20px rgba(0,0,0,0.2);
        transition: 0.3s;
        transform: translateY(-5px);
    }
</style>

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