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

<div class="row g-3">
    <!-- Counselor Info Header -->
    <div class="col-12">
        <div class="card border-0 shadow-sm mb-2" style="border-radius: 12px; overflow: hidden;">
            <div class="card-body p-4 bg-light bg-gradient">
                <div class="d-flex align-items-center gap-3">
                    <div class="bg-primary bg-opacity-10 p-3 rounded-circle text-primary" style="font-size: 2rem; line-height: 1;">
                        <i class="bi bi-person-hearts"></i>
                    </div>
                    <div>
                        <h5 class="fw-bold text-dark mb-1">Ruang Bimbingan Konseling (BK)</h5>
                        <small class="text-muted">Gunakan modul ini untuk menginput poin pelanggaran tata tertib siswa dan mengontrol pembinaan.</small>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Left Column: Classrooms List -->
    <div class="col-lg-5">
        <div class="card border-0 shadow-sm h-100" style="border-radius: 12px;">
            <div class="card-header bg-white border-0 pt-4 px-4 pb-2">
                <h6 class="fw-bold text-dark mb-0"><i class="bi bi-door-open text-primary me-2"></i>Daftar Kelas Binaan</h6>
                <small class="text-muted small">Pilih salah satu kelas di bawah untuk melihat siswa dan mencatat pelanggaran.</small>
            </div>
            <div class="card-body px-4 pb-4">
                <?php if (empty($classrooms)): ?>
                    <div class="text-center py-5 text-muted">
                        <i class="bi bi-door-closed fs-1 mb-2 opacity-50"></i>
                        <p class="mb-0 small">Belum ada data kelas di semester ini.</p>
                    </div>
                <?php else: ?>
                    <div class="list-group list-group-flush mt-2" style="max-height: 500px; overflow-y: auto;">
                        <?php foreach ($classrooms as $c): ?>
                            <a href="/bk/violations/classroom/<?= $c['id'] ?>" class="list-group-item list-group-item-action border-0 px-3 py-3 d-flex align-items-center justify-content-between mb-2 rounded bg-light" style="transition: all 0.2s;">
                                <div class="d-flex align-items-center gap-3">
                                    <div class="bg-primary text-white p-2 rounded d-flex align-items-center justify-content-center" style="width: 38px; height: 38px;">
                                        <i class="bi bi-people-fill"></i>
                                    </div>
                                    <div>
                                        <strong class="text-dark">Kelas <?= esc($c['name']) ?></strong>
                                        <div class="text-muted small" style="font-size: 0.75rem;">Klik untuk menginput poin</div>
                                    </div>
                                </div>
                                <i class="bi bi-chevron-right text-muted"></i>
                            </a>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Right Column: Top Violators -->
    <div class="col-lg-7">
        <div class="card border-0 shadow-sm h-100" style="border-radius: 12px;">
            <div class="card-header bg-white border-0 pt-4 px-4 pb-2">
                <h6 class="fw-bold text-dark mb-0"><i class="bi bi-graph-down-arrow text-danger me-2"></i>Daftar Siswa Pelanggar (Poin Tertinggi)</h6>
                <small class="text-muted small">Pemantauan seluruh kasus akumulasi poin aktif untuk prioritas pembinaan.</small>
            </div>
            <div class="card-body px-4 pb-4">
                <?php if (empty($topViolators)): ?>
                    <div class="text-center py-5 text-muted">
                        <div class="bg-success bg-opacity-10 p-3 rounded-circle text-success d-inline-flex mb-3" style="font-size: 2rem; line-height: 1;">
                            <i class="bi bi-emoji-smile-fill"></i>
                        </div>
                        <h6 class="fw-bold text-dark">Data Bersih!</h6>
                        <p class="mb-0 small">Belum ada siswa yang tercatat melakukan pelanggaran di semester ini.</p>
                    </div>
                <?php else: ?>
                    <div class="table-responsive" style="max-height: 500px; overflow-y: auto;">
                        <table class="table table-hover align-middle table-sm border-light mb-0">
                            <thead class="table-light" style="position: sticky; top: 0; z-index: 10; background-color: var(--bs-body-bg);">
                                <tr>
                                    <th class="text-center" style="width: 50px">No</th>
                                    <th>Nama Siswa</th>
                                    <th>Kelas</th>
                                    <th class="text-center" style="width: 100px">Total Poin</th>
                                    <th>Sanksi Terakhir</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $no = 1; foreach ($topViolators as $v): ?>
                                <tr>
                                    <td class="text-center text-muted small"><?= $no++ ?></td>
                                    <td>
                                        <strong class="text-dark d-block"><?= esc($v['full_name']) ?></strong>
                                        <small class="text-muted" style="font-size: 0.72rem;">NISN: <?= esc($v['nisn'] ?: '-') ?></small>
                                    </td>
                                    <td class="small text-secondary"><?= esc($v['classroom_name']) ?></td>
                                    <td class="text-center">
                                        <span class="badge bg-danger fs-6 px-2.5 py-1.5 rounded-pill" style="min-width: 50px;">
                                            <?= esc($v['total_points']) ?>
                                        </span>
                                    </td>
                                    <td>
                                        <?php
                                        $sanc = $v['sanction'];
                                        $sancClass = 'bg-secondary bg-opacity-10 text-secondary';
                                        if (str_contains($sanc, 'SP 3') || str_contains($sanc, 'Keluar')) $sancClass = 'bg-dark text-white';
                                        elseif (str_contains($sanc, 'SP 2') || str_contains($sanc, 'Skorsing')) $sancClass = 'bg-danger text-white';
                                        elseif (str_contains($sanc, 'SP 1')) $sancClass = 'bg-warning bg-opacity-10 text-warning';
                                        elseif ($sanc === 'Aman') $sancClass = 'bg-success bg-opacity-10 text-success';
                                        ?>
                                        <span class="badge <?= $sancClass ?> rounded px-2.5 py-1.5 small fw-semibold"><?= esc($sanc) ?></span>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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