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

include 'includes/config.php';
include 'includes/header_admin.php';   // Header standard untuk admin/pelajar
include 'includes/sidebar_admin.php';

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

// Statistik ringkas
$total_aktiviti = $conn->query("SELECT COUNT(*) AS total FROM aktiviti")->fetch_assoc()['total'] ?? 0;
$total_pelajar = $conn->query("SELECT COUNT(*) AS total FROM pelajar")->fetch_assoc()['total'] ?? 0;
$total_admin = 0;
if ($jenis_admin === 'utama') {
    $total_admin = $conn->query("SELECT COUNT(*) AS total FROM admin")->fetch_assoc()['total'] ?? 0;
}

// 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>
/* Reset & base */
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);
}

/* Cards */
.card {
    border-radius: 12px;
    padding: 25px 30px;
    color: white;
    box-shadow: 0 4px 15px rgb(0 0 0 / 0.1);
    transition: transform 0.2s ease;
}
.card:hover {
    transform: translateY(-5px);
}
.bg-purple { background-color: #6f42c1; }
.bg-orange { background-color: #fd7e14; }
.bg-green  { background-color: #198754; }

.card h4 {
    font-weight: 600;
    font-size: 22px;
    margin-bottom: 12px;
}
.card p {
    font-size: 36px;
    font-weight: 700;
    margin: 0;
}

/* 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;
}

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

.no-data {
    font-style: italic;
    color: #999;
    text-align: center;
    padding: 20px 0;
}

/* Responsive */
@media (max-width: 768px) {
    .main-content {
        margin-left: 0;
        margin-top: 130px;
        padding: 20px;
    }
    .sidebar {
        width: 100%;
        height: auto;
        position: relative;
    }
    .card p {
        font-size: 28px;
    }
}
</style>

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

    <div class="row" style="display: flex; gap: 20px; flex-wrap: wrap;">
        <div style="flex: 1; min-width: 220px;">
            <div class="card bg-purple text-center">
                <h4><i class="bi bi-list-task"></i> Jumlah Aktiviti</h4>
                <p><?= $total_aktiviti ?></p>
            </div>
        </div>

        <div style="flex: 1; min-width: 220px;">
            <div class="card bg-orange text-center">
                <h4><i class="bi bi-people-fill"></i> Jumlah Pelajar</h4>
                <p><?= $total_pelajar ?></p>
            </div>
        </div>

        <?php if ($jenis_admin === 'utama'): ?>
        <div style="flex: 1; min-width: 220px;">
            <div class="card bg-green text-center">
                <h4><i class="bi bi-person-badge-fill"></i> Jumlah Admin</h4>
                <p><?= $total_admin ?></p>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <section style="margin-top: 50px;">
        <div class="card" style="background: white; color: #333;">
            <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'; ?>
