<?= $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-person me-2 text-primary"></i>Jurnal <?= esc($teacher['name'] ?? 'Guru') ?></h5>
        <p class="text-muted small mb-0">Semester <?= esc($period['semester'] ?? '—') ?> / <?= esc($period['academic_year_name'] ?? '—') ?></p>
    </div>
    <a href="/admin/kbm/rekap" class="btn btn-outline-secondary btn-sm">
        <i class="bi bi-arrow-left me-1"></i>Kembali
    </a>
</div>

<?php if (empty($groups)): ?>
<div class="card shadow-sm border-0 rounded-3">
    <div class="card-body text-center py-5 text-muted">
        <i class="bi bi-journal-text" style="font-size:2rem;"></i>
        <p class="mt-2 mb-0">Guru ini belum memiliki jurnal.</p>
    </div>
</div>
<?php else: ?>

<?php
$totalAll = array_sum(array_map(function($g) { return count($g['journals']); }, $groups));
$totalTarget = count($groups) * (int)$target;
?>
<div class="card shadow-sm border-0 rounded-3 mb-3">
    <div class="card-body p-3">
        <strong>Total:</strong> <?= $totalAll ?> / <?= $totalTarget ?> pertemuan
        (<?= $totalTarget > 0 ? round(($totalAll / $totalTarget) * 100, 0) : 0 ?>%)
        &nbsp;|&nbsp;
        <strong>Kelas/Mapel:</strong> <?= count($groups) ?>
    </div>
</div>

<?php foreach ($groups as $key => $group):
    $classroomName = $group['classroom_name'];
    $subjectName = $group['subject_name'];
    $groupJournals = $group['journals'];
    $totalHadir = 0; $totalSiswa = 0;
?>
<div class="card shadow-sm border-0 rounded-3 mb-3">
    <div class="card-header bg-white py-2 border-0">
        <h6 class="fw-bold mb-0"><?= esc($classroomName) ?> — <?= esc($subjectName) ?></h6>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0 small">
                <thead class="table-light">
                    <tr>
                        <th style="width:28px;">No</th>
                        <th style="width:70px;">Tanggal</th>
                        <th style="width:36px;">Jam</th>
                        <th>Materi</th>
                        <th style="width:60px;">Hadir</th>
                        <th style="width:180px;">S/I/A</th>
                        <th style="width:120px;">Catatan</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($groupJournals as $j):
                        $absList = $absMap[$j['id']] ?? [];
                        $totalHadir += (int)$j['total_present'];
                        $totalSiswa += (int)$j['total_students'];
                    ?>
                    <tr>
                        <td class="text-center"><?= $j['meeting_number'] ?></td>
                        <td><?= date('d/m/Y', strtotime($j['meeting_date'])) ?></td>
                        <td><?= $j['start_period'] ?>-<?= $j['end_period'] ?></td>
                        <td><?= esc($j['topic']) ?: '<span class="text-muted">—</span>' ?></td>
                        <td class="text-center"><?= $j['total_present'] ?></td>
                        <td>
                            <?php
                            $sickN = []; $permitN = []; $absentN = [];
                            foreach ($absList as $a) {
                                if ($a['status'] === 'Sakit') $sickN[] = $a['full_name'];
                                elseif ($a['status'] === 'Izin') $permitN[] = $a['full_name'];
                                else $absentN[] = $a['full_name'];
                            }
                            $parts = [];
                            if (!empty($sickN)) $parts[] = '<span class="text-warning">S:' . implode(', ', array_map(function($n) { return explode(' ', $n)[0]; }, $sickN)) . '</span>';
                            if (!empty($permitN)) $parts[] = '<span class="text-info">I:' . implode(', ', array_map(function($n) { return explode(' ', $n)[0]; }, $permitN)) . '</span>';
                            if (!empty($absentN)) $parts[] = '<span class="text-danger">A:' . implode(', ', array_map(function($n) { return explode(' ', $n)[0]; }, $absentN)) . '</span>';
                            echo $parts ? implode(' ', $parts) : '<span class="text-muted">—</span>';
                            ?>
                        </td>
                        <td><?= esc($j['notes']) ?: '<span class="text-muted">—</span>' ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <div class="p-2 border-top text-muted small">
            Hadir: <?= $totalHadir ?> / <?= $totalSiswa ?>
            (<?= $totalSiswa > 0 ? round(($totalHadir / $totalSiswa) * 100, 1) : 0 ?>%)
        </div>
    </div>
</div>
<?php endforeach; ?>
<?php endif; ?>

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