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

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

$success_message = "";
$error_message = "";

// Function untuk assign peserta ke SEMUA station dalam ujian
function assignPesertaToUjian($pdo, $peserta_id, $ujian_id, $status = 'belum_mulai') {
    // Validasi ujian
    $checkUjian = $pdo->prepare("SELECT id, nama_ujian FROM ujian WHERE id = ?");
    $checkUjian->execute([$ujian_id]);
    $ujian = $checkUjian->fetch();
    
    if (!$ujian) {
        return ["error" => "Ujian tidak ditemukan"];
    }
    
    // Ambil SEMUA station untuk ujian ini
    $getStations = $pdo->prepare("
        SELECT us.station_id, us.urutan, s.nama_station 
        FROM ujian_stations us 
        JOIN stations s ON us.station_id = s.id 
        WHERE us.ujian_id = ? 
        ORDER BY us.urutan
    ");
    $getStations->execute([$ujian_id]);
    $stations = $getStations->fetchAll(PDO::FETCH_ASSOC);
    
    if (empty($stations)) {
        return ["error" => "Ujian '" . $ujian['nama_ujian'] . "' tidak memiliki station. Tambahkan station terlebih dahulu."];
    }
    
    // Cek apakah peserta sudah terdaftar di ujian ini
    $checkPeserta = $pdo->prepare("SELECT COUNT(*) FROM peserta_station WHERE peserta_id = ? AND ujian_id = ?");
    $checkPeserta->execute([$peserta_id, $ujian_id]);
    $alreadyAssigned = $checkPeserta->fetchColumn();
    
    if ($alreadyAssigned > 0) {
        return ["error" => "Peserta sudah terdaftar di ujian '" . $ujian['nama_ujian'] . "'"];
    }
    
    // Insert ke SEMUA station
    $successCount = 0;
    $errors = [];
    
    foreach ($stations as $station) {
        try {
            $stmt = $pdo->prepare("INSERT INTO peserta_station (peserta_id, station_id, ujian_id, urutan, status) 
                                  VALUES (?, ?, ?, ?, ?)");
            $stmt->execute([
                $peserta_id, 
                $station['station_id'], 
                $ujian_id, 
                $station['urutan'], 
                $status
            ]);
            $successCount++;
        } catch (PDOException $e) {
            $errors[] = "Station " . $station['nama_station'] . ": " . $e->getMessage();
        }
    }
    
    return [
        "success" => $successCount,
        "total_stations" => count($stations),
        "errors" => $errors,
        "ujian_nama" => $ujian['nama_ujian']
    ];
}

// Function untuk mass assign - assign BANYAK peserta sekaligus ke SATU ujian
function massAssignPesertaToUjian($pdo, $peserta_ids, $ujian_id, $status = 'belum_mulai') {
    // Validasi ujian
    $checkUjian = $pdo->prepare("SELECT id, nama_ujian FROM ujian WHERE id = ?");
    $checkUjian->execute([$ujian_id]);
    $ujian = $checkUjian->fetch();
    
    if (!$ujian) {
        return ["error" => "Ujian tidak ditemukan"];
    }
    
    // Ambil SEMUA station untuk ujian ini
    $getStations = $pdo->prepare("
        SELECT us.station_id, us.urutan, s.nama_station 
        FROM ujian_stations us 
        JOIN stations s ON us.station_id = s.id 
        WHERE us.ujian_id = ? 
        ORDER BY us.urutan
    ");
    $getStations->execute([$ujian_id]);
    $stations = $getStations->fetchAll(PDO::FETCH_ASSOC);
    
    if (empty($stations)) {
        return ["error" => "Ujian '" . $ujian['nama_ujian'] . "' tidak memiliki station"];
    }
    
    $total_success = 0;
    $total_errors = [];
    $assigned_peserta = [];
    $skipped_peserta = [];
    
    foreach ($peserta_ids as $peserta_id) {
        // Cek apakah peserta sudah terdaftar di ujian ini
        $checkPeserta = $pdo->prepare("SELECT COUNT(*) FROM peserta_station WHERE peserta_id = ? AND ujian_id = ?");
        $checkPeserta->execute([$peserta_id, $ujian_id]);
        $alreadyAssigned = $checkPeserta->fetchColumn();
        
        if ($alreadyAssigned > 0) {
            // Ambil nama peserta untuk info
            $getPeserta = $pdo->prepare("SELECT nama FROM peserta WHERE id = ?");
            $getPeserta->execute([$peserta_id]);
            $peserta_nama = $getPeserta->fetchColumn();
            $skipped_peserta[] = $peserta_nama;
            continue; // Skip peserta yang sudah terdaftar
        }
        
        // Insert ke semua station
        $successCount = 0;
        $errors = [];
        
        foreach ($stations as $station) {
            try {
                $stmt = $pdo->prepare("INSERT INTO peserta_station (peserta_id, station_id, ujian_id, urutan, status) 
                                      VALUES (?, ?, ?, ?, ?)");
                $stmt->execute([
                    $peserta_id, 
                    $station['station_id'], 
                    $ujian_id, 
                    $station['urutan'], 
                    $status
                ]);
                $successCount++;
            } catch (PDOException $e) {
                $errors[] = "Station " . $station['nama_station'] . ": " . $e->getMessage();
            }
        }
        
        if ($successCount > 0) {
            $total_success += $successCount;
            // Ambil nama peserta untuk info
            $getPeserta = $pdo->prepare("SELECT nama FROM peserta WHERE id = ?");
            $getPeserta->execute([$peserta_id]);
            $peserta_nama = $getPeserta->fetchColumn();
            $assigned_peserta[] = $peserta_nama;
        }
        
        if (!empty($errors)) {
            $total_errors = array_merge($total_errors, $errors);
        }
    }
    
    return [
        "success" => $total_success,
        "total_stations" => count($stations),
        "assigned_peserta" => $assigned_peserta,
        "skipped_peserta" => $skipped_peserta,
        "errors" => $total_errors,
        "ujian_nama" => $ujian['nama_ujian']
    ];
}

// Proses form assign peserta (single)
if (isset($_POST['assign_ujian'])) {
    $peserta_id = $_POST['peserta_id'];
    $ujian_id = $_POST['ujian_id'];
    $semester = $_POST['semester'] ?? '';
    
    $result = assignPesertaToUjian($pdo, $peserta_id, $ujian_id, 'belum_mulai');
    
    if (isset($result['error'])) {
        $error_message = $result['error'];
    } else {
        $success_message = "Berhasil menambahkan peserta ke " . $result['total_stations'] . " station pada ujian: " . $result['ujian_nama'];
        if (!empty($result['errors'])) {
            $error_message = "Beberapa error terjadi: " . implode(", ", $result['errors']);
        }
    }
}

// Proses mass assign (multiple peserta)
if (isset($_POST['mass_assign'])) {
    $peserta_ids = $_POST['peserta_ids'] ?? [];
    $ujian_id = $_POST['mass_ujian_id'];
    $semester = $_POST['semester'] ?? '';
    
    if (empty($peserta_ids)) {
        $error_message = "Pilih setidaknya 1 peserta";
    } else {
        $result = massAssignPesertaToUjian($pdo, $peserta_ids, $ujian_id, 'belum_mulai');
        
        if (isset($result['error'])) {
            $error_message = $result['error'];
        } else {
            $success_message = "Berhasil assign " . count($result['assigned_peserta']) . " peserta ke ujian: " . $result['ujian_nama'];
            $success_message .= " (" . $result['success'] . " assignment dibuat)";
            
            if (!empty($result['skipped_peserta'])) {
                $success_message .= "<br><small class='text-warning'>" . count($result['skipped_peserta']) . " peserta sudah terdaftar: " . implode(", ", $result['skipped_peserta']) . "</small>";
            }
            
            if (!empty($result['errors'])) {
                $error_message = "Beberapa error terjadi: " . implode(", ", $result['errors']);
            }
        }
    }
}

// Proses hapus peserta dari ujian
if (isset($_GET['delete_ujian'])) {
    $peserta_id = $_GET['peserta_id'];
    $ujian_id = $_GET['ujian_id'];
    
    try {
        $stmt = $pdo->prepare("DELETE FROM peserta_station WHERE peserta_id = ? AND ujian_id = ?");
        $stmt->execute([$peserta_id, $ujian_id]);
        $success_message = "Peserta berhasil dihapus dari ujian";
    } catch (PDOException $e) {
        $error_message = "Error menghapus peserta: " . $e->getMessage();
    }
}

// Ambil data untuk dropdown
try {
    // Daftar semester
    $semesterList = $pdo->query("SELECT DISTINCT semester FROM peserta ORDER BY semester")->fetchAll(PDO::FETCH_COLUMN);
    $selected_semester = $_POST['semester'] ?? $_GET['semester'] ?? ($semesterList[0] ?? 1);
    
    // Daftar peserta berdasarkan semester
    $pesertaList = $pdo->prepare("SELECT id, nama, nim, semester FROM peserta WHERE semester = ? ORDER BY nama");
    $pesertaList->execute([$selected_semester]);
    $pesertaList = $pesertaList->fetchAll(PDO::FETCH_ASSOC);
    
    // Daftar ujian dengan jumlah station
    $ujianList = $pdo->query("
        SELECT u.id, u.nama_ujian, u.tanggal_ujian, COUNT(us.station_id) as total_station 
        FROM ujian u 
        LEFT JOIN ujian_stations us ON u.id = us.ujian_id 
        WHERE (u.status = 'aktif' OR u.status = 'selesai')
        GROUP BY u.id
        ORDER BY u.tanggal_ujian DESC
    ")->fetchAll(PDO::FETCH_ASSOC);
    
    // Data peserta yang sudah diassign
    $assignedPeserta = $pdo->query("
        SELECT 
            ps.peserta_id,
            p.nama as peserta_nama,
            p.nim,
            p.semester,
            ps.ujian_id,
            u.nama_ujian,
            u.tanggal_ujian,
            COUNT(ps.station_id) as total_station,
            GROUP_CONCAT(s.nama_station ORDER BY ps.urutan SEPARATOR ', ') as station_list,
            MIN(ps.status) as status
        FROM peserta_station ps
        JOIN peserta p ON ps.peserta_id = p.id
        JOIN ujian u ON ps.ujian_id = u.id
        JOIN stations s ON ps.station_id = s.id
        GROUP BY ps.peserta_id, ps.ujian_id
        ORDER BY u.tanggal_ujian DESC, p.nama
    ")->fetchAll(PDO::FETCH_ASSOC);
    
} catch (PDOException $e) {
    $error_message = "Error mengambil data: " . $e->getMessage();
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kelola Peserta - SOCA OSCE System</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        .card-header {
            font-weight: 600;
        }
        .badge-station {
            background: #17a2b8;
            color: white;
            font-size: 0.8em;
        }
        .badge-count {
            background: #28a745;
            color: white;
        }
        .peserta-checkbox {
            max-height: 300px;
            overflow-y: auto;
            border: 1px solid #dee2e6;
            border-radius: 5px;
            padding: 10px;
            margin-top: 10px;
        }
        .peserta-checkbox-item {
            padding: 8px;
            border-bottom: 1px solid #f0f0f0;
            cursor: pointer;
        }
        .peserta-checkbox-item:hover {
            background: #f8f9fa;
        }
        .peserta-checkbox-item:last-child {
            border-bottom: none;
        }
        .action-buttons {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-bottom: 20px;
        }
        .action-buttons .btn {
            flex: 1;
            min-width: 150px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Main Content -->
            <main class="col-md-12 ms-sm-auto px-md-4">
                <!-- Page Header -->
                <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">
                        <i class="bi bi-people me-2"></i>Kelola Peserta Ujian
                    </h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <div class="btn-group me-2">
                            <a href="dashboard.php" class="btn btn-sm btn-outline-primary">
                                <i class="bi bi-speedometer2"></i> Dashboard
                            </a>
                            <a href="tambah_peserta.php?semester=<?= $selected_semester ?>" 
                               class="btn btn-sm btn-outline-success">
                                <i class="bi bi-person-add"></i> Tambah Peserta
                            </a>
                        </div>
                    </div>
                </div>

                <!-- Alert Messages -->
                <?php if ($success_message): ?>
                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                        <i class="bi bi-check-circle me-2"></i><?= $success_message ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                <?php endif; ?>
                
                <?php if ($error_message): ?>
                    <div class="alert alert-danger alert-dismissible fade show" role="alert">
                        <i class="bi bi-exclamation-triangle me-2"></i><?= $error_message ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                <?php endif; ?>

                <!-- Action Buttons -->
                <div class="action-buttons">
                    <?php foreach ([1, 3, 5, 7] as $sem): ?>
                        <a href="?semester=<?= $sem ?>" 
                           class="btn btn-<?= $selected_semester == $sem ? 'primary' : 'outline-primary' ?>">
                            Semester <?= $sem ?>
                        </a>
                    <?php endforeach; ?>
                </div>

                <div class="row">
                    <!-- Form Assign Single Peserta -->
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header bg-primary text-white">
                                <h5 class="card-title mb-0">
                                    <i class="bi bi-person-plus me-2"></i>Assign Peserta ke Ujian
                                </h5>
                            </div>
                            <div class="card-body">
                                <form method="POST" action="">
                                    <input type="hidden" name="semester" value="<?= $selected_semester ?>">
                                    
                                    <div class="mb-3">
                                        <label for="peserta_id" class="form-label">Pilih Peserta</label>
                                        <select name="peserta_id" id="peserta_id" class="form-select" required 
                                                <?= empty($pesertaList) ? 'disabled' : '' ?>>
                                            <option value="">-- Pilih Peserta --</option>
                                            <?php if (!empty($pesertaList)): ?>
                                                <?php foreach ($pesertaList as $peserta): ?>
                                                    <option value="<?= $peserta['id'] ?>">
                                                        <?= htmlspecialchars($peserta['nama']) ?> 
                                                        (<?= htmlspecialchars($peserta['nim']) ?>)
                                                    </option>
                                                <?php endforeach; ?>
                                            <?php else: ?>
                                                <option value="">Tidak ada peserta untuk semester <?= $selected_semester ?></option>
                                            <?php endif; ?>
                                        </select>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <label for="ujian_id" class="form-label">Pilih Ujian</label>
                                        <select name="ujian_id" class="form-select" required>
                                            <option value="">-- Pilih Ujian --</option>
                                            <?php foreach ($ujianList as $ujian): ?>
                                                <option value="<?= $ujian['id'] ?>">
                                                    <?= htmlspecialchars($ujian['nama_ujian']) ?> 
                                                    (<?= date('d/m/Y', strtotime($ujian['tanggal_ujian'])) ?>)
                                                    - <?= $ujian['total_station'] ?> station
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    
                                    <button type="submit" name="assign_ujian" class="btn btn-primary"
                                            <?= empty($pesertaList) ? 'disabled' : '' ?>>
                                        <i class="bi bi-save me-2"></i>Assign ke Ujian
                                    </button>
                                    
                                    <div class="form-text mt-2">
                                        <i class="bi bi-info-circle"></i> Peserta akan otomatis terdaftar di SEMUA station yang ada di ujian ini.
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>

                    <!-- Form Mass Assign -->
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header bg-success text-white">
                                <h5 class="card-title mb-0">
                                    <i class="bi bi-people-fill me-2"></i>Mass Assign (Multiple Peserta)
                                </h5>
                            </div>
                            <div class="card-body">
                                <form method="POST" action="" id="massAssignForm">
                                    <input type="hidden" name="semester" value="<?= $selected_semester ?>">
                                    
                                    <div class="mb-3">
                                        <label class="form-label">Pilih Peserta</label>
                                        <div class="peserta-checkbox">
                                            <?php if (!empty($pesertaList)): ?>
                                                <?php foreach ($pesertaList as $peserta): ?>
                                                    <div class="form-check peserta-checkbox-item">
                                                        <input class="form-check-input" 
                                                               type="checkbox" 
                                                               name="peserta_ids[]" 
                                                               value="<?= $peserta['id'] ?>" 
                                                               id="peserta_<?= $peserta['id'] ?>">
                                                        <label class="form-check-label" for="peserta_<?= $peserta['id'] ?>">
                                                            <?= htmlspecialchars($peserta['nama']) ?> 
                                                            (<?= htmlspecialchars($peserta['nim']) ?>)
                                                        </label>
                                                    </div>
                                                <?php endforeach; ?>
                                            <?php else: ?>
                                                <p class="text-muted">Tidak ada peserta untuk semester <?= $selected_semester ?></p>
                                            <?php endif; ?>
                                        </div>
                                        <div class="mt-2">
                                            <button type="button" class="btn btn-sm btn-outline-secondary" onclick="selectAllPeserta()">
                                                <i class="bi bi-check-all"></i> Pilih Semua
                                            </button>
                                            <button type="button" class="btn btn-sm btn-outline-secondary" onclick="deselectAllPeserta()">
                                                <i class="bi bi-x-circle"></i> Hapus Semua
                                            </button>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <label for="mass_ujian_id" class="form-label">Pilih Ujian</label>
                                        <select name="mass_ujian_id" id="mass_ujian_id" class="form-select" required>
                                            <option value="">-- Pilih Ujian --</option>
                                            <?php foreach ($ujianList as $ujian): ?>
                                                <option value="<?= $ujian['id'] ?>">
                                                    <?= htmlspecialchars($ujian['nama_ujian']) ?> 
                                                    (<?= date('d/m/Y', strtotime($ujian['tanggal_ujian'])) ?>)
                                                    - <?= $ujian['total_station'] ?> station
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    
                                    <button type="submit" name="mass_assign" class="btn btn-success"
                                            <?= empty($pesertaList) ? 'disabled' : '' ?>>
                                        <i class="bi bi-save me-2"></i>Assign Semua ke Ujian
                                    </button>
                                    
                                    <div class="form-text mt-2">
                                        <i class="bi bi-info-circle"></i> Pilih beberapa peserta untuk diassign sekaligus ke ujian yang sama.
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Daftar Peserta yang Sudah Diassign -->
                <div class="card mt-4">
                    <div class="card-header bg-info text-white">
                        <h5 class="card-title mb-0">
                            <i class="bi bi-list-check me-2"></i>Peserta yang Sudah Diassign
                            <span class="badge bg-light text-dark ms-2">Semester <?= $selected_semester ?></span>
                        </h5>
                    </div>
                    <div class="card-body">
                        <?php 
                        // Filter berdasarkan semester
                        $filteredAssigned = array_filter($assignedPeserta, function($item) use ($selected_semester) {
                            return $item['semester'] == $selected_semester;
                        });
                        ?>
                        
                        <?php if (empty($filteredAssigned)): ?>
                            <p class="text-muted text-center py-4">
                                Belum ada peserta semester <?= $selected_semester ?> yang diassign ke ujian.
                            </p>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table table-striped table-hover">
                                    <thead>
                                        <tr>
                                            <th>Peserta</th>
                                            <th>Ujian</th>
                                            <th>Station</th>
                                            <th>Status</th>
                                            <th>Aksi</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($filteredAssigned as $assign): ?>
                                            <tr>
                                                <td>
                                                    <strong><?= htmlspecialchars($assign['peserta_nama']) ?></strong><br>
                                                    <small class="text-muted"><?= htmlspecialchars($assign['nim']) ?></small>
                                                </td>
                                                <td>
                                                    <?= htmlspecialchars($assign['nama_ujian']) ?><br>
                                                    <small class="text-muted"><?= date('d/m/Y', strtotime($assign['tanggal_ujian'])) ?></small><br>
                                                    <span class="badge badge-count">
                                                        <?= $assign['total_station'] ?> Station
                                                    </span>
                                                </td>
                                                <td>
                                                    <small><?= $assign['station_list'] ?></small>
                                                </td>
                                                <td>
                                                    <?php 
                                                    $status_class = 'bg-secondary';
                                                    $status_text = 'Belum Mulai';
                                                    if ($assign['status'] === 'sedang_berlangsung') {
                                                        $status_class = 'bg-warning';
                                                        $status_text = 'Sedang Berlangsung';
                                                    } elseif ($assign['status'] === 'selesai') {
                                                        $status_class = 'bg-success';
                                                        $status_text = 'Selesai';
                                                    }
                                                    ?>
                                                    <span class="badge <?= $status_class ?>"><?= $status_text ?></span>
                                                </td>
                                                <td>
                                                    <a href="?delete_ujian=1&peserta_id=<?= $assign['peserta_id'] ?>&ujian_id=<?= $assign['ujian_id'] ?>&semester=<?= $selected_semester ?>" 
                                                       class="btn btn-danger btn-sm"
                                                       onclick="return confirm('Hapus peserta dari ujian ini?')">
                                                        <i class="bi bi-trash"></i> Hapus
                                                    </a>
                                                    <a href="detail_peserta.php?peserta_id=<?= $assign['peserta_id'] ?>&ujian_id=<?= $assign['ujian_id'] ?>" 
                                                       class="btn btn-info btn-sm">
                                                        <i class="bi bi-eye"></i> Detail
                                                    </a>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Info Box -->
                <div class="alert alert-light mt-4">
                    <h6><i class="bi bi-lightbulb me-2"></i>Cara Menggunakan:</h6>
                    <ol class="mb-0">
                        <li>Pilih semester di atas untuk melihat peserta</li>
                        <li><strong>Single Assign</strong>: Assign 1 peserta ke 1 ujian (otomatis ke semua station)</li>
                        <li><strong>Mass Assign</strong>: Assign beberapa peserta sekaligus ke 1 ujian</li>
                        <li>Peserta akan otomatis terdaftar di SEMUA station ujian yang dipilih</li>
                        <li>Jika ujian memiliki 3 station, maka peserta akan otomatis ikut 3 station</li>
                    </ol>
                </div>
            </main>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function selectAllPeserta() {
            document.querySelectorAll('input[name="peserta_ids[]"]').forEach(cb => {
                cb.checked = true;
            });
        }
        
        function deselectAllPeserta() {
            document.querySelectorAll('input[name="peserta_ids[]"]').forEach(cb => {
                cb.checked = false;
            });
        }
        
        // Auto-submit form ketika semester berubah
        function filterBySemester(semester) {
            window.location.href = '?semester=' + semester;
        }
        
        // Enable/disable form berdasarkan ketersediaan peserta
        document.addEventListener('DOMContentLoaded', function() {
            const pesertaSelect = document.getElementById('peserta_id');
            const assignButton = document.querySelector('button[name="assign_ujian"]');
            const massAssignButton = document.querySelector('button[name="mass_assign"]');
            
            if (pesertaSelect && pesertaSelect.options.length <= 1) {
                pesertaSelect.disabled = true;
                if (assignButton) {
                    assignButton.disabled = true;
                }
                if (massAssignButton) {
                    massAssignButton.disabled = true;
                }
            }
        });
    </script>
</body>
</html>