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

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

    <div class="d-flex align-items-center gap-2 mb-4">
        <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">Atur Ruang Ujian</h4>
            <small class="text-muted">Kelola ruang ujian untuk event: <strong><?= esc($event['name']) ?></strong></small>
        </div>
    </div>

    <?php if (session()->getFlashdata('success')): ?>
        <div class="alert alert-success alert-dismissible fade show">
            <?= 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">
            <?= session()->getFlashdata('error') ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger">
            <ul class="mb-0">
                <?php foreach (session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc($err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <div class="row g-4">
        <!-- Kolom Kiri: Tabel Ruang -->
        <div class="col-lg-8">
            <?php if (!$event['is_locked']): ?>
                <div class="d-flex flex-wrap gap-2 mb-3">
                    <!-- Generate dari Kelas -->
                    <form action="/exam-event/<?= $event['id'] ?>/rooms/generate-from-classes" method="POST">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-primary btn-sm">
                            <i class="bi bi-cpu-fill me-1"></i> Generate dari Kelas Aktif
                        </button>
                    </form>

                    <!-- Salin dari Event Lain -->
                    <?php if (!empty($otherEvents)): ?>
                        <button type="button" class="btn btn-outline-primary btn-sm" data-bs-toggle="modal" data-bs-target="#copyEventModal">
                            <i class="bi bi-copy me-1"></i> Salin dari Event Lain
                        </button>
                    <?php endif; ?>
                </div>
            <?php endif; ?>

            <div class="card shadow-sm border-0">
                <div class="card-header fw-semibold bg-white border-bottom py-3">
                    <i class="bi bi-list-stars me-2 text-primary"></i>Daftar Ruang Ujian
                </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" width="10%">#</th>
                                    <th width="50%">Nama Ruang</th>
                                    <th width="20%">Kapasitas (Peserta)</th>
                                    <th class="text-center" width="20%">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (empty($rooms)): ?>
                                    <tr>
                                        <td colspan="4" class="text-center py-5 text-muted">
                                            <i class="bi bi-house-exclamation fs-3 d-block mb-2"></i>
                                            Belum ada ruang ujian yang ditambahkan
                                        </td>
                                    </tr>
                                <?php else: ?>
                                    <?php $no = 1; foreach ($rooms as $r): ?>
                                    <tr>
                                        <td class="ps-3"><?= $no++ ?></td>
                                        <td>
                                            <div class="fw-semibold text-dark"><?= esc($r['name']) ?></div>
                                        </td>
                                        <td>
                                            <span class="badge bg-info-subtle text-info fw-bold"><?= esc($r['capacity']) ?> Kursi</span>
                                        </td>
                                        <td class="text-center">
                                            <div class="d-flex gap-1 justify-content-center">
                                                <!-- Edit -->
                                                <button type="button" class="btn btn-sm btn-outline-warning" 
                                                        data-bs-toggle="modal" data-bs-target="#editModal_<?= $r['id'] ?>"
                                                        title="Edit Ruang" <?= $event['is_locked'] ? 'disabled' : '' ?>>
                                                    <i class="bi bi-pencil"></i>
                                                </button>
                                                <!-- Hapus -->
                                                <a href="/exam-event/rooms/delete/<?= $r['id'] ?>" 
                                                   class="btn btn-sm btn-outline-danger <?= $event['is_locked'] ? 'disabled' : '' ?>" 
                                                   title="Hapus Ruang">
                                                    <i class="bi bi-trash"></i>
                                                </a>
                                            </div>

                                            <!-- Modal Edit Ruang -->
                                            <div class="modal fade text-start" id="editModal_<?= $r['id'] ?>" tabindex="-1" aria-hidden="true">
                                                <div class="modal-dialog">
                                                    <form action="/exam-event/rooms/update/<?= $r['id'] ?>" method="POST">
                                                        <?= csrf_field() ?>
                                                        <div class="modal-content border-0 shadow">
                                                            <div class="modal-header bg-warning-subtle py-3">
                                                                <h5 class="modal-title fw-bold text-dark"><i class="bi bi-pencil-square me-2"></i>Edit Ruang</h5>
                                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                            </div>
                                                            <div class="modal-body p-4">
                                                                <div class="mb-3">
                                                                    <label class="form-label fw-bold">Nama Ruang <span class="text-danger">*</span></label>
                                                                    <input type="text" name="name" class="form-control" value="<?= esc($r['name']) ?>" required>
                                                                </div>
                                                                <div class="mb-3">
                                                                    <label class="form-label fw-bold">Kapasitas Ujian <span class="text-danger">*</span></label>
                                                                    <input type="number" name="capacity" class="form-control" value="<?= esc($r['capacity']) ?>" min="1" required>
                                                                    <div class="form-text">Maksimal jumlah siswa yang bisa ditampung di ruangan ini saat ujian.</div>
                                                                </div>
                                                            </div>
                                                            <div class="modal-footer border-top-0">
                                                                <button type="button" class="btn btn-outline-secondary" data-bs-close="modal">Batal</button>
                                                                <button type="submit" class="btn btn-warning"><i class="bi bi-save me-1"></i>Simpan Perubahan</button>
                                                            </div>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>

                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Kolom Kanan: Form Tambah (Hanya aktif jika tidak dikunci) -->
        <div class="col-lg-4">
            <div class="card shadow-sm border-0">
                <div class="card-header fw-semibold bg-white border-bottom py-3">
                    <i class="bi bi-plus-circle me-2 text-success"></i>Tambah Ruang Ujian
                </div>
                <div class="card-body p-4">
                    <?php if ($event['is_locked']): ?>
                        <div class="alert alert-warning mb-0">
                            <i class="bi bi-lock-fill me-1"></i>
                            Event ini telah <strong>dikunci</strong>. Anda tidak dapat menambah atau mengubah ruang ujian saat ini.
                        </div>
                    <?php else: ?>
                        <form action="/exam-event/<?= $event['id'] ?>/rooms/store" method="POST">
                            <?= csrf_field() ?>
                            <!-- Dropdown helper kelas -->
                            <div class="mb-3">
                                <label class="form-label fw-bold small text-muted">Salin dari Template Kelas (Opsional)</label>
                                <select class="form-select" id="classTemplateSelect">
                                    <option value="">-- Pilih Kelas --</option>
                                    <?php foreach ($classrooms as $c): ?>
                                        <?php $count = $studentCounts[$c['id']] ?? 0; ?>
                                        <option value="Ruang <?= esc($c['name']) ?>" data-capacity="<?= $count ?>">
                                            Kelas <?= esc($c['name']) ?> (<?= $count ?> siswa)
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                                <div class="form-text">Membantu memberikan gambaran nama & kapasitas kelas.</div>
                            </div>
                            
                            <hr class="my-3 opacity-25">

                            <div class="mb-3">
                                <label class="form-label fw-bold">Nama Ruang <span class="text-danger">*</span></label>
                                <input type="text" name="name" class="form-control" placeholder="misal: Ruang 01, Lab Komputer" value="<?= old('name') ?>" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label fw-bold">Kapasitas Ujian <span class="text-danger">*</span></label>
                                <input type="number" name="capacity" class="form-control" placeholder="misal: 20" value="<?= old('capacity', 20) ?>" min="1" required>
                                <div class="form-text">Kapasitas ruang disesuaikan dengan jumlah kursi/meja.</div>
                            </div>
                            <div class="d-grid mt-4">
                                <button type="submit" class="btn btn-success"><i class="bi bi-plus-lg me-1"></i>Tambah Ruang</button>
                            </div>
                        </form>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

</div>

<!-- Modal Salin dari Event Lain -->
<?php if (!empty($otherEvents) && !$event['is_locked']): ?>
<div class="modal fade" id="copyEventModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <form action="/exam-event/<?= $event['id'] ?>/rooms/copy-from-event" method="POST">
            <?= csrf_field() ?>
            <div class="modal-content border-0 shadow">
                <div class="modal-header bg-primary-subtle py-3">
                    <h5 class="modal-title fw-bold text-dark"><i class="bi bi-copy me-2"></i>Salin Ruang dari Event Lain</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label fw-bold">Pilih Event Asal <span class="text-danger">*</span></label>
                        <select name="from_event_id" class="form-select" required>
                            <option value="">-- Pilih Event --</option>
                            <?php foreach ($otherEvents as $oe): ?>
                                <option value="<?= $oe['id'] ?>"><?= esc($oe['name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                        <div class="form-text text-muted">
                            Semua ruang dari event asal akan disalin ke event ini (ruang dengan nama yang sama akan dilewati).
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-top-0">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary"><i class="bi bi-copy me-1"></i>Salin Ruang</button>
                </div>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

<script>
document.getElementById('classTemplateSelect')?.addEventListener('change', function() {
    const selectedOption = this.options[this.selectedIndex];
    const nameInput = document.querySelector('input[name="name"]');
    const capacityInput = document.querySelector('input[name="capacity"]');
    
    if (selectedOption.value) {
        nameInput.value = selectedOption.value;
        capacityInput.value = selectedOption.getAttribute('data-capacity') || 20;
    }
});
</script>

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