<?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
$aktiviti_terbaru = $conn->query("SELECT nama_aktiviti, tarikh_mula, tarikh_tamat FROM aktiviti ORDER BY tarikh_mula DESC LIMIT 5");
?>

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

.menu-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 25px;
}

.menu-card {
    border-radius: 18px;
    padding: 40px 20px;
    text-align: center;
    box-shadow: 0 8px 20px rgba(0,0,0,0.15);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    text-decoration: none;
    color: white;
    font-weight: 600;
}
.menu-card:hover {
    transform: translateY(-8px) scale(1.03);
    box-shadow: 0 12px 25px rgba(0,0,0,0.25);
}
.menu-card i {
    font-size: 55px;
    margin-bottom: 15px;
}
.menu-card h4 {
    font-size: 20px;
    margin: 0;
}

/* Gradient warna menu */
.bg-purple { background: linear-gradient(135deg, #6f42c1, #9b59b6); }
.bg-orange { background: linear-gradient(135deg, #fd7e14, #f39c12); }
.bg-blue   { background: linear-gradient(135deg, #0d6efd, #2980b9); }
.bg-green  { background: linear-gradient(135deg, #198754, #27ae60); }
.bg-pink   { background: linear-gradient(135deg, #d63384, #e83e8c); }
.bg-red    { background: linear-gradient(135deg, #dc3545, #e74c3c); }
.bg-teal   { background: linear-gradient(135deg, #20c997, #16a085); }
.bg-dark   { background: linear-gradient(135deg, #343a40, #2c3e50); }

/* Aktiviti terbaru */
.card-aktiviti {
    background: white;
    border-radius: 12px;
    padding: 20px;
    margin-top: 50px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.08);
}
.table-custom {
    width: 100%;
    border-collapse: collapse;
}
.table-custom th, .table-custom td {
    padding: 12px 15px;
    border-bottom: 1px solid #ddd;
}
.table-custom th {
    background-color: #3b0a77;
    color: white;
}
.no-data {
    text-align: center;
    color: #888;
    font-style: italic;
}

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

    <div class="card-aktiviti">
        <h5 style="font-weight: 700; font-size: 20px; 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>
</div>

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