<?php
require_once '../includes/auth.php';

if (!isPenguji()) {
    redirect('../login.php');
}

$penguji_id = $_SESSION['user_id'];
$ujian_id = $_GET['ujian_id'] ?? null;
$station_id = $_GET['station_id'] ?? null;

if (!$ujian_id || !$station_id) {
    redirect('dashboard_penguji.php');
}

// Validasi apakah station ini diassign ke penguji dengan peran yang benar
$stmt = $pdo->prepare("
    SELECT 
        s.*, 
        u.nama_ujian, 
        us.urutan,
        ps.peran
    FROM stations s 
    INNER JOIN ujian_stations us ON s.id = us.station_id 
    INNER JOIN ujian u ON us.ujian_id = u.id 
    INNER JOIN penguji_station ps ON s.id = ps.station_id 
    WHERE s.id = ? 
    AND us.ujian_id = ? 
    AND ps.penguji_id = ? 
    AND ps.ujian_id = ?
    AND u.status = 'aktif'
");
$stmt->execute([$station_id, $ujian_id, $penguji_id, $ujian_id]);
$station = $stmt->fetch();

// Simpan total_maksimal
$total_maksimal = $station['total_maksimal'] ?? 0;

if (!$station) {
    redirect('dashboard_penguji.php');
}

// ============================================
// AMBIL PESERTA DENGAN STATUS PENILAIAN
// ============================================
$stmt = $pdo->prepare("
    SELECT 
        p.*,
        (
            SELECT COUNT(*) 
            FROM penilaian n 
            WHERE n.peserta_id = p.id 
            AND n.station_id = ? 
            AND n.ujian_id = ? 
            AND n.penguji_id = ?
            AND n.nilai IS NOT NULL
        ) as jumlah_aspek_dinilai,
        (
            SELECT COUNT(*) 
            FROM aspek_penilaian ap 
            WHERE ap.station_id = ?
        ) as total_aspek,
        (
            SELECT n.status_penilaian 
            FROM penilaian n 
            WHERE n.peserta_id = p.id 
            AND n.station_id = ? 
            AND n.ujian_id = ? 
            AND n.penguji_id = ?
            ORDER BY n.created_at DESC 
            LIMIT 1
        ) as status_penilaian
    FROM peserta p 
    INNER JOIN peserta_station ps ON p.id = ps.peserta_id 
    WHERE ps.station_id = ? AND ps.ujian_id = ?
    ORDER BY p.nama
");
$stmt->execute([
    $station_id, $ujian_id, $penguji_id,
    $station_id,
    $station_id, $ujian_id, $penguji_id,
    $station_id, $ujian_id
]);
$peserta_list = $stmt->fetchAll();

// ============================================
// PAGINATION UNTUK PENILAIAN TERBARU
// ============================================
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$limit = 10;
$offset = ($page - 1) * $limit;

// Hitung total data penilaian
$stmt_count = $pdo->prepare("
    SELECT COUNT(DISTINCT CONCAT(n.peserta_id, '|', n.created_at)) as total
    FROM penilaian n
    WHERE n.penguji_id = ? AND n.ujian_id = ? AND n.station_id = ?
");
$stmt_count->execute([$penguji_id, $ujian_id, $station_id]);
$total_data = $stmt_count->fetchColumn();
$total_pages = ceil($total_data / $limit);

// Ambil penilaian terbaru dengan pagination - MENGGUNAKAN CARA KETIGA
$stmt = $pdo->prepare("
    SELECT 
        p.nama as peserta_nama,
        p.nim,
        s.nama_station,
        SUM(n.nilai) as total_nilai,
        MAX(n.created_at) as terakhir_dinilai,
        pg.global_performance
    FROM penilaian n
    INNER JOIN peserta p ON n.peserta_id = p.id
    INNER JOIN stations s ON n.station_id = s.id
    LEFT JOIN penilaian_global pg ON (
        n.ujian_id = pg.ujian_id 
        AND n.peserta_id = pg.peserta_id 
        AND n.station_id = pg.station_id 
        AND n.penguji_id = pg.penguji_id
    )
    WHERE n.penguji_id = ? AND n.ujian_id = ? AND n.station_id = ?
    GROUP BY p.id, p.nama, p.nim, s.nama_station, pg.global_performance
    ORDER BY terakhir_dinilai DESC
    LIMIT {$limit} OFFSET {$offset}
");
$stmt->execute([$penguji_id, $ujian_id, $station_id]);
$penilaian_terbaru = $stmt->fetchAll();

// Ambil data penguji dari database berdasarkan session
$stmt = $pdo->prepare("
    SELECT p.nama 
    FROM penguji p 
    WHERE p.id = ? OR p.nip IN (SELECT username FROM users WHERE id = ?)
");
$stmt->execute([$penguji_id, $penguji_id]);
$penguji_data = $stmt->fetch();

$nama_penguji = $penguji_data['nama'] ?? $_SESSION['user_name'];
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Daftar Peserta | SOCA OSCE System</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;14..32,400;14..32,500;14..32,600;14..32,700;14..32,800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #eef2f6 100%);
            min-height: 100vh;
        }

        .dashboard-wrapper {
            display: flex;
            min-height: 100vh;
        }

        .sidebar {
            width: 280px;
            background: linear-gradient(180deg, #1a1f2e 0%, #0f1119 100%);
            color: white;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            z-index: 100;
            box-shadow: 4px 0 20px rgba(0, 0, 0, 0.08);
        }

        .sidebar-header {
            padding: 1.8rem 1.5rem;
            border-bottom: 1px solid rgba(255, 255, 255, 0.08);
            text-align: center;
        }

        .logo-area {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
            margin-bottom: 0.5rem;
        }

        .logo-icon {
            width: 45px;
            height: 45px;
            background: linear-gradient(135deg, #667eea, #1958e1);
            border-radius: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.5rem;
        }

        .logo-text h3 {
            font-size: 1.2rem;
            font-weight: 700;
            letter-spacing: -0.3px;
            background: linear-gradient(135deg, #fff, #1958e1);
            background-clip: text;
            -webkit-background-clip: text;
            color: transparent;
        }

        .logo-text p {
            font-size: 0.7rem;
            opacity: 0.7;
            margin-top: 2px;
        }

        .user-info-sidebar {
            padding: 1.2rem;
            background: rgba(255, 255, 255, 0.05);
            margin: 1rem;
            border-radius: 16px;
            text-align: center;
        }

        .user-avatar {
            width: 55px;
            height: 55px;
            background: linear-gradient(135deg, #667eea, #1958e1);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 10px;
            font-size: 1.5rem;
        }

        .nav-menu {
            padding: 1rem 0;
        }

        .nav-item {
            padding: 0.75rem 1.5rem;
            margin: 0.25rem 0.75rem;
            border-radius: 12px;
            display: flex;
            align-items: center;
            gap: 12px;
            color: rgba(255, 255, 255, 0.7);
            transition: all 0.2s;
            text-decoration: none;
        }

        .nav-item:hover, .nav-item.active {
            background: rgba(102, 126, 234, 0.2);
            color: white;
        }

        .nav-item i {
            width: 24px;
            font-size: 1.1rem;
        }

        .nav-item.logout {
            margin-top: 2rem;
            color: rgba(239, 68, 68, 0.7);
        }

        .nav-item.logout:hover {
            background: rgba(239, 68, 68, 0.2);
            color: #ef4444;
        }

        .main-content {
            flex: 1;
            margin-left: 280px;
            padding: 1.5rem 2rem 2rem;
        }

        .top-navbar {
            background: white;
            border-radius: 20px;
            padding: 1rem 1.8rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
            border: 1px solid rgba(0, 0, 0, 0.05);
        }

        .page-title h2 {
            font-size: 1.5rem;
            font-weight: 700;
            background: linear-gradient(135deg, #1e293b, #334155);
            background-clip: text;
            -webkit-background-clip: text;
            color: transparent;
        }

        .user-dropdown {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-name {
            font-weight: 600;
            color: #1e293b;
        }

        .user-badge {
            background: linear-gradient(135deg, #667eea, #1958e1);
            color: white;
            padding: 0.4rem 1.2rem;
            border-radius: 30px;
            font-size: 0.75rem;
            font-weight: 600;
        }

        .card {
            background: white;
            border-radius: 24px;
            padding: 1.8rem;
            margin-bottom: 2rem;
            box-shadow: 0 4px 14px rgba(0, 0, 0, 0.05);
            border: 1px solid rgba(0, 0, 0, 0.04);
        }

        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 0 1.2rem 0;
            border-bottom: 2px solid #f1f5f9;
            margin-bottom: 1.5rem;
            flex-wrap: wrap;
            gap: 1rem;
        }

        .card-header h3 {
            font-size: 1.1rem;
            font-weight: 700;
            color: #0f172a;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .card-header h3 i {
            color: #1958e1;
        }

        .station-banner {
            background: linear-gradient(135deg, #667eea, #1958e1);
            border-radius: 20px;
            padding: 1.5rem 2rem;
            color: white;
            margin-bottom: 2rem;
        }

        .station-banner .station-name {
            font-size: 1.5rem;
            font-weight: 700;
        }

        .station-banner .station-detail {
            font-size: 0.95rem;
            opacity: 0.9;
            margin-top: 0.3rem;
        }

        .station-banner .station-detail i {
            margin-right: 8px;
        }

        .btn {
            padding: 0.6rem 1.2rem;
            border-radius: 12px;
            font-weight: 600;
            font-size: 0.85rem;
            cursor: pointer;
            transition: all 0.2s;
            border: none;
            font-family: 'Inter', sans-serif;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            text-decoration: none;
        }

        .btn-secondary {
            background: #f1f5f9;
            color: #475569;
        }

        .btn-secondary:hover {
            background: #e2e8f0;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea, #1958e1);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
        }

        /* Stats Badge */
        .stats-badges {
            display: flex;
            gap: 0.8rem;
            flex-wrap: wrap;
        }

        .stat-badge {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 0.4rem 1rem;
            border-radius: 30px;
            font-size: 0.75rem;
            font-weight: 600;
        }

        .stat-total {
            background: #dbeafe;
            color: #1e40af;
        }

        .stat-sudah {
            background: #d1fae5;
            color: #065f46;
        }

        .stat-sedang {
            background: #fef3c7;
            color: #92400e;
        }

        .stat-belum {
            background: #fee2e2;
            color: #991b1b;
        }

        /* Search Box */
        .search-box {
            position: relative;
            display: inline-block;
        }

        .search-input {
            padding: 0.6rem 1rem 0.6rem 2.5rem;
            border: 2px solid #e2e8f0;
            border-radius: 30px;
            font-size: 0.85rem;
            width: 220px;
            transition: all 0.3s;
            background: #f8fafc;
            font-family: 'Inter', sans-serif;
        }

        .search-input:focus {
            outline: none;
            border-color: #1958e1;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
            width: 260px;
            background: white;
        }

        .search-icon {
            position: absolute;
            left: 12px;
            top: 50%;
            transform: translateY(-50%);
            color: #94a3b8;
            font-size: 0.9rem;
        }

        .search-clear {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            background: none;
            border: none;
            color: #94a3b8;
            cursor: pointer;
            font-size: 1.2rem;
            display: none;
            padding: 0;
            width: 20px;
            height: 20px;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
        }

        .search-clear:hover {
            color: #ef4444;
        }

        .search-highlight {
            background: #20ea2e;
            padding: 0 2px;
            border-radius: 3px;
        }

        /* Peserta Grid */
        .peserta-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 1.2rem;
        }

        .peserta-card {
            background: white;
            border: 2px solid #eef2ff;
            border-radius: 16px;
            padding: 1.5rem;
            transition: all 0.3s;
            text-decoration: none;
            color: inherit;
            display: block;
            position: relative;
        }

        .peserta-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 12px 25px -10px rgba(0, 0, 0, 0.1);
            border-color: #c7d2fe;
        }

        .peserta-card.sudah-dinilai {
            border-color: #86efac;
            background: #f0fdf4;
        }

        .peserta-card.sudah-dinilai:hover {
            border-color: #4ade80;
        }

        .peserta-card.sedang-dinilai {
            border-color: #93c5fd;
            background: #eff6ff;
        }

        .peserta-card.sedang-dinilai:hover {
            border-color: #60a5fa;
        }

        .peserta-card.belum-dinilai {
            border-color: #fca5a5;
            background: #fef2f2;
        }

        .peserta-card.belum-dinilai:hover {
            border-color: #f87171;
        }

        .peserta-card.hidden {
            display: none;
        }

        .peserta-nim {
            font-size: 0.8rem;
            color: #64748b;
            font-weight: 500;
            font-family: 'Courier New', monospace;
        }

        .peserta-nama {
            font-size: 1.1rem;
            font-weight: 700;
            color: #0f172a;
            margin-top: 0.2rem;
        }

        .peserta-status {
            margin-top: 0.8rem;
        }

        .badge {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 0.3rem 0.8rem;
            border-radius: 30px;
            font-size: 0.7rem;
            font-weight: 600;
        }

        .badge-success {
            background: #d1fae5;
            color: #065f46;
        }

        .badge-warning {
            background: #fef3c7;
            color: #92400e;
        }

        .badge-info {
            background: #dbeafe;
            color: #1e40af;
        }

        .badge-info i {
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .btn-action {
            display: block;
            width: 100%;
            margin-top: 1rem;
            padding: 0.6rem;
            border-radius: 10px;
            font-weight: 600;
            font-size: 0.85rem;
            text-align: center;
            transition: all 0.3s;
            border: none;
            cursor: pointer;
            font-family: 'Inter', sans-serif;
        }

        .btn-action-primary {
            background: linear-gradient(135deg, #667eea, #1958e1);
            color: white;
        }

        .btn-action-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
        }

        .btn-action-success {
            background: #10b981;
            color: white;
        }

        .btn-action-success:hover {
            background: #059669;
            transform: translateY(-2px);
        }

        /* Table Styles */
        .table-container {
            overflow-x: auto;
            border-radius: 16px;
            border: 1px solid #eef2ff;
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 0.85rem;
        }

        .data-table thead tr {
            background: linear-gradient(135deg, #5572f0, #1958e1);
            color: white;
        }

        .data-table th {
            padding: 0.9rem 0.8rem;
            text-align: left;
            font-weight: 600;
            font-size: 0.75rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .data-table td {
            padding: 0.9rem 0.8rem;
            border-bottom: 1px solid #0f0f0f;
            vertical-align: middle;
        }

        .data-table tbody tr:hover {
            background: #d5dff3;
        }

        .data-table tbody tr:last-child td {
            border-bottom: none;
        }

        .nilai-badge {
            display: inline-block;
            padding: 0.3rem 0.8rem;
            border-radius: 30px;
            font-weight: 700;
            font-size: 0.85rem;
            background: linear-gradient(135deg, #10b981, #059669);
            color: white;
        }

        .global-performance-badge {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 0.3rem 0.8rem;
            border-radius: 30px;
            font-size: 0.7rem;
            font-weight: 600;
        }

        .badge-danger {
            background: #fee2e2;
            color: #991b1b;
        }

        .badge-secondary {
            background: #e2e8f0;
            color: #475569;
        }

        .btn-edit-penilaian {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 0.4rem 0.8rem;
            border-radius: 8px;
            background: #dbeafe;
            color: #1e40af;
            text-decoration: none;
            font-size: 0.75rem;
            font-weight: 600;
            transition: all 0.2s;
        }

        .btn-edit-penilaian:hover {
            background: #3b82f6;
            color: white;
        }

        .alert-box {
            padding: 1rem 1.2rem;
            border-radius: 16px;
            border-left: 4px solid;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .alert-box.info {
            background: #dbeafe;
            border-color: #3b82f6;
            color: #1e40af;
        }

        .alert-box i {
            font-size: 1.2rem;
        }

        .empty-state {
            text-align: center;
            padding: 3rem 1rem;
        }

        .empty-state i {
            font-size: 4rem;
            color: #cbd5e1;
            margin-bottom: 1rem;
        }

        .empty-state h3 {
            font-size: 1.3rem;
            color: #0f172a;
            margin-bottom: 0.5rem;
        }

        .empty-state p {
            color: #64748b;
        }

        .empty-search {
            text-align: center;
            padding: 2rem;
            color: #64748b;
            background: #f8fafc;
            border-radius: 16px;
            margin-top: 1rem;
        }

        .empty-search i {
            font-size: 2rem;
            color: #94a3b8;
            margin-bottom: 0.5rem;
        }

        .text-muted {
            color: #64748b;
        }

        /* Responsive */
        @media (max-width: 1024px) {
            .sidebar { width: 250px; }
            .main-content { margin-left: 250px; }
        }

        @media (max-width: 768px) {
            .sidebar { transform: translateX(-100%); position: fixed; }
            .main-content { margin-left: 0; padding: 1rem; }
            .top-navbar { flex-direction: column; gap: 0.5rem; text-align: center; }
            .card-header { flex-direction: column; align-items: stretch; }
            .peserta-grid { grid-template-columns: 1fr; }
            .stats-badges { justify-content: space-between; }
            .stat-badge { flex: 1; justify-content: center; font-size: 0.65rem; padding: 0.3rem 0.5rem; }
            .search-box { width: 100%; }
            .search-input { width: 100%; }
            .search-input:focus { width: 100%; }
            .station-banner { padding: 1rem; }
            .station-banner .station-name { font-size: 1.2rem; }
        }

        ::-webkit-scrollbar { width: 6px; height: 6px; }
        ::-webkit-scrollbar-track { background: #f1f5f9; border-radius: 10px; }
        ::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 10px; }

        /* Pagination Styles */
.pagination-wrapper {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid #e2e8f0;
}

.pagination-info {
    font-size: 0.85rem;
    color: #64748b;
}

.pagination-info strong {
    color: #0f172a;
}

.pagination-buttons {
    display: flex;
    gap: 0.3rem;
    flex-wrap: wrap;
}

.pagination-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 36px;
    height: 36px;
    padding: 0 0.8rem;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    background: white;
    color: #475569;
    font-weight: 600;
    font-size: 0.85rem;
    text-decoration: none;
    transition: all 0.2s;
    font-family: 'Inter', sans-serif;
    cursor: pointer;
}

.pagination-btn:hover {
    border-color: #667eea;
    color: #667eea;
    background: #f8f9ff;
}

.pagination-btn.active {
    background: linear-gradient(135deg, #667eea, #1958e1);
    border-color: #667eea;
    color: white;
}

.pagination-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.pagination-btn:disabled:hover {
    border-color: #e2e8f0;
    color: #475569;
    background: white;
}

.pagination-btn i {
    font-size: 0.8rem;
}

.pagination-ellipsis {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 36px;
    height: 36px;
    color: #94a3b8;
    font-weight: 600;
}
    </style>
</head>
<body>
<div class="dashboard-wrapper">
    <!-- Sidebar -->
    <aside class="sidebar">
        <div class="sidebar-header">
            <div class="logo-area">
                <div class="logo-icon"><i class="fas fa-tooth"></i></div>
                <div class="logo-text"><h3>OSCE</h3><p>Penguji Panel</p></div>
            </div>
        </div>
        <div class="user-info-sidebar">
            <div class="user-avatar"><i class="fas fa-user-tie"></i></div>
            <div style="font-weight: 600;"><?php echo htmlspecialchars($nama_penguji); ?></div>
            <div style="font-size: 0.7rem; opacity: 0.7;">Penguji</div>
        </div>
        <div class="nav-menu">
            <a href="dashboard_penguji.php" class="nav-item"><i class="fas fa-tachometer-alt"></i> <span>Dashboard</span></a>
            <a href="edit_profil_penguji.php" class="nav-item"><i class="fas fa-user-edit"></i> <span>Edit Profil</span></a>
            <a href="../logout.php" class="nav-item logout"><i class="fas fa-sign-out-alt"></i> <span>Logout</span></a>
        </div>
    </aside>

    <!-- Main Content -->
    <main class="main-content">
        <div class="top-navbar">
            <div class="page-title">
                <h2><i class="fas fa-users" style="margin-right: 10px; color: #667eea;"></i>Daftar Peserta</h2>
            </div>
            <div class="user-dropdown">
                <span class="user-badge"><i class="fas fa-user-tie"></i> Penguji</span>
                <span class="user-name"><?php echo htmlspecialchars($nama_penguji); ?></span>
            </div>
        </div>

        <!-- Station Banner -->
        <div class="station-banner">
            <div class="station-name"><?php echo htmlspecialchars($station['nama_station']); ?></div>
            <div class="station-detail">
                <i class="fas fa-calendar-alt"></i> <?php echo htmlspecialchars($station['nama_ujian']); ?>
                <span style="margin: 0 10px;">|</span>
                <i class="fas fa-hashtag"></i> Station <?php echo $station['urutan']; ?>
                <span style="margin: 0 10px;">|</span>
                <i class="fas fa-flag-checkered"></i> <?php echo $station['peran'] ?? 'Penguji'; ?>
            </div>
        </div>

        <!-- Navigation -->
        <div style="margin-bottom: 1.5rem;">
            <a href="dashboard_penguji.php" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Kembali ke Dashboard
            </a>
        </div>

        <!-- Daftar Peserta -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fas fa-users"></i> Daftar Peserta</h3>
                <div style="display: flex; gap: 15px; flex-wrap: wrap; align-items: center;">
                    <?php 
                        $total_peserta = count($peserta_list);
                        $sudah_dinilai = 0;
                        $sedang_dinilai = 0;
                        $belum_dinilai = 0;

                        foreach ($peserta_list as $p) {
                            $jumlah_aspek_dinilai = $p['jumlah_aspek_dinilai'] ?? 0;
                            $total_aspek = $p['total_aspek'] ?? 0;
                            $status_penilaian = $p['status_penilaian'] ?? '';
                            
                            if ($total_aspek > 0 && $jumlah_aspek_dinilai >= $total_aspek) {
                                $sudah_dinilai++;
                            } elseif ($jumlah_aspek_dinilai > 0 || $status_penilaian == 'mulai_penilaian') {
                                $sedang_dinilai++;
                            } else {
                                $belum_dinilai++;
                            }
                        }
                    ?>
                    <div class="stats-badges">
                        <span class="stat-badge stat-total"><i class="fas fa-users"></i> Total: <?php echo $total_peserta; ?></span>
                        <span class="stat-badge stat-sudah"><i class="fas fa-check-circle"></i> Selesai: <?php echo $sudah_dinilai; ?></span>
                        <span class="stat-badge stat-sedang"><i class="fas fa-spinner fa-pulse"></i> Proses: <?php echo $sedang_dinilai; ?></span>
                        <span class="stat-badge stat-belum"><i class="fas fa-clock"></i> Belum: <?php echo $belum_dinilai; ?></span>
                    </div>
                    <div class="search-box">
                        <input type="text" id="searchPeserta" class="search-input" placeholder="Cari nama atau NIM..." onkeyup="filterPeserta()">
                        <i class="fas fa-search search-icon"></i>
                        <button type="button" class="search-clear" onclick="clearSearch()">×</button>
                    </div>
                </div>
            </div>

            <p class="text-muted" style="font-size: 0.85rem; margin-bottom: 1.2rem;">
                <i class="fas fa-info-circle" style="color: #667eea;"></i>
                Klik pada kartu peserta untuk memulai atau melanjutkan penilaian.
            </p>

            <?php if (empty($peserta_list)): ?>
                <div class="empty-state">
                    <i class="fas fa-user-friends"></i>
                    <h3>Tidak Ada Peserta</h3>
                    <p>Belum ada peserta yang diassign ke station ini.</p>
                </div>
            <?php else: ?>
                <div class="peserta-grid" id="pesertaGrid">
                    <?php foreach ($peserta_list as $peserta):
                        $jumlah_aspek_dinilai = $peserta['jumlah_aspek_dinilai'] ?? 0;
                        $total_aspek = $peserta['total_aspek'] ?? 0;
                        $status_penilaian = $peserta['status_penilaian'] ?? '';
                        
                        if ($total_aspek > 0 && $jumlah_aspek_dinilai >= $total_aspek) {
                            $card_class = 'sudah-dinilai';
                            $status_text = 'Selesai';
                            $status_class = 'badge-success';
                            $status_icon = 'fas fa-check-circle';
                            $btn_class = 'btn-action-success';
                            $btn_text = '<i class="fas fa-edit"></i> Edit Penilaian';
                        } elseif ($jumlah_aspek_dinilai > 0 || $status_penilaian == 'mulai_penilaian') {
                            $card_class = 'sedang-dinilai';
                            $status_text = 'Sedang Dinilai';
                            $status_class = 'badge-info';
                            $status_icon = 'fas fa-spinner fa-pulse';
                            $btn_class = 'btn-action-primary';
                            $btn_text = '<i class="fas fa-play-circle"></i> Lanjutkan Penilaian';
                        } else {
                            $card_class = 'belum-dinilai';
                            $status_text = 'Belum Dinilai';
                            $status_class = 'badge-warning';
                            $status_icon = 'fas fa-clock';
                            $btn_class = 'btn-action-primary';
                            $btn_text = '<i class="fas fa-play-circle"></i> Mulai Penilaian';
                        }
                    ?>
                        <a href="penilaian_detail.php?ujian_id=<?php echo $ujian_id; ?>&station_id=<?php echo $station_id; ?>&peserta_id=<?php echo $peserta['id']; ?>" 
                           class="peserta-card <?php echo $card_class; ?>" 
                           data-nama="<?php echo strtolower(htmlspecialchars($peserta['nama'])); ?>"
                           data-nim="<?php echo strtolower(htmlspecialchars($peserta['nim'])); ?>">
                            <div class="peserta-nim"><?php echo htmlspecialchars($peserta['nim']); ?></div>
                            <div class="peserta-nama"><?php echo htmlspecialchars($peserta['nama']); ?></div>
                            <div class="peserta-status">
                                <span class="badge <?php echo $status_class; ?>">
                                    <i class="<?php echo $status_icon; ?>"></i> <?php echo $status_text; ?>
                                </span>
                            </div>
                            <div class="btn-action <?php echo $btn_class; ?>">
                                <?php echo $btn_text; ?>
                            </div>
                        </a>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>

        <!-- Penilaian Terbaru -->
<div class="card">
    <div class="card-header">
        <h3><i class="fas fa-history"></i> Penilaian Terbaru</h3>
        <div style="display: flex; gap: 0.8rem; align-items: center; flex-wrap: wrap;">
            <span class="stat-badge stat-total">
                <i class="fas fa-list"></i> <?php echo $total_data; ?> Record
            </span>
            <?php if ($total_data > $limit): ?>
                <span class="stat-badge stat-sedang">
                    <i class="fas fa-layer-group"></i> Halaman <?php echo $page; ?> dari <?php echo $total_pages; ?>
                </span>
            <?php endif; ?>
        </div>
    </div>

    <?php if (empty($penilaian_terbaru)): ?>
        <div class="alert-box info">
            <i class="fas fa-info-circle"></i>
            <span>Belum ada penilaian untuk station ini.</span>
        </div>
    <?php else: ?>
        <div class="table-container">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>Peserta</th>
                        <th>NIM</th>
                        <th>Nilai</th>
                        <th>Global Performance</th>
                        <th>Waktu</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $start_number = $offset + 1;
                    foreach ($penilaian_terbaru as $index => $nilai):
                        $no = $start_number + $index;
                        $stmt_find = $pdo->prepare("SELECT id FROM peserta WHERE nim = ?");
                        $stmt_find->execute([$nilai['nim']]);
                        $peserta_find = $stmt_find->fetch();
                        $peserta_id = $peserta_find ? $peserta_find['id'] : '';

                        $gp_class = 'badge-secondary';
                        $gp_text = 'Belum Dinilai';
                        if (!empty($nilai['global_performance'])) {
                            switch($nilai['global_performance']) {
                                case 'lulus': $gp_class = 'badge-success'; $gp_text = 'Lulus'; break;
                                case 'borderline': $gp_class = 'badge-warning'; $gp_text = 'Borderline'; break;
                                case 'tidak_lulus': $gp_class = 'badge-danger'; $gp_text = 'Tidak Lulus'; break;
                            }
                        }
                    ?>
                        <tr>
                            <td><?php echo $no; ?></td>
                            <td><strong><?php echo htmlspecialchars($nilai['peserta_nama']); ?></strong></td>
                            <td><code style="background: #f1f5f9; padding: 0.2rem 0.6rem; border-radius: 4px; font-size: 0.8rem;"><?php echo htmlspecialchars($nilai['nim']); ?></code></td>
                            <td><span class="nilai-badge"><?php echo $nilai['total_nilai']; ?>/<?php echo $total_maksimal; ?></span></td>
                            <td>
                                <span class="global-performance-badge <?php echo $gp_class; ?>">
                                    <?php if ($gp_text == 'Lulus'): ?><i class="fas fa-check-circle"></i><?php endif; ?>
                                    <?php if ($gp_text == 'Borderline'): ?><i class="fas fa-exclamation-triangle"></i><?php endif; ?>
                                    <?php if ($gp_text == 'Tidak Lulus'): ?><i class="fas fa-times-circle"></i><?php endif; ?>
                                    <?php if ($gp_text == 'Belum Dinilai'): ?><i class="fas fa-clock"></i><?php endif; ?>
                                    <?php echo $gp_text; ?>
                                </span>
                            </td>
                            <td><span style="font-size: 0.8rem; color: #64748b;"><?php echo date('d/m/Y H:i', strtotime($nilai['terakhir_dinilai'])); ?></span></td>
                            <td>
                                <?php if ($peserta_id): ?>
                                    <a href="penilaian_detail.php?ujian_id=<?php echo $ujian_id; ?>&station_id=<?php echo $station_id; ?>&peserta_id=<?php echo $peserta_id; ?>" class="btn-edit-penilaian">
                                        <i class="fas fa-edit"></i> Edit
                                    </a>
                                <?php else: ?>
                                    <span class="text-muted">-</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        <?php if ($total_pages > 1): ?>
            <div class="pagination-wrapper">
                <div class="pagination-info">
                    <i class="fas fa-database"></i>
                    Menampilkan <strong><?php echo $offset + 1; ?></strong> - 
                    <strong><?php echo min($offset + $limit, $total_data); ?></strong> 
                    dari <strong><?php echo $total_data; ?></strong> record
                </div>
                <div class="pagination-buttons">
                    <!-- Tombol Previous -->
                    <a href="?ujian_id=<?php echo $ujian_id; ?>&station_id=<?php echo $station_id; ?>&page=<?php echo max(1, $page - 1); ?>" 
                       class="pagination-btn <?php echo $page <= 1 ? 'disabled' : ''; ?>"
                       <?php echo $page <= 1 ? 'onclick="return false;"' : ''; ?>>
                        <i class="fas fa-chevron-left"></i>
                    </a>
                    
                    <?php
                    // Tentukan range halaman yang ditampilkan
                    $start_page = max(1, $page - 2);
                    $end_page = min($total_pages, $page + 2);
                    
                    if ($start_page > 1) {
                        echo '<a href="?ujian_id=' . $ujian_id . '&station_id=' . $station_id . '&page=1" class="pagination-btn">1</a>';
                        if ($start_page > 2) {
                            echo '<span class="pagination-ellipsis">…</span>';
                        }
                    }
                    
                    for ($i = $start_page; $i <= $end_page; $i++) {
                        $active_class = ($i == $page) ? 'active' : '';
                        echo '<a href="?ujian_id=' . $ujian_id . '&station_id=' . $station_id . '&page=' . $i . '" class="pagination-btn ' . $active_class . '">' . $i . '</a>';
                    }
                    
                    if ($end_page < $total_pages) {
                        if ($end_page < $total_pages - 1) {
                            echo '<span class="pagination-ellipsis">…</span>';
                        }
                        echo '<a href="?ujian_id=' . $ujian_id . '&station_id=' . $station_id . '&page=' . $total_pages . '" class="pagination-btn">' . $total_pages . '</a>';
                    }
                    ?>
                    
                    <!-- Tombol Next -->
                    <a href="?ujian_id=<?php echo $ujian_id; ?>&station_id=<?php echo $station_id; ?>&page=<?php echo min($total_pages, $page + 1); ?>" 
                       class="pagination-btn <?php echo $page >= $total_pages ? 'disabled' : ''; ?>"
                       <?php echo $page >= $total_pages ? 'onclick="return false;"' : ''; ?>>
                        <i class="fas fa-chevron-right"></i>
                    </a>
                </div>
            </div>
        <?php endif; ?>

        <!-- Legend -->
        <div style="margin-top: 1rem; padding: 1rem; background: #f8fafc; border-radius: 12px; font-size: 0.8rem; display: flex; gap: 1.5rem; flex-wrap: wrap;">
            <strong style="color: #0f172a;">Global Performance:</strong>
            <span class="global-performance-badge badge-success"><i class="fas fa-check-circle"></i> Lulus</span>
            <span class="global-performance-badge badge-warning"><i class="fas fa-exclamation-triangle"></i> Borderline</span>
            <span class="global-performance-badge badge-danger"><i class="fas fa-times-circle"></i> Tidak Lulus</span>
            <span class="global-performance-badge badge-secondary"><i class="fas fa-clock"></i> Belum Dinilai</span>
        </div>
    <?php endif; ?>
</div>
    </main>
</div>

<script>
    // Fungsi filter pencarian
    function filterPeserta() {
        const searchTerm = document.getElementById('searchPeserta').value.toLowerCase().trim();
        const clearBtn = document.querySelector('.search-clear');
        const cards = document.querySelectorAll('.peserta-card');
        const grid = document.getElementById('pesertaGrid');
        
        if (clearBtn) {
            clearBtn.style.display = searchTerm.length > 0 ? 'flex' : 'none';
        }
        
        let visibleCount = 0;
        
        cards.forEach(card => {
            const nama = card.dataset.nama || card.querySelector('.peserta-nama').textContent.toLowerCase();
            const nim = card.dataset.nim || card.querySelector('.peserta-nim').textContent.toLowerCase();
            
            if (nama.includes(searchTerm) || nim.includes(searchTerm)) {
                card.classList.remove('hidden');
                visibleCount++;
                
                if (searchTerm.length > 0) {
                    highlightText(card, searchTerm);
                } else {
                    removeHighlight(card);
                }
            } else {
                card.classList.add('hidden');
            }
        });
        
        let emptyMsg = document.querySelector('.empty-search');
        if (visibleCount === 0 && searchTerm.length > 0) {
            if (!emptyMsg) {
                emptyMsg = document.createElement('div');
                emptyMsg.className = 'empty-search';
                emptyMsg.innerHTML = '<i class="fas fa-search"></i><p>Tidak ditemukan peserta dengan nama atau NIM: <strong>' + searchTerm + '</strong></p>';
                if (grid) {
                    grid.parentNode.insertBefore(emptyMsg, grid.nextSibling);
                }
            } else {
                emptyMsg.style.display = 'block';
                emptyMsg.innerHTML = '<i class="fas fa-search"></i><p>Tidak ditemukan peserta dengan nama atau NIM: <strong>' + searchTerm + '</strong></p>';
            }
        } else {
            if (emptyMsg) {
                emptyMsg.style.display = 'none';
            }
        }
    }

    function highlightText(card, searchTerm) {
        removeHighlight(card);
        
        const namaElement = card.querySelector('.peserta-nama');
        const nimElement = card.querySelector('.peserta-nim');
        
        if (namaElement) {
            const originalNama = namaElement.textContent;
            const regex = new RegExp(`(${searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
            namaElement.innerHTML = originalNama.replace(regex, '<span class="search-highlight">$1</span>');
        }
        
        if (nimElement) {
            const originalNim = nimElement.textContent;
            const regex = new RegExp(`(${searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
            nimElement.innerHTML = originalNim.replace(regex, '<span class="search-highlight">$1</span>');
        }
    }

    function removeHighlight(card) {
        const namaElement = card.querySelector('.peserta-nama');
        const nimElement = card.querySelector('.peserta-nim');
        
        if (namaElement && namaElement.innerHTML.includes('search-highlight')) {
            namaElement.innerHTML = namaElement.textContent;
        }
        
        if (nimElement && nimElement.innerHTML.includes('search-highlight')) {
            nimElement.innerHTML = nimElement.textContent;
        }
    }

    function clearSearch() {
        const searchInput = document.getElementById('searchPeserta');
        if (searchInput) {
            searchInput.value = '';
            filterPeserta();
            searchInput.focus();
        }
    }

    // Konfirmasi sebelum memulai penilaian
    document.addEventListener('DOMContentLoaded', function() {
        const cards = document.querySelectorAll('.peserta-card');
        
        cards.forEach(card => {
            card.addEventListener('click', function(e) {
                e.preventDefault();
                
                const isSudah = this.classList.contains('sudah-dinilai');
                const nama = this.querySelector('.peserta-nama').textContent;
                const link = this.href;
                
                if (isSudah) {
                    window.location.href = link;
                    return;
                }
                
                if (confirm(`Mulai penilaian untuk:\n${nama}\n\nPastikan peserta sudah siap di station.\n\nLanjutkan?`)) {
                    window.location.href = link;
                }
            });
        });
    });
</script>
</body>
</html>