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

// URL gambar
$gambar_url = !empty($pelajar['gambar']) ? 'uploads/' . $pelajar['gambar'] : 'images/default-avatar.png';

// Aktiviti akan datang (ikut pelajar)
$sqlAktiviti = "
    SELECT a.nama_aktiviti, a.tarikh_mula, a.tempat
    FROM penyertaan p
    JOIN aktiviti a ON p.aktiviti_id = a.id
    WHERE p.pelajar_id = $id_pelajar
    AND a.tarikh_mula >= CURDATE()
    ORDER BY a.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);">

    <!-- Kad Profil -->
    <div class="card shadow p-4 mb-4">
        <div class="row g-4 align-items-center">
            <div class="col-md-3 text-center">
                <img src="<?= htmlspecialchars($gambar_url); ?>" 
                     alt="Gambar Pelajar" 
                     class="rounded-circle img-thumbnail shadow-sm" 
                     style="width: 150px; height: 150px; object-fit: cover; cursor: pointer;"
                     title="Klik untuk tukar gambar"
                     onclick="document.getElementById('uploadGambar').click();">
                <form id="formGambar" action="upload_gambar.php" method="POST" enctype="multipart/form-data" style="display:none;">
                    <input type="file" name="gambar" id="uploadGambar" accept="image/*" onchange="document.getElementById('formGambar').submit();">
                </form>
            </div>
            <div class="col-md-9">
                <h3 class="fw-bold text-primary mb-3">Selamat Datang, <?= htmlspecialchars($pelajar['nama']); ?> ??</h3>
                <div class="row">
                    <div class="col-md-6">
                        <p><strong>No. Kad Pengenalan:</strong> <?= htmlspecialchars($pelajar['no_ic']); ?></p>
                        <p><strong>Program:</strong> <?= htmlspecialchars($pelajar['program']); ?></p>
                        <p><strong>Jantina:</strong> <?= htmlspecialchars($pelajar['jantina']); ?></p>
                        <p><strong>Status:</strong> <?= htmlspecialchars($pelajar['status']); ?></p>
                    </div>
                    <div class="col-md-6">
                        <p><strong>Bangsa:</strong> <?= htmlspecialchars($pelajar['bangsa']); ?></p>
                        <p><strong>Agama:</strong> <?= htmlspecialchars($pelajar['agama']); ?></p>
                        <p><strong>Warganegara:</strong> <?= htmlspecialchars($pelajar['warganegara']); ?></p>
                        <p><strong>No. Telefon:</strong> <?= htmlspecialchars($pelajar['no_telefon']); ?></p>
                        <p><strong>E-mel:</strong> <?= htmlspecialchars($pelajar['emel']); ?></p>
                    </div>
                </div>
            </div>
        </div>
    </div>

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

    <!-- Menu Shortcut -->
    <div class="row g-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="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-success"></i>
                    <h6 class="mt-3">Laporan & Statistik</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="logout.php" class="text-decoration-none" onclick="return confirm('Log keluar?')">
                <div class="card text-center shadow-sm p-3 h-100 border-0 hover-shadow">
                    <i class="bi bi-box-arrow-right display-4 text-danger"></i>
                    <h6 class="mt-3">Log Keluar</h6>
                </div>
            </a>
        </div>
    </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'; ?>
