<?php
// index.php - Tampilan peserta untuk juri
require_once '../../config.php';
requireRole('juri');

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

// 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 {
    // Dapatkan nama cabang lomba
    $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'];
    
    // Filter domisili jika ada
    $domisili_filter = isset($_GET['domisili']) ? $_GET['domisili'] : '';
    $where_domisili = "";
    if ($domisili_filter === 'kota') {
        $where_domisili = "AND p.domisili = 'kota'";
    } else if ($domisili_filter === 'kabupaten') {
        $where_domisili = "AND p.domisili = 'kabupaten'";
    }
    
    // Dapatkan semua peserta (kota dan kabupaten) untuk cabang lomba ini
    $sql = "
        SELECT 
            p.nisn as nisn,
            p.nama as nama,
            p.domisili as domisili,
            p.foto as foto,
            p.karya as karya,
            CASE 
                WHEN n.id IS NULL THEN 'Belum Dinilai'
                ELSE 'Sudah Dinilai'
            END as status,
            n.id as penilaian_id
        FROM (
            SELECT 
                dk.NISN as nisn,
                dk.Nama_Peserta as nama,
                'kota' as domisili,
                dk.Foto_Peserta as foto,
                dk.Karya as karya,
                dk.ID_Cabang_Lomba as id_cabang_lomba
            FROM 
                domisilikota dk
            WHERE 
                dk.ID_Cabang_Lomba = ?
            
            UNION ALL
            
            SELECT 
                dkb.NISN as nisn,
                dkb.Nama_Peserta as nama,
                'kabupaten' as domisili,
                dkb.Foto_Peserta as foto,
                dkb.Karya as karya,
                dkb.ID_Cabang_Lomba as id_cabang_lomba
            FROM 
                domisilikabupaten dkb
            WHERE 
                dkb.ID_Cabang_Lomba = ?
        ) as p
        LEFT JOIN penilaian n ON 
            p.nisn = n.nisn_peserta AND 
            n.id_juri = ? AND 
            n.id_cabang_lomba = ? AND
            n.domisili = p.domisili
        WHERE 
            p.id_cabang_lomba = ?
            $where_domisili
        ORDER BY 
            p.domisili ASC, 
            p.nama ASC
    ";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("iiiii", $id_cabang_lomba, $id_cabang_lomba, $juri_id, $id_cabang_lomba, $id_cabang_lomba);
    $stmt->execute();
    $result = $stmt->get_result();
    $peserta = $result->fetch_all(MYSQLI_ASSOC);
}
?>

