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

include 'includes/config.php';
include 'includes/header_admin.php';
include 'includes/sidebar_admin.php';

$jenis_admin = $_SESSION['jenis_admin'] ?? 'admin';

// Aktiviti terbaru (5 rekod)
$aktiviti_terbaru = $conn->query("
    SELECT nama_aktiviti, tarikh_mula, tarikh_tamat 
    FROM aktiviti 
    ORDER BY tarikh_mula DESC 
    LIMIT 5
");

// Top 10 pelajar paling aktif - auto kira merit
$top_pelajar = $conn->query("
    SELECT p.nama, p.program, COUNT(py.id) * 5 AS merit
    FROM pelajar p
    LEFT JOIN penyertaan py 
        ON p.id = py.pelajar_id 
        AND py.status_kehadiran = 'Hadir'
    GROUP BY p.id
    ORDER BY merit DESC
    LIMIT 10
");
?>

<style>
body {
    background-color: #f9fafb;
    font-family: 'Rubik', sans-serif;
    margin: 0;
    padding: 0;
    color: #333;
}
.main-content {
    margin-left: 180px;
    margin-top: 100px;
    padding: 30px 40px;
}
.page-title {
    font-weight: 700;
    font-size: 34px;
    color: #3b0a77;
    margin-bottom: 30px;
    text-shadow: 0 1px 3px rgb(0 0 0 / 0.1);
}
.menu-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
}
.menu-card {
    flex: 1 1 calc(33.333% - 20px);
    min-width: 250px;
    background: linear-gradient(135deg, #6f42c1, #9b59b6);
    color: white;
    border-radius: 15px;
    padding: 30px;
    text-align: center;
    text-decoration: none;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.menu-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 6px 18px rgba(0,0,0,0.15);
}
.menu-card i {
    font-size: 48px;
    margin-bottom: 15px;
}
.menu-card h4 {
    font-size: 20px;
    font-weight: 600;
}
.table-custom {
    width: 100%;
    border-collapse: collapse;
    margin-top: 30px;
    font-size: 16px;
}
.table-custom thead {
    background-color: #3b0a77;
    color: white;
}
.table-custom th, .table-custom td {
    padding: 12px 15px;
    border: 1px solid #ddd;
}
.no-data {
    font-style: italic;
    text-align: center;
    color: #999;
}
@media (max-width: 768px) {
    .main-content {
        margin-left: 0;
        margin-top: 130px;
        padding: 20px;
    }
    .menu-card {
        flex: 1 1 100%;
    }
}
</style>

<div class="main-content">
    <h2 class="page-title">Selamat datang, <?= htmlspecialchars($_SESSION['nama']) ?>!</h2>

    <div class="menu-grid">
        <a href="senarai_aktiviti.php" class="menu-card" style="background: linear-gradient(135deg, #6f42c1, #8e44ad);">
            <i class="bi bi-calendar-event"></i>
            <h4>Pengurusan Aktiviti</h4>
        </a>
        <a href="senarai_pelajar.php" class="menu-card" style="background: linear-gradient(135deg, #fd7e14, #ff9800);">
            <i class="bi bi-people"></i>
            <h4>Pengurusan Pelajar</h4>
        </a>
        <?php if ($jenis_admin === 'utama'): ?>
        <a href="senarai_admin.php" class="menu-card" style="background: linear-gradient(135deg, #198754, #27ae60);">
            <i class="bi bi-person-badge"></i>
            <h4>Pengurusan Admin</h4>
        </a>
        <?php endif; ?>
        <a href="senarai_penyertaan.php" class="menu-card" style="background: linear-gradient(135deg, #0d6efd, #3498db);">
            <i class="bi bi-check2-square"></i>
            <h4>Pengurusan Penyertaan</h4>
        </a>
        <a href="pengurusan_kehadiran.php" class="menu-card" style="background: linear-gradient(135deg, #ffc107, #f39c12);">
            <i class="bi bi-clipboard-check"></i>
            <h4>Pengurusan Kehadiran</h4>
        </a>
        <a href="pengurusan_sijil.php" class="menu-card" style="background: linear-gradient(135deg, #e83e8c, #c2185b);">
            <i class="bi bi-award"></i>
            <h4>Pengurusan Sijil</h4>
        </a>
        <a href="laporan.php" class="menu-card" style="background: linear-gradient(135deg, #20c997, #16a085);">
            <i class="bi bi-bar-chart"></i>
            <h4>Laporan & Statistik</h4>
        </a>
        <?php if ($jenis_admin === 'utama'): ?>
        <a href="laporan_merit.php" target="_blank" class="menu-card" style="background: linear-gradient(135deg, #6c757d, #495057);">
            <i class="bi bi-file-earmark-text"></i>
            <h4>Laporan Merit</h4>
        </a>
        <a href="logout_admin.php" onclick="return confirm('Log keluar?')" class="menu-card" style="background: linear-gradient(135deg, #dc3545, #c0392b);">
            <i class="bi bi-box-arrow-right"></i>
            <h4>Log Keluar</h4>
        </a>
        <?php endif; ?>
    </div>

    <!-- Aktiviti terbaru -->
    <div style="margin-top:50px; background: white; padding: 20px; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.05);">
        <h5 style="font-weight: 700; font-size: 22px; color: #3b0a77;">
            <i class="bi bi-clock-history"></i> Aktiviti Terbaru
        </h5>
        <table class="table-custom">
            <thead>
                <tr>
                    <th>Nama Aktiviti</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><?= 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="3" class="no-data">Tiada aktiviti direkodkan</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <!-- Top 10 Pelajar Aktif -->
    <div style="margin-top:30px; background: white; padding: 20px; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.05);">
        <h5 style="font-weight: 700; font-size: 22px; color: #3b0a77;">
            <i class="bi bi-trophy"></i> Top 10 Pelajar Paling Aktif
        </h5>
        <table class="table-custom">
            <thead>
                <tr>
                    <th>Nama Pelajar</th>
                    <th>Program</th>
                    <th>Markah Merit</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($top_pelajar && $top_pelajar->num_rows > 0): ?>
                    <?php while ($row = $top_pelajar->fetch_assoc()): ?>
                        <tr>
                            <td><?= htmlspecialchars($row['nama']) ?></td>
                            <td><?= htmlspecialchars($row['program']) ?></td>
                            <td><?= (int)$row['merit'] ?></td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr><td colspan="3" class="no-data">Tiada data pelajar aktif</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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