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

<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="fw-bold mb-0"><i class="bi bi-clipboard-data me-2 text-primary"></i>Rekap Jurnal</h5>
        <p class="text-muted small mb-0">Semester <?= esc($period['semester'] ?? '—') ?> / <?= esc($period['academic_year_name'] ?? '—') ?></p>
    </div>
    <a href="/admin/kbm/jurnal-kelas" class="btn btn-primary btn-sm rounded-2">
        <i class="bi bi-printer me-1"></i> Cetak Jurnal Kelas
    </a>
</div>

<div class="card shadow-sm border-0 rounded-3">
    <div class="card-body p-0">
        <?php if (empty($progress)): ?>
        <div class="text-center py-5 text-muted">
            <i class="bi bi-journal-text" style="font-size:2rem;"></i>
            <p class="mt-2 mb-0">Belum ada data jurnal untuk semester ini.</p>
        </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th>Guru</th>
                        <th>Kelas/Mapel</th>
                        <th style="width:200px;">Progress</th>
                        <th style="width:80px;">%</th>
                        <th>Terakhir Isi</th>
                        <th class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($progress as $p):
                        $total    = (int)$p['total_pertemuan'];
                        $kelasMapel = (int)($p['total_kelas_mapel'] ?? 1);
                        $target   = $kelasMapel * (int)$targetJp;
                        $percent  = $target > 0 ? round(($total / $target) * 100, 0) : 0;
                        $barClass = $percent >= 80 ? 'bg-success' : ($percent >= 50 ? 'bg-warning text-dark' : 'bg-danger');
                    ?>
                    <tr>
                        <td><strong><?= esc($p['teacher_name']) ?></strong></td>
                        <td><span class="badge bg-info bg-opacity-10 text-info"><?= $kelasMapel ?> kelas/mapel</span></td>
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <span class="fw-semibold"><?= $total ?></span>
                                <span class="text-muted">/ <?= $target ?> pertemuan</span>
                                <div class="progress flex-grow-1" style="height:7px;min-width:60px;">
                                    <div class="progress-bar <?= $barClass ?>" role="progressbar"
                                         style="width:<?= min(100, $percent) ?>%;"></div>
                                </div>
                            </div>
                        </td>
                        <td>
                            <span class="badge <?= $barClass ?>"><?= $percent ?>%</span>
                        </td>
                        <td style="font-size:0.85rem;">
                            <?php if ($p['terakhir_isi']): ?>
                                <?= date('d/m/Y', strtotime($p['terakhir_isi'])) ?>
                                <?= $percent >= 80 ? '<i class="bi bi-check-circle-fill text-success ms-1"></i>' : '<i class="bi bi-exclamation-triangle-fill text-warning ms-1"></i>' ?>
                            <?php else: ?>
                                <span class="text-muted">—</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-center">
                            <a href="/admin/kbm/detail/<?= $p['teacher_id'] ?>" class="btn btn-sm btn-outline-primary" title="Lihat Jurnal">
                                <i class="bi bi-eye"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>

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