<?php
// juri_dashboard.php
require_once '../config.php';
requireRole('juri');

$conn = connectDB();
$id_cabang_lomba = $_SESSION['id_cabang_lomba'];
$domisili = $_SESSION['domisili'];

// Jika juri tidak memiliki cabang lomba yang ditugaskan
if (empty($id_cabang_lomba)) {
    $error = "Anda belum ditugaskan ke cabang lomba manapun. Silakan hubungi administrator.";
} else {
    $sql = "SELECT Bidang_Lomba FROM cabang_lomba WHERE ID = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $id_cabang_lomba);
    $stmt->execute();
    $result = $stmt->get_result();
    $cabang_lomba = $result->fetch_assoc();
    $nama_cabang_lomba = $cabang_lomba['Bidang_Lomba'];
}
?>

<!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 Juri - 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>
    </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="../juri_dashboard" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-white text-decoration-none">
                        <span class="fs-4">Panel Juri</span>
                    </a>
                    <hr>
                    <ul class="nav nav-pills flex-column mb-auto">
                        <li class="nav-item">
                            <a href="../juri_dashboard" class="nav-link active">
                                <i class="fas fa-home me-2"></i> Dashboard
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="juri_peserta_kota_kabupaten" class="nav-link">
                                <i class="fas fa-users me-2"></i> Daftar Peserta Kota/Kab
                            </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 Juri</h1>
                </div>
                
                <?php if (isset($error)): ?>
                    <div class="alert alert-warning">
                        <?php echo $error; ?>
                    </div>
                <?php else: ?>
                    <div class="alert alert-danger">
                        <i class="fas fa-info-circle me-2"></i> Anda ditugaskan sebagai juri untuk <strong><?php echo $nama_cabang_lomba; ?></strong>
                        <br>
                        <br>
                        
                        <a href="https://docs.google.com/forms/d/e/1FAIpQLScHE4qD0E74m-QHAuORcDVc12H-LFM-pz_xdy1DvBQCdrZI0Q/viewform" 
   class="btn btn-danger" target="_blank">
   <i class="fas fa-exclamation-triangle"></i> Mohon Diisi!
</a>

                        
                    </div>
                    
                    <div class="row">
                        <?php
                        // Jumlah total peserta kota dan kabupaten
                        $sql = "SELECT COUNT(*) as total FROM domisilikota WHERE ID_Cabang_Lomba = ? UNION ALL SELECT COUNT(*) as total FROM domisilikabupaten WHERE ID_Cabang_Lomba = ?";
                        $stmt = $conn->prepare($sql);
                        $stmt->bind_param("ii", $id_cabang_lomba, $id_cabang_lomba);
                        $stmt->execute();
                        $result = $stmt->get_result();
                        $rowKota = $result->fetch_assoc();
                        $rowKabupaten = $result->fetch_assoc();

                        $totalPesertaKota = $rowKota['total'];
                        $totalPesertaKabupaten = $rowKabupaten['total'];

                        // Jumlah peserta yang sudah dinilai
                        $sql = "SELECT COUNT(DISTINCT nisn_peserta) as total FROM penilaian WHERE id_juri = ? AND id_cabang_lomba = ? AND domisili IN ('kota', 'kabupaten')";
                        $stmt = $conn->prepare($sql);
                        $stmt->bind_param("ii", $_SESSION['user_id'], $id_cabang_lomba);
                        $stmt->execute();
                        $result = $stmt->get_result();
                        $row = $result->fetch_assoc();
                        $dinilaiTotal = $row['total'];

                        // Menghitung total peserta keseluruhan
                        $totalPeserta = $totalPesertaKota + $totalPesertaKabupaten;
                        $persentaseTotal = ($totalPeserta > 0) ? ($dinilaiTotal / $totalPeserta) * 100 : 0;
                        ?>
                        <div class="col-md-6">
                            <div class="dashboard-card bg-primary text-white">
                                <h5>Peserta Kota</h5>
                                <h2><?php echo $totalPesertaKota; ?></h2>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="dashboard-card bg-success text-white">
                                <h5>Peserta Kabupaten</h5>
                                <h2><?php echo $totalPesertaKabupaten; ?></h2>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row mt-4">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header bg-info text-white">
                                    <h5 class="mb-0">Ringkasan Status Penilaian</h5>
                                </div>
                                <div class="card-body">
                                    <h6>Total Progres Penilaian</h6>
                                    <div class="progress mb-3" style="height: 25px;">
                                        <div class="progress-bar bg-primary" role="progressbar" 
                                            style="width: <?php echo $persentaseTotal; ?>%;" 
                                            aria-valuenow="<?php echo $persentaseTotal; ?>" 
                                            aria-valuemin="0" 
                                            aria-valuemax="100">
                                            <?php echo round($persentaseTotal); ?>% Selesai
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            </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(); ?>