<!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>Daftar Peserta - Sistem Penilaian FLS3N</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);
        }
        .thumbnail {
            width: 100px;
            height: 100px;
            object-fit: cover;
            border-radius: 5px;
        }
        .badge-kota {
            background-color: #17a2b8;
        }
        .badge-kabupaten {
            background-color: #28a745;
        }
        .filter-btn {
            margin-right: 5px;
            margin-bottom: 10px;
        }
        .status-belum {
            color: #dc3545;
            font-weight: bold;
        }
        .status-sudah {
            color: #28a745;
            font-weight: bold;
        }
        .btn-action {
            width: 100px;
        }
    </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="../index.php" class="nav-link">
                                <i class="fas fa-home me-2"></i> Dashboard
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="index.php" class="nav-link active">
                                <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">Daftar Peserta</h1>
                </div>
                
                <?php if (isset($error)): ?>
                    <div class="alert alert-warning">
                        <?php echo $error; ?>
                    </div>
                <?php else: ?>
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i> Anda ditugaskan sebagai juri untuk <strong><?php echo $nama_cabang_lomba; ?></strong>
                    </div>
                    
                    <!-- Filter Buttons -->
                    <div class="mb-3">
                        <a href="index.php" class="btn btn-outline-primary filter-btn <?php echo empty($domisili_filter) ? 'active' : ''; ?>">
                            Semua
                        </a>
                        <a href="index.php?domisili=kota" class="btn btn-outline-info filter-btn <?php echo $domisili_filter === 'kota' ? 'active' : ''; ?>">
                            Kota
                        </a>
                        <a href="index.php?domisili=kabupaten" class="btn btn-outline-success filter-btn <?php echo $domisili_filter === 'kabupaten' ? 'active' : ''; ?>">
                            Kabupaten
                        </a>
                    </div>
                    
                    <!-- Participants Table -->
                    <div class="card shadow-sm">
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>Foto</th>
                                            <th>Hasil Karya</th>
                                            <th>Status</th>
                                            <th>Aksi</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if (empty($peserta)): ?>
                                            <tr>
                                                <td colspan="4" class="text-center">Tidak ada peserta yang ditemukan</td>
                                            </tr>
                                        <?php else: ?>
                                            <?php foreach ($peserta as $p): ?>
                                                <tr>
                                                    <!-- Foto -->
                                                    <td>
                                                        <div class="d-flex align-items-center">
                                                            <a href="<?php echo $p['foto']; ?>" target="_blank" class="btn btn-sm btn-outline-primary me-2">
                                                                <i class="fas fa-image me-1"></i> Lihat Foto
                                                            </a>
                                                            <span class="badge <?php echo $p['domisili'] === 'kota' ? 'badge-kota' : 'badge-kabupaten'; ?> text-white">
                                                                <?php echo ucfirst($p['domisili']); ?>
                                                            </span>
                                                        </div>
                                                    </td>
                                                    
                                                    <!-- Hasil Karya -->
                                                    <td>
                                                        <?php 
                                                        // Cek apakah URL karya adalah link Google Drive, YouTube, atau lainnya
                                                        $karya_url = $p['karya'];
                                                        if (strpos($karya_url, 'drive.google.com') !== false) {
                                                            echo "<a href='{$karya_url}' target='_blank' class='btn btn-sm btn-outline-primary'>
                                                                <i class='fab fa-google-drive me-1'></i> Lihat Karya
                                                            </a>";
                                                        } elseif (strpos($karya_url, 'youtu.be') !== false || strpos($karya_url, 'youtube.com') !== false) {
                                                            echo "<a href='{$karya_url}' target='_blank' class='btn btn-sm btn-outline-danger'>
                                                                <i class='fab fa-youtube me-1'></i> Lihat Video
                                                            </a>";
                                                        } else {
                                                            echo "<a href='{$karya_url}' target='_blank' class='btn btn-sm btn-outline-secondary'>
                                                                <i class='fas fa-link me-1'></i> Lihat Karya
                                                            </a>";
                                                        }
                                                        ?>
                                                    </td>
                                                    
                                                    <!-- Status -->
                                                    <td>
                                                        <?php if ($p['status'] === 'Belum Dinilai'): ?>
                                                            <span class="status-belum">
                                                                <i class="fas fa-times-circle me-1"></i> Belum Dinilai
                                                            </span>
                                                        <?php else: ?>
                                                            <span class="status-sudah">
                                                                <i class="fas fa-check-circle me-1"></i> Sudah Dinilai
                                                            </span>
                                                        <?php endif; ?>
                                                    </td>
                                                    
                                                    <!-- Aksi -->
                                                    <td>
                                                        <?php if ($p['status'] === 'Belum Dinilai'): ?>
                                                            <a href="juri_form_penilaian.php?nisn=<?php echo $p['nisn']; ?>&domisili=<?php echo $p['domisili']; ?>" class="btn btn-primary btn-action">
                                                                <i class="fas fa-star me-1"></i> Nilai
                                                            </a>
                                                        <?php else: ?>
                                                            <a href="" class="btn btn-warning btn-action">
                                                                <i class="fas fa-lock me-1"></i>
                                                            </a>
                                                        <?php endif; ?>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </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(); ?>