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

        /* Container ikon/logo di dalam card */
        .dashboard-card-icon {
            font-size: 48px;
            opacity: 0.2;
            position: absolute;
            right: 15px;
            top: 15px;
            z-index: 0;
        }

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

        /* Pengaturan Gambar Logo Wilayah agar rapi di pojok */
        .dashboard-card-img {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            opacity: 0.25; /* Sedikit lebih terang agar terlihat jelas */
            width: 100px;
            height: 100px;
            object-fit: contain;
            z-index: 0;
        }

        .card-tervalidasi {
            background: linear-gradient(135deg, #22c55e 0%, #15803d 100%);
            color: white;
        }

        .card-menunggu {
            background: linear-gradient(135deg, #facc15 0%, #a16207 100%);
            color: white;
        }

        .card-ditolak {
            background: linear-gradient(135deg, #ef4444 0%, #b91c1c 100%);
            color: white;
        }

        .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>
    
    <div class="row">
        <?php
        // Queries Wilayah
        $totalPesertaKotaSalatiga = $conn->query("SELECT COUNT(*) as total FROM domisilikota")->fetch_assoc()['total'];
        $totalPesertaKabupatenSemarang = $conn->query("SELECT COUNT(*) as total FROM domisilikabupaten")->fetch_assoc()['total'];
        $totalPesertaKabupatenDemak = $conn->query("SELECT COUNT(*) as total FROM domisilikabupaten2")->fetch_assoc()['total'];
        ?>

        <div class="col-md-4 mb-3">
            <div class="dashboard-card card-kota-salatiga">
                <img src="../../assets/kota-salatiga.png" alt="Logo 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>
        
        <div class="col-md-4 mb-3">
            <div class="dashboard-card card-kabupaten-semarang">
                <img src="../../assets/kabupaten-semarang.png" alt="Logo Kab 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>
        
        <div class="col-md-4 mb-3">
            <div class="dashboard-card card-kabupaten-demak">
                <img src="../../assets/kabupaten-demak.png" alt="Logo Kab 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>
    </div>

    <div class="row">
        <?php
        // Queries Status Sekolah
        $totalSekolah = $conn->query("SELECT COUNT(*) as total FROM master_sekolah")->fetch_assoc()['total'];
        $totalTervalidasi = $conn->query("SELECT COUNT(*) as total FROM master_sekolah WHERE Status = 'tervalidasi'")->fetch_assoc()['total'];
        $totalMenunggu = $conn->query("SELECT COUNT(*) as total FROM master_sekolah WHERE Status = 'memerlukan_validasi'")->fetch_assoc()['total'];
        $totalDitolak = $conn->query("SELECT COUNT(*) as total FROM master_sekolah WHERE Status = 'ditolak'")->fetch_assoc()['total'];
        ?>

        <div class="col-md-3 mb-3">
            <div class="dashboard-card card-tervalidasi">
                <div class="dashboard-card-icon"><i class="fas fa-check-circle"></i></div>
                <h5><i class="fas fa-school me-2"></i> Tervalidasi</h5>
                <h2><?php echo $totalTervalidasi; ?></h2>
                <p>Pendaftaran disetujui</p>
            </div>
        </div>

        <div class="col-md-3 mb-3">
            <div class="dashboard-card card-menunggu">
                <div class="dashboard-card-icon"><i class="fas fa-clock"></i></div>
                <h5><i class="fas fa-hourglass-half me-2"></i> Menunggu</h5>
                <h2><?php echo $totalMenunggu; ?></h2>
                <p>Memerlukan validasi</p>
            </div>
        </div>

        <div class="col-md-3 mb-3">
            <div class="dashboard-card card-ditolak">
                <div class="dashboard-card-icon"><i class="fas fa-times-circle"></i></div>
                <h5><i class="fas fa-exclamation-triangle me-2"></i> Ditolak</h5>
                <h2><?php echo $totalDitolak; ?></h2>
                <p>Pendaftaran tidak sesuai</p>
            </div>
        </div>

        <div class="col-md-3 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> Total Sekolah</h5>
                    <h2><?php echo $totalSekolah; ?></h2>
                    <p>Semua data sekolah</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);
                                while($row = $result->fetch_assoc()) {
                                    echo "<tr><td>{$row['ID']}</td><td>{$row['Bidang_Lomba']}</td><td>{$row['total_peserta']}</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><td>{$row['nama_lengkap']}</td><td>{$row['email']}</td><td>" . ($row["Bidang_Lomba"] ? $row["Bidang_Lomba"] : "Belum ditentukan") . "</td></tr>";
                                    }
                                } else {
                                    echo "<tr><td colspan='3' class='text-center'>Tidak ada data</td></tr>";
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php include 'sidebar-footer.php'; ?>
    <?php $conn->close(); ?>
</body>
</html>