<?php // app/Views/guru/kelas/index.php ?>
<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

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

    <div class="mb-4">
        <h4 class="fw-bold mb-0">Kelas dan Jadwal Saya</h4>
        <small class="text-muted">
            Periode: <?= esc($activePeriod['year_name'] ?? '-') ?> — Semester <?= esc($activePeriod['semester'] ?? '-') ?>
        </small>
    </div>

    <?php if (empty($subjects) && empty($bkClasses)): ?>
        <div class="card shadow-sm">
            <div class="card-body text-center py-5 text-muted">
                <i class="bi bi-inbox fs-1 d-block mb-3 opacity-50"></i>
                <div class="fw-semibold">Belum ada kelas yang diampu</div>
                <small>Hubungi admin untuk setting kelas mengajar</small>
            </div>
        </div>
    <?php endif; ?>

    <!-- Kelas Mengajar (per mapel) -->
    <?php if (!empty($subjects)): ?>
    <div class="mb-4">
        <h6 class="fw-bold text-primary mb-3">
            <i class="bi bi-book me-2"></i>Kelas Mengajar
        </h6>
        <div class="row g-3">
            <?php foreach ($subjects as $subject): ?>
            <div class="col-md-6 col-lg-4">
                <div class="card shadow-sm h-100">
                    <div class="card-header bg-primary bg-opacity-10 border-bottom-0 py-2">
                        <div class="fw-bold text-primary">
                            <i class="bi bi-book me-2"></i><?= esc($subject['subject_name']) ?>
                        </div>
                    </div>
                    <div class="card-body p-0">
                        <ul class="list-group list-group-flush">
                            <?php foreach ($subject['classrooms'] as $cls): ?>
                            <li class="list-group-item d-flex justify-content-between align-items-center py-2">
                                <div>
                                    <span class="fw-semibold small"><?= esc($cls['grade_name']) ?></span>
                                    <span class="text-muted small"> — <?= esc($cls['classroom_name']) ?></span>
                                </div>
                                <span class="badge bg-secondary-subtle text-secondary">
                                    <?= $cls['student_count'] ?> siswa
                                </span>
                            </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Kelas BK -->
    <?php if (!empty($bkClasses)): ?>
    <div class="mb-4">
        <h6 class="fw-bold text-success mb-3">
            <i class="bi bi-person-heart me-2"></i>Kelas BK
        </h6>
        <div class="row g-3">
            <?php foreach ($bkClasses as $cls): ?>
            <div class="col-md-4 col-lg-3">
                <div class="card shadow-sm text-center p-3 h-100">
                    <div class="fs-2 text-success mb-2">
                        <i class="bi bi-door-open"></i>
                    </div>
                    <div class="fw-bold"><?= esc($cls['grade_name']) ?> — <?= esc($cls['classroom_name']) ?></div>
                    <div class="text-muted small mt-1"><?= $cls['student_count'] ?> siswa</div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- ================================================================ -->
    <!-- JADWAL PELAJARAN MINGGUAN GURU                                    -->
    <!-- ================================================================ -->
    <div class="card mb-4 border-0 shadow-sm mt-4">
        <div class="card-header bg-white py-3 d-flex align-items-center justify-content-between">
            <h6 class="fw-bold text-primary mb-0">
                <i class="bi bi-calendar3 me-2"></i>Jadwal Pelajaran Mengajar
            </h6>
            <?php if (isset($activeImport) && $activeImport): ?>
                <span class="badge bg-primary bg-opacity-10 text-primary fw-medium px-2.5 py-1">
                    <?= esc($activeImport['name']) ?>
                </span>
            <?php endif; ?>
        </div>
        <div class="card-body p-3">
            <?php if (!isset($activeImport) || !$activeImport): ?>
                <div class="text-center py-4 text-muted">
                    <i class="bi bi-calendar-x fs-3 d-block mb-2 text-secondary"></i>
                    <small>Belum ada jadwal semester aktif yang dipublikasikan oleh kurikulum.</small>
                </div>
            <?php elseif (!isset($ascTeacher) || !$ascTeacher): ?>
                <div class="alert alert-warning mb-0 border-0 bg-warning bg-opacity-10 text-warning d-flex align-items-start gap-2">
                    <i class="bi bi-exclamation-triangle-fill fs-5 flex-shrink-0 mt-0.5 text-warning"></i>
                    <div>
                        <div class="fw-semibold">Akun Belum Sinkron</div>
                        <small class="d-block mt-0.5 text-muted">Akun guru Anda belum dihubungkan dengan jadwal aSc oleh Admin. Silakan hubungi bagian kurikulum untuk melakukan pemetaan akun agar jadwal pelajaran Anda dapat ditampilkan.</small>
                    </div>
                </div>
            <?php elseif (empty($scheduleData)): ?>
                <div class="text-center py-4 text-muted">
                    <i class="bi bi-calendar-minus fs-3 d-block mb-2 text-secondary"></i>
                    <small>Tidak ada jam mengajar terdaftar untuk Anda pada jadwal semester ini.</small>
                </div>
            <?php else: 
                $teacherBg = $ascTeacher['color'] ?? '#3b71ca';
                // Hitung contrast color secara dinamis
                $hex = ltrim($teacherBg, '#');
                $luminance = 1;
                if (strlen($hex) === 6) {
                    $r = hexdec(substr($hex, 0, 2));
                    $g = hexdec(substr($hex, 2, 2));
                    $b = hexdec(substr($hex, 4, 2));
                    $luminance = (0.299 * $r + 0.587 * $g + 0.114 * $b) / 255;
                }
                $teacherFg = $luminance > 0.5 ? '#000000' : '#ffffff';
            ?>
                <!-- Matrix Timetable Grid -->
                <div class="table-responsive rounded border">
                    <table class="table table-bordered mb-0 schedule-matrix" style="table-layout: fixed;">
                        <thead class="table-light text-center align-middle">
                            <tr>
                                <th style="width: 90px; font-size: 0.8rem;" class="bg-light text-dark fw-bold">Jam</th>
                                <?php foreach ($scheduleDays as $day): ?>
                                    <th style="min-width: 100px; font-size: 0.8rem;" class="bg-light text-dark fw-bold">
                                        <?= strtoupper(substr($day['day_name'], 0, 3)) ?>
                                        <div class="small fw-normal text-muted" style="font-size: 0.65rem;"><?= $day['day_name'] ?></div>
                                    </th>
                                <?php endforeach; ?>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($schedulePeriods as $period): 
                                $pNum = $period['period_number'];
                            ?>
                                <tr>
                                     <!-- Kolom Jam -->
                                     <td class="text-center align-middle bg-light text-dark py-2">
                                         <div class="fw-bold text-dark fs-7">Jam <?= $pNum ?></div>
                                     </td>

                                    <!-- Kolom per Hari -->
                                    <?php foreach ($scheduleDays as $day): 
                                        $dayNum = $day['day_number'];
                                        $slot = $scheduleData[$dayNum][$pNum] ?? null;
                                        $isKbmSlot = !isset($customHours[$dayNum]) || $pNum <= $customHours[$dayNum];
                                    ?>
                                        <?php if (!$isKbmSlot) : ?>
                                            <td class="text-center align-middle p-1 bg-light text-muted" style="height: 55px; opacity: 0.5;">
                                                <span class="text-muted small" style="font-size: 0.65rem; opacity: 0.4;">Non-KBM</span>
                                            </td>
                                        <?php else : ?>
                                            <td class="text-center align-middle p-1 <?= $slot ? 'has-slot' : 'empty-slot' ?>"
                                                style="height: 55px; position: relative; <?= $slot ? 'background: ' . $teacherBg . ' !important; color: ' . $teacherFg . ' !important;' : 'background: #fdfdfd;' ?>"
                                                <?php if ($slot): ?>
                                                    title="<?= esc($slot['display_class_name'] ?? $slot['class_name']) ?> — <?= esc($slot['display_name'] ?? $slot['subject_name']) ?>"
                                                <?php endif; ?>>
                                                <?php if ($slot): ?>
                                                    <div class="fw-bold" style="font-size: 0.78rem; line-height: 1.2;">
                                                        <?= esc($slot['display_class_name'] ?? $slot['class_name']) ?>
                                                    </div>
                                                    <div style="font-size: 0.65rem; line-height: 1.2; opacity: 0.9; margin-top: 1px;">
                                                        <?= esc($slot['display_code'] ?? $slot['subject_code']) ?>
                                                    </div>
                                                <?php else: ?>
                                                    <span class="text-muted" style="font-size: 0.7rem; opacity: 0.2;">-</span>
                                                <?php endif; ?>
                                            </td>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- Summary Badges -->
                <?php
                $classSummary = [];
                $totalHours = 0;
                foreach ($scheduleData as $daySlots) {
                    foreach ($daySlots as $slot) {
                        $key = $slot['display_class_name'] ?? $slot['class_name'];
                        if (!isset($classSummary[$key])) {
                            $classSummary[$key] = [
                                'class' => $key,
                                'subjects' => [],
                                'hours' => 0
                            ];
                        }
                        $classSummary[$key]['hours']++;
                        $totalHours++;
                        $mapelName = $slot['display_name'] ?? $slot['subject_name'];
                        $classSummary[$key]['subjects'][$mapelName] = true;
                    }
                }
                ksort($classSummary);
                ?>
                <div class="d-flex align-items-center justify-content-between mt-3 flex-wrap gap-2">
                    <div class="small text-muted">
                        Total Mengajar: <strong class="text-primary fs-6"><?= $totalHours ?></strong> jam/minggu
                    </div>
                </div>
                <div class="row g-2 mt-1">
                    <?php foreach ($classSummary as $summary): ?>
                        <div class="col-6 col-sm-4 col-md-3 col-lg-2">
                            <div class="border rounded p-2 text-center h-100 bg-light d-flex flex-column justify-content-between">
                                <div class="fw-bold text-white rounded py-0.5 px-2 mb-1 small" style="background: <?= $teacherBg ?>; color: <?= $teacherFg ?> !important; font-size: 0.75rem;">
                                    <?= esc($summary['class']) ?>
                                </div>
                                <div class="text-muted text-truncate px-1" style="font-size: 0.68rem;" title="<?= implode(', ', array_keys($summary['subjects'])) ?>">
                                    <?= implode(', ', array_keys($summary['subjects'])) ?>
                                </div>
                                <div class="fw-bold text-primary small mt-1" style="font-size: 0.8rem;">
                                    <?= $summary['hours'] ?> jam
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <style>
    .schedule-matrix {
        border-collapse: collapse;
    }
    .schedule-matrix th {
        font-weight: 600;
    }
    .schedule-matrix td.has-slot {
        transition: all 0.2s ease-in-out;
    }
    .schedule-matrix td.has-slot:hover {
        transform: scale(1.03);
        box-shadow: 0 4px 10px rgba(0,0,0,0.15);
        z-index: 10;
        cursor: default;
    }
    .schedule-matrix td.empty-slot {
        background-color: #fafafa !important;
    }
    </style>

</div>

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