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

<div class="row justify-content-center">
    <div class="col-12 col-xl-10">
        
        <!-- Header -->
        <div class="card border-0 shadow-sm mb-4" style="border-radius: 12px;">
            <div class="card-body p-4 bg-light bg-gradient" style="border-radius: 12px;">
                <h5 class="fw-bold mb-1 text-dark"><i class="bi bi-journal-bookmark-fill text-primary me-2"></i>Rekapitulasi Nilai Rapor Mandiri</h5>
                <small class="text-muted">Menampilkan rekam jejak perkembangan seluruh nilai rapor Anda selama menempuh pendidikan di madrasah/sekolah.</small>
            </div>
        </div>

        <!-- Grades Container -->
        <div class="card border-0 shadow-sm" style="border-radius: 12px;">
            <div class="card-body p-3 p-md-4">
                <?php if (empty($subjects) || empty($schoolGrades)): ?>
                    <div class="text-center py-5 text-muted">
                        <i class="bi bi-journal-x fs-1 mb-2"></i>
                        <p class="mb-0">Nilai rapor belum tersedia atau belum di-import oleh wali kelas/admin.</p>
                    </div>
                <?php else: ?>
                    
                    <!-- 1. DESKTOP VIEW (Shown on md screens and above) -->
                    <div class="d-none d-md-block">
                        <div class="table-responsive">
                            <table class="table table-bordered align-middle text-center table-hover mb-0" style="min-width: 800px;">
                                <thead class="table-light">
                                    <tr>
                                        <th rowspan="2" class="align-middle" width="5%">No</th>
                                        <th rowspan="2" class="align-middle text-start">Mata Pelajaran</th>
                                        <?php foreach ($schoolGrades as $g): ?>
                                            <th colspan="2">Kelas <?= esc($g['name']) ?></th>
                                        <?php endforeach; ?>
                                    </tr>
                                    <tr>
                                        <?php foreach ($schoolGrades as $g): ?>
                                            <th width="10%">Sem 1</th>
                                            <th width="10%">Sem 2</th>
                                        <?php endforeach; ?>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $no = 1; foreach ($subjects as $subjName): ?>
                                    <tr>
                                        <td class="text-center text-muted small"><?= $no++ ?></td>
                                        <td class="text-start fw-semibold text-dark"><?= esc($subjName) ?></td>
                                        
                                        <?php foreach ($schoolGrades as $g): ?>
                                            <?php foreach ([1, 2] as $sem): ?>
                                                <?php $score = $matrix[$subjName][$g['id']][$sem] ?? null; ?>
                                                <td>
                                                    <?php if ($score !== null): ?>
                                                        <?php 
                                                        $scoreClass = 'bg-success bg-opacity-10 text-success';
                                                        if ($score < 70) $scoreClass = 'bg-danger bg-opacity-10 text-danger';
                                                        elseif ($score < 80) $scoreClass = 'bg-warning bg-opacity-10 text-warning';
                                                        ?>
                                                        <span class="badge <?= $scoreClass ?> px-2 py-1 fs-6" style="min-width: 45px;">
                                                            <?= number_format($score, 0) ?>
                                                        </span>
                                                    <?php else: ?>
                                                        <span class="text-muted small">—</span>
                                                    <?php endif; ?>
                                                </td>
                                            <?php endforeach; ?>
                                        <?php endforeach; ?>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <!-- 2. MOBILE VIEW (Shown on screens smaller than md) -->
                    <div class="d-md-none">
                        <?php $no = 1; foreach ($subjects as $subjName): ?>
                        <div class="card mb-3 border-0 bg-light" style="border-radius: 10px;">
                            <div class="card-body p-3">
                                <div class="d-flex align-items-center gap-2 mb-3 pb-2 border-bottom">
                                    <span class="badge bg-primary rounded-circle d-flex align-items-center justify-content-center" style="width: 24px; height: 24px; font-size: 0.75rem;"><?= $no++ ?></span>
                                    <h6 class="fw-bold text-dark mb-0"><?= esc($subjName) ?></h6>
                                </div>
                                <div class="row g-2">
                                    <?php foreach ($schoolGrades as $g): ?>
                                    <div class="col-12 mb-2">
                                        <small class="text-primary fw-semibold d-block mb-1" style="font-size: 0.75rem;">Kelas <?= esc($g['name']) ?></small>
                                        <div class="d-flex justify-content-between align-items-center bg-white p-2 rounded small shadow-sm mb-1">
                                            <span class="text-secondary">Semester 1</span>
                                            <?php $score1 = $matrix[$subjName][$g['id']][1] ?? null; ?>
                                            <?php if ($score1 !== null): ?>
                                                <strong class="<?= $score1 < 70 ? 'text-danger' : 'text-success' ?>"><?= number_format($score1, 0) ?></strong>
                                            <?php else: ?>
                                                <span class="text-muted">—</span>
                                            <?php endif; ?>
                                        </div>
                                        <div class="d-flex justify-content-between align-items-center bg-white p-2 rounded small shadow-sm">
                                            <span class="text-secondary">Semester 2</span>
                                            <?php $score2 = $matrix[$subjName][$g['id']][2] ?? null; ?>
                                            <?php if ($score2 !== null): ?>
                                                <strong class="<?= $score2 < 70 ? 'text-danger' : 'text-success' ?>"><?= number_format($score2, 0) ?></strong>
                                            <?php else: ?>
                                                <span class="text-muted">—</span>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>

                <?php endif; ?>
            </div>
        </div>

    </div>
</div>

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