<?php
// admin_dashboard.php
require_once '../config.php';
requireRole('admin');

$conn = connectDB();
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="../../assets/logoFLS3N.png" type="image/x-icon">
    <title>Dashboard Admin - Sistem Penilaian Karya Siswa</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .sidebar {
            min-height: 100vh;
            background-color: #212529;
            color: white;
            position: sticky;
            top: 0;
        }
        .sidebar a {
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            transition: color 0.3s;
        }
        .sidebar a:hover {
            color: white;
        }
        .sidebar .nav-link.active {
            background-color: rgba(255, 255, 255, 0.1);
            color: white;
        }
        .sidebar .nav-item {
            margin-bottom: 5px;
        }
        .content {
            padding: 20px;
        }
        .dashboard-card {
            border-radius: 5px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <div class="col-md-3 col-lg-2 px-0 sidebar">
                <div class="d-flex flex-column p-3">
                    <a href="../admin_dashboard" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-white text-decoration-none">
                        <span class="fs-4">Admin Panel</span>
                    </a>
                    <hr>
                    <ul class="nav nav-pills flex-column mb-auto">
                        <li class="nav-item">
                            <a href="../admin_dashboard" class="nav-link active">
                                <i class="fas fa-home me-2"></i> Dashboard
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="admin_cabang_lomba" class="nav-link">
                                <i class="fas fa-trophy me-2"></i> Cabang Lomba
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="admin_kelola_peserta" class="nav-link">
                                <i class="fas fa-user-tie me-2"></i> Kelola Peserta
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="admin_peserta_kota" class="nav-link">
                                <i class="fas fa-users me-2"></i> Peserta Kota Salatiga
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="admin_peserta_kabupaten" class="nav-link">
                                <i class="fas fa-users me-2"></i> Peserta Kabupaten Semarang
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="admin_peserta_kabupaten_2" class="nav-link">
                                <i class="fas fa-users me-2"></i> Peserta Kabupaten Demak
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="admin_admin" class="nav-link">
                                <i class="fas fa-user-shield me-2"></i> Kelola Admin
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="admin_juri" class="nav-link">
                                <i class="fas fa-user-tie me-2"></i> Kelola Juri
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="admin_penilaian" class="nav-link">
                                <i class="fas fa-star me-2"></i> Hasil Penilaian
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="admin_status_lomba" class="nav-link">
                                <i class="fas fa-toggle-on me-2"></i> Status Lomba
                            </a>
                        </li>
                    </ul>
                    <hr>
                    <div class="dropdown">
                        <a href="#" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" id="dropdownUser1" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="fas fa-user-circle me-2 fs-5"></i>
                            <strong><?php echo $_SESSION['nama']; ?></strong>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-dark text-small shadow" aria-labelledby="dropdownUser1">
                            
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="../logout.php">Logout</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <!-- Content -->
            <div class="col-md-9 col-lg-10 content">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">Dashboard Admin</h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                    </div>
                </div>
                
                <div class="row">
                    <!-- Jumlah Peserta Kota -->
                    <?php
                    $sql = "SELECT COUNT(*) as total FROM domisilikota";
                    $result = $conn->query($sql);
                    $row = $result->fetch_assoc();
                    $totalPesertaKota = $row['total'];
                    ?>
                    <div class="col-md-4">
                        <div class="dashboard-card bg-primary text-white">
                            <h5>Peserta Kota</h5>
                            <h2><?php echo $totalPesertaKota; ?></h2>
                            <p>Total peserta dari domisili kota</p>
                        </div>
                    </div>
                    
                    <!-- Jumlah Peserta Kabupaten -->
                    <?php
                    $sql = "SELECT COUNT(*) as total FROM domisilikabupaten";
                    $result = $conn->query($sql);
                    $row = $result->fetch_assoc();
                    $totalPesertaKabupaten = $row['total'];
                    ?>
                    <div class="col-md-4">
                        <div class="dashboard-card bg-success text-white">
                            <h5>Peserta Kabupaten</h5>
                            <h2><?php echo $totalPesertaKabupaten; ?></h2>
                            <p>Total peserta dari domisili kabupaten</p>
                        </div>
                    </div>
                    
                    <!-- Jumlah Juri -->
                    <?php
                    $sql = "SELECT COUNT(*) as total FROM users WHERE role = 'juri'";
                    $result = $conn->query($sql);
                    $row = $result->fetch_assoc();
                    $totalJuri = $row['total'];
                    ?>
                    <div class="col-md-4">
                        <div class="dashboard-card bg-info text-white">
                            <h5>Juri</h5>
                            <h2><?php echo $totalJuri; ?></h2>
                            <p>Total juri aktif</p>
                        </div>
                    </div>
                </div>
                
                <div class="row mt-4">
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header">
                                <h5>Daftar Cabang Lomba</h5>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-striped table-sm">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>Bidang Lomba</th>
                                                <th>Jumlah Peserta</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $sql = "SELECT cl.ID, cl.Bidang_Lomba, 
                                                   (SELECT COUNT(*) FROM domisilikota WHERE ID_Cabang_Lomba = cl.ID) +
                                                   (SELECT COUNT(*) FROM domisilikabupaten WHERE ID_Cabang_Lomba = cl.ID) as total_peserta
                                                   FROM cabang_lomba cl
                                                   ORDER BY cl.ID";
                                            $result = $conn->query($sql);
                                            
                                            if ($result->num_rows > 0) {
                                                while($row = $result->fetch_assoc()) {
                                                    echo "<tr>";
                                                    echo "<td>" . $row["ID"] . "</td>";
                                                    echo "<td>" . $row["Bidang_Lomba"] . "</td>";
                                                    echo "<td>" . $row["total_peserta"] . "</td>";
                                                    echo "</tr>";
                                                }
                                            } else {
                                                echo "<tr><td colspan='3' class='text-center'>Tidak ada data</td></tr>";
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header">
                                <h5>Juri Terbaru</h5>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-striped table-sm">
                                        <thead>
                                            <tr>
                                                <th>Nama</th>
                                                <th>Email</th>
                                                <th>Cabang Lomba</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $sql = "SELECT u.nama_lengkap, u.email, c.Bidang_Lomba 
                                                   FROM users u
                                                   LEFT JOIN cabang_lomba c ON u.id_cabang_lomba = c.ID
                                                   WHERE u.role = 'juri'
                                                   ORDER BY u.created_at DESC
                                                   LIMIT 5";
                                            $result = $conn->query($sql);
                                            
                                            if ($result->num_rows > 0) {
                                                while($row = $result->fetch_assoc()) {
                                                    echo "<tr>";
                                                    echo "<td>" . $row["nama_lengkap"] . "</td>";
                                                    echo "<td>" . $row["email"] . "</td>";
                                                    echo "<td>" . ($row["Bidang_Lomba"] ? $row["Bidang_Lomba"] : "Belum ditentukan") . "</td>";
                                                    echo "</tr>";
                                                }
                                            } else {
                                                echo "<tr><td colspan='3' class='text-center'>Tidak ada data</td></tr>";
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

<?php $conn->close(); ?>