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

<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 80px 40px;
    min-height: calc(100vh - 100px - 50px);
}

/* Menu Grid */
.menu-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 25px;
    margin-bottom: 50px;
}

.menu-card {
    border-radius: 18px;
    padding: 30px;
    color: white;
    text-align: center;
    font-weight: 600;
    font-size: 20px;
    box-shadow: 0 10px 20px rgba(0,0,0,0.15);
    transition: transform 0.25s ease, box-shadow 0.25s ease;
    text-decoration: none;
}
.menu-card i {
    font-size: 48px;
    margin-bottom: 15px;
    display: block;
}
.menu-card:hover {
    transform: translateY(-8px);
    box-shadow: 0 15px 25px rgba(0,0,0,0.25);
}

/* Gradients */
.bg-gradient-purple { background: linear-gradient(135deg, #6f42c1, #b07bff); }
.bg-gradient-orange { background: linear-gradient(135deg, #fd7e14, #ffb166); }
.bg-gradient-green  { background: linear-gradient(135deg, #198754, #4ed798); }
.bg-gradient-blue   { background: linear-gradient(135deg, #0d6efd, #66b2ff); }
.bg-gradient-pink   { background: linear-gradient(135deg, #d63384, #ff99c8); }
.bg-gradient-teal   { background: linear-gradient(135deg, #20c997, #6fffd2); }
.bg-gradient-red    { background: linear-gradient(135deg, #dc3545, #ff7b88); }
.bg-gradient-dark   { background: linear-gradient(135deg, #343a40, #6c757d); }

/* Table */
.table-custom {
    border-collapse: separate !important;
    border-spacing: 0 8px !important;
    font-size: 16px;
    width: 100%;
}
.table-custom thead tr {
    background-color: #3b0a77;
    color: white;
    border-radius: 12px;
}
.table-custom th, 
.table-custom td {
    padding: 14px 20px;
    vertical-align: middle !important;
}
.table-custom tbody tr {
    background-color: white;
    box-shadow: 0 2px 8px rgb(0 0 0 / 0.05);
    border-radius: 12px;
    margin-bottom: 12px;
}
.table-custom tbody tr td {
    border: none !important;
}
.no-data {
    font-style: italic;
    color: #999;
    text-align: center;
    padding: 20px 0;
}

h2.page-title {
    font-weight: 700;
    font-size: 34px;
    color: #3b0a77;
    margin-bottom: 30px;
    text-shadow: 0 1px 3px rgb(0 0 0 / 0.1);
}

/* Responsive */
@media (max-width: 992px) {
    .menu-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}
@media (max-width: 600px) {
    .menu-grid {
        grid-template-columns: 1fr;
    }
    .main-content {
        margin-left: 0;
        margin-top: 130px;
        padding: 20px;
    }
}
</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 bg-gradient-purple">
            <i class="bi bi-calendar-event"></i> Pengurusan Aktiviti
        </a>
        <a href="senarai_pelajar.php" class="menu-card bg-gradient-orange">
            <i class="bi bi-people"></i> Pengurusan Pelajar
        </a>
        <a href="senarai_penyertaan.php" class="menu-card bg-gradient-green">
            <i class="bi bi-check2-square"></i> Pengurusan Penyertaan
        </a>
        <a href="pengurusan_kehadiran.php" class="menu-card bg-gradient-blue">
            <i class="bi bi-clipboard-check"></i> Pengurusan Kehadiran
        </a>
        <a href="pengurusan_sijil.php" class="menu-card bg-gradient-pink">
            <i class="bi bi-award"></i> Pengurusan Sijil
        </a>
        <a href="laporan.php" class="menu-card bg-gradient-teal">
            <i class="bi bi-bar-chart"></i> Laporan & Statistik
        </a>
        <?php if ($jenis_admin === 'utama'): ?>
        <a href="senarai_admin.php" class="menu-card bg-gradient-red">
            <i class="bi bi-person-badge"></i> Pengurusan Admin
        </a>
        <?php endif; ?>
        <a href="tetapan.php" class="menu-card bg-gradient-dark">
            <i class="bi bi-gear"></i> Tetapan Sistem
        </a>
        <a href="logout_admin.php" onclick="return confirm('Log keluar?')" class="menu-card bg-gradient-orange">
            <i class="bi bi-box-arrow-right"></i> Log Keluar
        </a>
    </div>

    <section>
        <div class="card" style="background: white; color: #333; border-radius: 12px; padding: 25px;">
            <h5 style="font-weight: 700; font-size: 24px; color: #3b0a77;">
                <i class="bi bi-clock-history"></i> Aktiviti Terbaru
            </h5>
            <table class="table table-bordered table-custom mt-3">
                <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>
    </section>
</div>

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