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

<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/<?= $event['id'] ?>/proctor" class="btn btn-outline-secondary btn-sm" title="Kembali ke Pengaturan">
                <i class="bi bi-arrow-left"></i>
            </a>
            <div>
                <h4 class="mb-0 fw-bold">Matrix & Beban Kerja Pengawas</h4>
                <small class="text-muted">Hasil acak penugasan dan beban kerja pengawas: <strong><?= esc($event['name']) ?></strong></small>
            </div>
        </div>
        <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>
    </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; ?>

    <div class="row g-4">
        <!-- Matrix Penugasan Pengawas -->
        <div class="col-12">
            <div class="card shadow-sm border-0">
                <div class="card-header fw-bold bg-white border-bottom py-3 d-flex align-items-center justify-content-between">
                    <span>
                        <i class="bi bi-table me-2 text-success"></i>Matrix Jadwal & Pengawas Ruang
                    </span>
                    <span class="badge bg-light text-secondary border">Total: <?= count($rooms) ?> Ruang</span>
                </div>
                <div class="card-body p-0">
                    <?php if (empty($assignments)): ?>
                        <div class="text-center py-5 my-5 text-muted">
                            <i class="bi bi-table fs-1 d-block mb-3 text-secondary"></i>
                            <h5 class="fw-bold">Jadwal Pengawas Kosong</h5>
                            <p class="text-sm px-5">Belum ada penugasan pengawas yang dibuat untuk event ini.</p>
                        </div>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover align-middle mb-0" style="font-size: 0.85rem;">
                                <thead class="table-light">
                                    <tr>
                                        <th class="ps-3 text-center" width="4%">No</th>
                                        <th width="14%">Hari/Tanggal</th>
                                        <th width="16%">Mapel</th>
                                        <th width="10%">Waktu</th>
                                        <?php foreach ($rooms as $room): 
                                            $shortRoomName = trim(preg_replace('/^ruang\s*/i', 'R', $room['name']));
                                        ?>
                                            <th class="text-center" style="min-width: 70px;"><?= esc($shortRoomName) ?></th>
                                        <?php endforeach; ?>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                    $no = 1; 
                                    foreach ($schedules as $sch): 
                                        $subjects = $sch['subjects'] ?: [];
                                        // Filter valid subjects first
                                        $validSubjects = [];
                                        foreach ($subjects as $sub) {
                                            $name = is_array($sub) ? ($sub['name'] ?? '') : $sub;
                                            if (!empty($name)) {
                                                $validSubjects[] = $sub;
                                            }
                                        }
                                        $rowspan = count($validSubjects);
                                        if ($rowspan === 0) continue;

                                        foreach ($validSubjects as $index => $sub):
                                            $subjectName = is_array($sub) ? ($sub['name'] ?? '') : $sub;
                                            $subjectTime = is_array($sub) ? ($sub['time'] ?? '') : '';
                                    ?>
                                    <tr>
                                        <?php if ($index === 0): ?>
                                            <td class="ps-3 text-center fw-semibold text-muted" rowspan="<?= $rowspan ?>"><?= $no++ ?></td>
                                            <td rowspan="<?= $rowspan ?>" class="bg-light-subtle">
                                                <div class="fw-bold text-dark"><?= esc($sch['exam_day']) ?></div>
                                                <div class="text-xs text-muted" style="font-size: 0.75rem;"><?= date('d F Y', strtotime($sch['exam_date'])) ?></div>
                                            </td>
                                        <?php endif; ?>
                                        <td class="fw-semibold text-primary" style="font-size: 0.8rem;" title="<?= esc($subjectName) ?>">
                                            <?= esc($subjectName) ?>
                                        </td>
                                        <td>
                                            <?php if (!empty($subjectTime)): ?>
                                                <span class="badge bg-secondary-subtle text-secondary font-monospace" style="font-size: 0.75rem;"><?= esc($subjectTime) ?></span>
                                            <?php else: ?>
                                                <span class="text-muted">-</span>
                                            <?php endif; ?>
                                        </td>
                                        <?php foreach ($rooms as $room): 
                                            $assign = $assignments[$sch['id']][$subjectName][$room['id']] ?? null;
                                            $p1_code = '';
                                            $p1_name = '';
                                            $p2_code = '';
                                            $p2_name = '';
                                            if ($assign) {
                                                if (!empty($assign['teacher_id_1']) && isset($teacherLookup[$assign['teacher_id_1']])) {
                                                    $t1 = $teacherLookup[$assign['teacher_id_1']];
                                                    $p1_code = $t1['short_code'] ?: $t1['name'];
                                                    $p1_name = $t1['name'];
                                                }
                                                if (!empty($assign['teacher_id_2']) && isset($teacherLookup[$assign['teacher_id_2']])) {
                                                    $t2 = $teacherLookup[$assign['teacher_id_2']];
                                                    $p2_code = $t2['short_code'] ?: $t2['name'];
                                                    $p2_name = $t2['name'];
                                                }
                                            }
                                        ?>
                                            <td class="text-center p-2">
                                                <?php if (!empty($p1_code)): ?>
                                                    <div class="d-flex flex-column align-items-center gap-1">
                                                        <span class="badge bg-success-subtle text-success border border-success-subtle px-2 py-1 text-xs" style="font-weight: 600; min-width: 45px;" title="<?= esc($p1_name) ?>"><?= esc($p1_code) ?></span>
                                                        <?php if (!empty($p2_code)): ?>
                                                            <span class="badge bg-info-subtle text-info border border-info-subtle px-2 py-1 text-xs" style="font-weight: 600; min-width: 45px;" title="<?= esc($p2_name) ?>"><?= esc($p2_code) ?></span>
                                                        <?php endif; ?>
                                                    </div>
                                                <?php else: ?>
                                                    <span class="text-muted font-monospace text-xs">-</span>
                                                <?php endif; ?>
                                            </td>
                                        <?php endforeach; ?>
                                    </tr>
                                        <?php endforeach; ?>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Beban Kerja Pengawas -->
        <div class="col-12">
            <div class="card shadow-sm border-0 mb-4">
                <div class="card-header fw-bold bg-white border-bottom py-3 d-flex align-items-center justify-content-between">
                    <span>
                        <i class="bi bi-person-badge-fill me-2 text-primary"></i>Analisis Beban Kerja Guru
                    </span>
                    <span class="badge bg-primary-subtle text-primary border">Aktif: <?= count($workload) ?> Guru</span>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th class="ps-3 text-center" width="5%">No</th>
                                    <th>Guru / Pegawai</th>
                                    <th class="text-center" width="15%">Inisial (Short Code)</th>
                                    <th class="text-center" width="15%">Peran Tugas</th>
                                    <th class="text-center" width="20%">Beban Mengawas (Hari)</th>
                                    <th class="text-center" width="20%">Beban Mengawas (Sesi / Mapel)</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (empty($workload)): ?>
                                    <tr>
                                        <td colspan="6" class="text-center py-4 text-muted">Belum ada guru yang dikonfigurasi aktif</td>
                                    </tr>
                                <?php else: ?>
                                    <?php 
                                    $wNo = 1;
                                    foreach ($workload as $tid => $wl): 
                                        $t = $wl['teacher'];
                                    ?>
                                    <tr>
                                        <td class="ps-3 text-center"><?= $wNo++ ?></td>
                                        <td>
                                            <div class="fw-semibold text-dark"><?= 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">
                                            <span class="badge bg-light text-secondary border fw-bold"><?= esc($t['short_code'] ?: '-') ?></span>
                                        </td>
                                        <td class="text-center">
                                            <?php if ($wl['role'] === 'pengawas'): ?>
                                                <span class="badge bg-success-subtle text-success border border-success-subtle px-3 py-1 text-sm">Pengawas</span>
                                            <?php elseif ($wl['role'] === 'panitia'): ?>
                                                <span class="badge bg-info-subtle text-info border border-info-subtle px-3 py-1 text-sm">Panitia</span>
                                            <?php else: ?>
                                                <span class="badge bg-light text-secondary border px-3 py-1 text-sm">None</span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="text-center">
                                            <?php if ($wl['role'] === 'pengawas'): ?>
                                                <span class="badge bg-primary fs-6 px-3 py-1">
                                                    <?= $wl['days_count'] ?> Hari
                                                    <?php if (!empty($wl['max_days']) && $wl['max_days'] > 0): ?>
                                                        / Maks <?= $wl['max_days'] ?>
                                                    <?php endif; ?>
                                                </span>
                                            <?php else: ?>
                                                <span class="text-muted font-monospace">-</span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="text-center">
                                            <?php if ($wl['role'] === 'pengawas'): ?>
                                                <span class="badge bg-secondary fs-6 px-3 py-1"><?= $wl['sessions_count'] ?> Sesi</span>
                                            <?php else: ?>
                                                <span class="text-muted font-monospace">-</span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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