<?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>
        .dashboard-card {
            border-radius: 10px;
            padding: 25px;
            margin-bottom: 20px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            border: none;
            position: relative;
            overflow: hidden;
        }

        .dashboard-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
        }

        .dashboard-card-icon {
            font-size: 48px;
            opacity: 0.2;
            position: absolute;
            right: 15px;
            top: 15px;
        }

        .dashboard-card h5 {
            font-weight: 600;
            margin-bottom: 10px;
            position: relative;
            z-index: 1;
        }

        .dashboard-card h2 {
            font-weight: 700;
            font-size: 2.5rem;
            margin: 10px 0;
            position: relative;
            z-index: 1;
        }

        .dashboard-card p {
            margin: 0;
            position: relative;
            z-index: 1;
            font-size: 0.9rem;
        }

        .dashboard-card-img {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            opacity: 0.15;
            width: 120px;
            height: 120px;
            object-fit: contain;
            z-index: 0;
        }

        .card-kota-salatiga {
            background: linear-gradient(135deg, #3b82f6 0%, #1e3a8a 100%);
            color: white;
        }

        .card-kabupaten-semarang {
            background: linear-gradient(135deg, #10b981 0%, #065f46 100%);
            color: white;
        }

        .card-kabupaten-demak {
            background: linear-gradient(135deg, #f59e0b 0%, #b45309 100%);
            color: white;
        }
    </style>
</head>
<body>
    <?php include 'sidebar.php'; ?>

                <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">
                    <!-- Peserta Kota Salatiga -->
                    <?php
                    $sql = "SELECT COUNT(*) as total FROM domisilikota";
                    $result = $conn->query($sql);
                    $row = $result->fetch_assoc();
                    $totalPesertaKotaSalatiga = $row['total'];
                    ?>
                    <div class="col-md-4 mb-3">
                        <div class="dashboard-card card-kota-salatiga">
                            <div class="dashboard-card-icon">
                                <i class="fas fa-city"></i>
                            </div>
                            <img src="../../assets/kota-salatiga.png" alt="Kota Salatiga" class="dashboard-card-img">
                            <h5><i class="fas fa-map-pin me-2"></i> Kota Salatiga</h5>
                            <h2><?php echo $totalPesertaKotaSalatiga; ?></h2>
                            <p>Total peserta terdaftar</p>
                        </div>
                    </div>
                    
                    <!-- Peserta Kabupaten Semarang -->
                    <?php
                    $sql = "SELECT COUNT(*) as total FROM domisilikabupaten";
                    $result = $conn->query($sql);
                    $row = $result->fetch_assoc();
                    $totalPesertaKabupatenSemarang = $row['total'];
                    ?>
                    <div class="col-md-4 mb-3">
                        <div class="dashboard-card card-kabupaten-semarang">
                            <div class="dashboard-card-icon">
                                <i class="fas fa-map-marked-alt"></i>
                            </div>
                            <img src="../../assets/kabupaten-semarang.png" alt="Kabupaten Semarang" class="dashboard-card-img">
                            <h5><i class="fas fa-map-pin me-2"></i> Kabupaten Semarang</h5>
                            <h2><?php echo $totalPesertaKabupatenSemarang; ?></h2>
                            <p>Total peserta terdaftar</p>
                        </div>
                    </div>
                    
                    <!-- Peserta Kabupaten Demak -->
                    <?php
                    $sql = "SELECT COUNT(*) as total FROM domisilikabupaten2";
                    $result = $conn->query($sql);
                    $row = $result->fetch_assoc();
                    $totalPesertaKabupatenDemak = $row['total'];
                    ?>
                    <div class="col-md-4 mb-3">
                        <div class="dashboard-card card-kabupaten-demak">
                            <div class="dashboard-card-icon">
                                <i class="fas fa-map-marked-alt"></i>
                            </div>
                            <img src="../../assets/kabupaten-demak.png" alt="Kabupaten Demak" class="dashboard-card-img">
                            <h5><i class="fas fa-map-pin me-2"></i> Kabupaten Demak</h5>
                            <h2><?php echo $totalPesertaKabupatenDemak; ?></h2>
                            <p>Total peserta terdaftar</p>
                        </div>
                    </div>

                    <!-- Pendaftaran Sekolah -->
                    <?php
                    $sql = "SELECT COUNT(*) as total FROM master_sekolah";
                    $result = $conn->query($sql);
                    $row = $result->fetch_assoc();
                    $totalSekolah = $row['total'];
                    ?>
                    <div class="col-md-4 mb-3">
                        <a href="admin_kelola_sekolah" style="text-decoration: none;">
                            <div class="dashboard-card" style="background: linear-gradient(135deg, #8b5cf6 0%, #6d28d9 100%); color: white;">
                                <div class="dashboard-card-icon">
                                    <i class="fas fa-school"></i>
                                </div>
                                <h5><i class="fas fa-book me-2"></i> Pendaftaran Sekolah</h5>
                                <h2><?php echo $totalSekolah; ?></h2>
                                <p>Sekolah terdaftar</p>
                            </div>
                        </a>
                    </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) +
                                                   (SELECT COUNT(*) FROM domisilikabupaten2 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>

    <?php include 'sidebar-footer.php'; ?>

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