<?php
include '../includes/auth.php';
include '../includes/functions.php';
include '../includes/headeradm.php';

$activeEvent = getActiveEvent();
$allEvents = getAllEvents();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - DIGITAL PANDAI</title>
    <link href="../assets/css/sweetalert2.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
     <link href="../assets/css/style.css" rel="stylesheet">
</head>
<body>
    <!-- <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
             <a class="navbar-brand" href="#">
                <i class="fas fa-user-check text-primary"></i> 
                <span class="ms-2">Absensi Digital </span>
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link active" href="dashboard.php">Dashboard</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="events.php">Kelola Event</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="attendance.php">Daftar Hadir</a>
                    </li>
                </ul>
                <ul class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown">
                            <?= $_SESSION['admin_username'] ?>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="logout.php">Logout</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav> -->

    <div class="container mt-5">
        <h2 class="mb-4">Dashboard Admin</h2>
        
        <div class="row">
            <div class="col-md-6">
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        Event Aktif Saat Ini
                    </div>
                    <div class="card-body">
                        <?php if ($activeEvent): ?>
                            <h5><?= $activeEvent['nama_event'] ?></h5>
                            <p>Tanggal: <?= date('d F Y', strtotime($activeEvent['tanggal_event'])) ?></p>
                            <p>Pengesah: <?= $activeEvent['pengesah'] ?></p>
                            <a href="events.php" class="btn btn-sm btn-primary">Kelola Event</a>
                        <?php else: ?>
                            <p>Tidak ada event aktif saat ini.</p>
                            <a href="events.php" class="btn btn-sm btn-primary">Buat Event Baru</a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            
            <div class="col-md-6">
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        Statistik Terkini
                    </div>
                    <div class="card-body">
                        <p>Total Event: <?= count($allEvents) ?></p>
                        <?php if ($activeEvent): 
                            $attendance = getAttendanceByEvent($activeEvent['id_event']); ?>
                            <p>Absensi Event Aktif: <?= count($attendance) ?> orang</p>
                            <a href="attendance.php?event_id=<?= $activeEvent['id_event'] ?>" class="btn btn-sm btn-primary">Lihat Daftar Hadir</a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="card">
            <div class="card-header bg-primary text-white">
                5 Event Terbaru
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>Nama Event</th>
                                <th>Tanggal</th>
                                <th>Status</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach (array_slice($allEvents, 0, 5) as $event): ?>
                            <tr>
                                <td><?= $event['nama_event'] ?></td>
                                <td><?= date('d/m/Y', strtotime($event['tanggal_event'])) ?></td>
                                <td>
                                    <span class="badge bg-<?= $event['status'] === 'aktif' ? 'success' : 'secondary' ?>">
                                        <?= ucfirst($event['status']) ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="attendance.php?event_id=<?= $event['id_event'] ?>" class="btn btn-sm btn-info">Lihat Absensi</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <a href="events.php" class="btn btn-primary mt-3">Lihat Semua Event</a>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="../assets/js/sweetalert2.min.js"></script>
</body>
</html>