<?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;
            background-color: #f8f9fa;
        }
        
        /* Dashboard Card Styling */
        .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;
            color: white;
        }

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

        .card-menunggu-penilaian {
            background: linear-gradient(135deg, #f59e0b 0%, #b45309 100%);
        }

        .card-sudah-dinilai {
            background: linear-gradient(135deg, #10b981 0%, #065f46 100%);
        }

        .card-total-peserta {
            background: linear-gradient(135deg, #3b82f6 0%, #1e3a8a 100%);
        }

        .card-cabang-lomba {
            background: linear-gradient(135deg, #8b5cf6 0%, #6d28d9 100%);
        }

        .progress-wrapper {
            background: white;
            border-radius: 10px;
            padding: 20px;
            margin-top: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }

        .progress-wrapper h5 {
            color: #333;
            margin-bottom: 15px;
            font-weight: 600;
        }
</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-info" style="background-color: #e0f2fe; color: #0c4a6e; border-color: #0ea5e9;">
                        <i class="fas fa-info-circle me-2"></i> Anda ditugaskan sebagai juri untuk <strong><?php echo $nama_cabang_lomba; ?></strong>
                    </div>

                    <!-- Google Form Banner -->
                    <div class="alert alert-warning mb-4" style="background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%); color: #7c2d12; border: none;">
                        <div class="row align-items-center">
                            <div class="col-md-10">
                                <i class="fas fa-exclamation-triangle me-2"></i> 
                                <strong>Perhatian!</strong> Pastikan untuk mengisi form evaluasi juri agar data penilaian Anda tercatat dengan lengkap.
                            </div>
                            <div class="col-md-2 text-end">
                                <a href="https://docs.google.com/forms/d/e/1FAIpQLSc5SKFkWgCRPsJ8DNpRUOk5zZnhYAiMCxuVq_aGNv0RPxfdkQ/viewform" 
                                   target="_blank" class="btn btn-danger btn-sm">
                                    <i class="fas fa-external-link-alt me-1"></i> Mohon Diisi
                                </a>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <?php
                        // Jumlah total peserta per region
                        $sql = "SELECT COUNT(*) as total FROM domisilikota WHERE ID_Cabang_Lomba = ?";
                        $stmt = $conn->prepare($sql);
                        $stmt->bind_param("i", $id_cabang_lomba);
                        $stmt->execute();
                        $rowKotaSalatiga = $stmt->get_result()->fetch_assoc();
                        $totalKotaSalatiga = $rowKotaSalatiga['total'];

                        $sql = "SELECT COUNT(*) as total FROM domisilikabupaten WHERE ID_Cabang_Lomba = ?";
                        $stmt = $conn->prepare($sql);
                        $stmt->bind_param("i", $id_cabang_lomba);
                        $stmt->execute();
                        $rowKabSemarang = $stmt->get_result()->fetch_assoc();
                        $totalKabSemarang = $rowKabSemarang['total'];

                        $sql = "SELECT COUNT(*) as total FROM domisilikabupaten2 WHERE ID_Cabang_Lomba = ?";
                        $stmt = $conn->prepare($sql);
                        $stmt->bind_param("i", $id_cabang_lomba);
                        $stmt->execute();
                        $rowKabDemak = $stmt->get_result()->fetch_assoc();
                        $totalKabDemak = $rowKabDemak['total'];

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

                        $sql = "SELECT COUNT(DISTINCT nisn_peserta) as total FROM penilaian WHERE id_juri = ? AND id_cabang_lomba = ? AND domisili = 'kabupaten_semarang'";
                        $stmt = $conn->prepare($sql);
                        $stmt->bind_param("ii", $_SESSION['user_id'], $id_cabang_lomba);
                        $stmt->execute();
                        $dinilaiKabSemarang = $stmt->get_result()->fetch_assoc()['total'];

                        $sql = "SELECT COUNT(DISTINCT nisn_peserta) as total FROM penilaian WHERE id_juri = ? AND id_cabang_lomba = ? AND domisili = 'kabupaten_demak'";
                        $stmt = $conn->prepare($sql);
                        $stmt->bind_param("ii", $_SESSION['user_id'], $id_cabang_lomba);
                        $stmt->execute();
                        $dinilaiKabDemak = $stmt->get_result()->fetch_assoc()['total'];

                        // Total keseluruhan
                        $totalPeserta = $totalKotaSalatiga + $totalKabSemarang + $totalKabDemak;
                        $totalDinilai = $dinilaiKotaSalatiga + $dinilaiKabSemarang + $dinilaiKabDemak;
                        $belumDinilai = $totalPeserta - $totalDinilai;
                        $persentase = ($totalPeserta > 0) ? ($totalDinilai / $totalPeserta) * 100 : 0;
                        ?>

                        <!-- Cards Row 1: Status Penilaian -->
                        <div class="col-md-6 mb-3">
                            <div class="dashboard-card card-menunggu-penilaian">
                                <div class="dashboard-card-icon"><i class="fas fa-hourglass-half"></i></div>
                                <h5><i class="fas fa-clock me-2"></i> Menunggu Penilaian</h5>
                                <h2><?php echo $belumDinilai; ?></h2>
                                <p>Peserta belum dinilai</p>
                            </div>
                        </div>

                        <div class="col-md-6 mb-3">
                            <div class="dashboard-card card-sudah-dinilai">
                                <div class="dashboard-card-icon"><i class="fas fa-check-circle"></i></div>
                                <h5><i class="fas fa-check me-2"></i> Sudah Dinilai</h5>
                                <h2><?php echo $totalDinilai; ?></h2>
                                <p>Peserta selesai dinilai</p>
                            </div>
                        </div>
                    </div>

                    <!-- Cards Row 2: Per Region -->
                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <a href="juri_peserta_kota_kabupaten/index.php?region=kota_salatiga" style="text-decoration: none;">
                                <div class="dashboard-card" style="background: linear-gradient(135deg, #06b6d4 0%, #0e7490 100%);">
                                    <div class="dashboard-card-icon"><i class="fas fa-users"></i></div>
                                    <h5><i class="fas fa-map-pin me-2"></i> Kota Salatiga</h5>
                                    <h2><?php echo $totalKotaSalatiga; ?></h2>
                                    <p><?php echo $dinilaiKotaSalatiga; ?> dari <?php echo $totalKotaSalatiga; ?> dinilai</p>
                                </div>
                            </a>
                        </div>

                        <div class="col-md-4 mb-3">
                            <a href="juri_peserta_kota_kabupaten/index.php?region=kabupaten_semarang" style="text-decoration: none;">
                                <div class="dashboard-card" style="background: linear-gradient(135deg, #14b8a6 0%, #0d9488 100%);">
                                    <div class="dashboard-card-icon"><i class="fas fa-users"></i></div>
                                    <h5><i class="fas fa-map-pin me-2"></i> Kab. Semarang</h5>
                                    <h2><?php echo $totalKabSemarang; ?></h2>
                                    <p><?php echo $dinilaiKabSemarang; ?> dari <?php echo $totalKabSemarang; ?> dinilai</p>
                                </div>
                            </a>
                        </div>

                        <div class="col-md-4 mb-3">
                            <a href="juri_peserta_kota_kabupaten/index.php?region=kabupaten_demak" style="text-decoration: none;">
                                <div class="dashboard-card" style="background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);">
                                    <div class="dashboard-card-icon"><i class="fas fa-users"></i></div>
                                    <h5><i class="fas fa-map-pin me-2"></i> Kab. Demak</h5>
                                    <h2><?php echo $totalKabDemak; ?></h2>
                                    <p><?php echo $dinilaiKabDemak; ?> dari <?php echo $totalKabDemak; ?> dinilai</p>
                                </div>
                            </a>
                        </div>
                    </div>

                    <!-- Progress Section -->
                    <div class="progress-wrapper">
                        <h5>Progress Penilaian Keseluruhan</h5>
                        <div class="progress" style="height: 30px;">
                            <div class="progress-bar" role="progressbar" 
                                style="width: <?php echo $persentase; ?>%; background: linear-gradient(90deg, #10b981 0%, #059669 100%);" 
                                aria-valuenow="<?php echo $persentase; ?>" 
                                aria-valuemin="0" 
                                aria-valuemax="100">
                                <span style="font-weight: 600; color: white;"><?php echo round($persentase, 1); ?>% - <?php echo $totalDinilai; ?>/<?php echo $totalPeserta; ?> Peserta</span>
                            </div>
                        </div>
                    </div>

                    <!-- Quick Action -->
                    <div class="row mt-4">
                        <div class="col-md-12">
                            <a href="juri_peserta_kota_kabupaten/" class="btn btn-primary btn-lg">
                                <i class="fas fa-clipboard-list me-2"></i> Mulai Penilaian Peserta
                            </a>
                        </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(); ?>
