<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<style>
/* Prevent scroll jump when clicking roles selection radio buttons */
.table-responsive input.btn-check {
    display: none !important;
}
</style>

<div class="container-fluid py-4">

    <!-- Header -->
    <div class="d-flex align-items-center justify-content-between mb-4">
        <div class="d-flex align-items-center gap-2">
            <a href="/exam-event" class="btn btn-outline-secondary btn-sm">
                <i class="bi bi-arrow-left"></i>
            </a>
            <div>
                <h4 class="mb-0 fw-bold">Pengaturan & Jadwal Pengawas Ujian</h4>
                <small class="text-muted">Atur kepanitiaan, pengawas, dan acak jadwal penugasan ruang: <strong><?= esc($event['name']) ?></strong></small>
            </div>
        </div>
        <?php if (!empty($assignments)): ?>
            <div class="d-flex gap-2">
                <a href="/exam-event/<?= $event['id'] ?>/print/proctor-schedule" target="_blank" class="btn btn-outline-primary">
                    <i class="bi bi-printer-fill me-1"></i> Cetak Jadwal Pengawas
                </a>
                <a href="/exam-event/<?= $event['id'] ?>/print/proctor-attendance" target="_blank" class="btn btn-primary">
                    <i class="bi bi-journal-text me-1"></i> Cetak Hadir Pengawas
                </a>
            </div>
        <?php endif; ?>
    </div>

    <!-- Flash Alerts -->
    <?php if (session()->getFlashdata('success')): ?>
        <div class="alert alert-success alert-dismissible fade show">
            <i class="bi bi-check-circle me-1"></i>
            <?= session()->getFlashdata('success') ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('error')): ?>
        <div class="alert alert-danger alert-dismissible fade show">
            <i class="bi bi-exclamation-triangle me-1"></i>
            <?= session()->getFlashdata('error') ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <div class="row g-4">
        <!-- Kolom Kiri: Pengaturan Peran & Kapasitas (Lebih lebar) -->
        <div class="col-lg-8">
            <div class="card shadow-sm border-0 mb-4">
                <div class="card-header fw-bold bg-white border-bottom py-3">
                    <i class="bi bi-gear-fill me-2 text-primary"></i>Pengaturan Pengawas & Panitia
                </div>
                <div class="card-body p-4">
                    <form action="/exam-event/<?= $event['id'] ?>/proctor/save-settings" method="POST">
                        <?= csrf_field() ?>
                        
                        <div class="mb-4">
                            <label class="form-label fw-bold">Aturan Jumlah Pengawas per Ruang</label>
                            <div class="d-flex gap-4 mt-2">
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="proctors_per_room" id="proctors_1" value="1" <?= $event['proctors_per_room'] == 1 ? 'checked' : '' ?> <?= $event['is_locked'] ? 'disabled' : '' ?>>
                                    <label class="form-check-label" for="proctors_1">
                                        1 Pengawas per Ruang
                                    </label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="proctors_per_room" id="proctors_2" value="2" <?= $event['proctors_per_room'] == 2 ? 'checked' : '' ?> <?= $event['is_locked'] ? 'disabled' : '' ?>>
                                    <label class="form-check-label" for="proctors_2">
                                        2 Pengawas per Ruang
                                    </label>
                                </div>
                            </div>
                            <div class="form-text mt-1 text-muted">Tentukan jumlah pengawas yang bertugas di setiap ruang ujian pada satu sesi.</div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold d-block mb-3">Pilih Peran Guru</label>
                            <div class="table-responsive border rounded" style="max-height: 480px; overflow-y: auto;">
                                <table class="table table-hover align-middle mb-0" style="font-size: 0.9rem;">
                                    <thead class="table-light sticky-top" style="z-index: 1;">
                                        <tr>
                                            <th class="ps-3" width="10%">Inisial</th>
                                            <th width="35%">Nama Guru</th>
                                            <th width="20%" class="text-center">Maks Hari</th>
                                            <th width="35%" class="text-center">Peran Tugas</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if (empty($teachers)): ?>
                                            <tr>
                                                <td colspan="4" class="text-center py-4 text-muted">Belum ada data guru aktif</td>
                                            </tr>
                                        <?php else: ?>
                                            <?php foreach ($teachers as $t): 
                                                $currentRole = $eventRoles[$t['id']] ?? 'tidak_bertugas';
                                            ?>
                                            <tr>
                                                <td class="ps-3 fw-bold text-dark">
                                                    <span class="badge bg-light text-secondary border"><?= esc($t['short_code'] ?: '-') ?></span>
                                                </td>
                                                <td>
                                                    <div class="fw-semibold"><?= esc($t['name']) ?></div>
                                                    <small class="text-muted text-xs"><?= esc(teacher_id_label($t['status_kepegawaian'] ?? null)) ?>. <?= esc(teacher_id_display($t)) ?></small>
                                                </td>
                                                <td class="text-center">
                                                    <input type="number" class="form-control form-control-sm text-center mx-auto" name="max_days[<?= $t['id'] ?>]" value="<?= esc($eventTeachersData[$t['id']]['max_days'] ?? 0) ?>" min="0" style="max-width: 80px;" <?= $event['is_locked'] ? 'disabled' : '' ?> placeholder="0">
                                                    <small class="text-muted" style="font-size: 0.7rem; font-weight: normal;">0 = Bebas</small>
                                                </td>
                                                <td>
                                                    <div class="d-flex justify-content-center gap-1">
                                                        <!-- Radio Pengawas -->
                                                        <input type="radio" class="btn-check" name="roles[<?= $t['id'] ?>]" id="role_pws_<?= $t['id'] ?>" value="pengawas" <?= $currentRole == 'pengawas' ? 'checked' : '' ?> <?= $event['is_locked'] ? 'disabled' : '' ?>>
                                                        <label class="btn btn-xs btn-outline-success px-2 py-1" style="font-size: 0.75rem;" for="role_pws_<?= $t['id'] ?>">Pengawas</label>
 
                                                        <!-- Radio Panitia -->
                                                        <input type="radio" class="btn-check" name="roles[<?= $t['id'] ?>]" id="role_pnt_<?= $t['id'] ?>" value="panitia" <?= $currentRole == 'panitia' ? 'checked' : '' ?> <?= $event['is_locked'] ? 'disabled' : '' ?>>
                                                        <label class="btn btn-xs btn-outline-info px-2 py-1" style="font-size: 0.75rem;" for="role_pnt_<?= $t['id'] ?>">Panitia</label>
 
                                                        <!-- Radio Tidak Bertugas -->
                                                        <input type="radio" class="btn-check" name="roles[<?= $t['id'] ?>]" id="role_none_<?= $t['id'] ?>" value="tidak_bertugas" <?= $currentRole == 'tidak_bertugas' ? 'checked' : '' ?> <?= $event['is_locked'] ? 'disabled' : '' ?>>
                                                        <label class="btn btn-xs btn-outline-secondary px-2 py-1" style="font-size: 0.75rem;" for="role_none_<?= $t['id'] ?>">None</label>
                                                    </div>
                                                </td>
                                            </tr>
                                            <?php endforeach; ?>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <?php if (!$event['is_locked']): ?>
                            <div class="d-grid mt-4">
                                <button type="submit" class="btn btn-success">
                                    <i class="bi bi-save me-1"></i> Simpan Pengaturan Peran
                                </button>
                            </div>
                        <?php endif; ?>
                    </form>
                </div>
            </div>
        </div>

        <!-- Kolom Kanan: Aksi & Informasi (Lebih ramping) -->
        <div class="col-lg-4">
            <!-- Card Visualisasi & Hasil Matrix -->
            <div class="card shadow-sm border-0 mb-4">
                <div class="card-header fw-bold bg-white border-bottom py-3">
                    <i class="bi bi-table me-2 text-success"></i>Matrix & Hasil Jadwal
                </div>
                <div class="card-body p-4 text-center">
                    <?php if (empty($assignments)): ?>
                        <div class="py-3 text-muted">
                            <i class="bi bi-calendar-x fs-1 d-block mb-3 text-secondary"></i>
                            <h6 class="fw-bold">Jadwal Pengawas Belum Ada</h6>
                            <p class="text-xs px-2">Data pengawas belum diacak. Silakan lakukan pengacakan jadwal di bawah.</p>
                        </div>
                        <button class="btn btn-outline-success w-100 disabled" disabled>
                            <i class="bi bi-eye-fill me-1"></i> Lihat Matrix & Beban Kerja
                        </button>
                    <?php else: ?>
                        <div class="py-3 text-success">
                            <i class="bi bi-calendar-check-fill fs-1 d-block mb-3 text-success animate-bounce"></i>
                            <h6 class="fw-bold text-dark">Jadwal Pengawas Aktif</h6>
                            <p class="text-xs text-muted px-2">Jadwal pengawas dan pembagian beban kerja telah siap.</p>
                        </div>
                        <div class="d-flex flex-column gap-2 mt-2">
                            <a href="/exam-event/<?= $event['id'] ?>/proctor/matrix" class="btn btn-success w-100 fw-semibold">
                                <i class="bi bi-eye-fill me-1"></i> Lihat Matrix & Beban Kerja
                            </a>
                            <a href="/exam-event/<?= $event['id'] ?>/print/proctor-schedule" target="_blank" class="btn btn-outline-primary w-100 fw-semibold">
                                <i class="bi bi-printer-fill me-1"></i> Cetak Jadwal Pengawas
                            </a>
                        </div>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Card Aksi Acak -->
            <div class="card shadow-sm border-0">
                <div class="card-header fw-bold bg-white border-bottom py-3">
                    <i class="bi bi-cpu-fill me-2 text-warning"></i>Pembuat Jadwal Otomatis
                </div>
                <div class="card-body p-4">
                    <p class="text-muted small">Fitur ini akan secara otomatis mengacak jadwal pengawas dengan ketentuan: <strong>Guru bertugas penuh selama 1 hari ujian</strong> di ruang yang sama (tidak berpindah per mata pelajaran).</p>
                    
                    <?php
                    $proctorsCount = 0;
                    foreach ($eventRoles as $rid => $rrole) {
                        if ($rrole === 'pengawas') $proctorsCount++;
                    }
                    ?>
                    <div class="alert bg-light border-0 d-flex align-items-center justify-content-between p-3 mb-4">
                        <div class="d-flex align-items-center gap-2">
                            <i class="bi bi-people-fill text-success fs-4"></i>
                            <div>
                                <small class="text-muted d-block" style="font-size: 0.75rem;">Total Pengawas Aktif</small>
                                <span class="fw-bold text-dark fs-6"><?= $proctorsCount ?> Guru</span>
                            </div>
                        </div>
                        <div class="text-end">
                            <span class="badge bg-secondary-subtle text-secondary"><?= count($rooms) ?> Ruang</span>
                        </div>
                    </div>

                    <?php if ($event['is_locked']): ?>
                        <div class="alert alert-warning mb-0 text-sm">
                            <i class="bi bi-lock-fill me-1"></i>
                            Event telah dikunci. Buka kunci jika Anda ingin mengatur ulang peran atau mengacak ulang jadwal.
                        </div>
                    <?php else: ?>
                        <div class="d-flex flex-column gap-2">
                            <form action="/exam-event/<?= $event['id'] ?>/proctor/generate" method="POST">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-warning w-100 text-dark fw-semibold" onclick="return confirm('Apakah Anda yakin ingin mengacak ulang penugasan pengawas? Semua penugasan pengawas yang ada saat ini akan dihapus!')">
                                    <i class="bi bi-shuffle me-1"></i> Acak Penugasan Pengawas
                                </button>
                            </form>
                            
                            <?php if (!empty($assignments)): ?>
                                <a href="/exam-event/<?= $event['id'] ?>/proctor/clear" class="btn btn-outline-danger w-100" onclick="return confirm('Apakah Anda yakin ingin mengosongkan seluruh penugasan pengawas?')">
                                    <i class="bi bi-eraser me-1"></i> Kosongkan Penugasan
                                </a>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
