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

<?php if (!$teacher || !$activePeriod): ?>
<div class="alert alert-warning">
    <i class="bi bi-exclamation-triangle me-2"></i>
    <?= !$teacher ? 'Data guru tidak ditemukan.' : 'Belum ada periode akademik aktif.' ?>
</div>
<?php return; endif; ?>

<?php if (isset($openCounselingCount) && $openCounselingCount > 0): ?>
<div class="alert alert-warning border-0 shadow-sm d-flex align-items-center justify-content-between p-3 mb-4" style="border-radius: 12px; background-color: #fff9db; border-left: 4px solid #fcc419 !important;">
    <div class="d-flex align-items-center gap-3">
        <div class="bg-warning bg-opacity-20 text-warning p-2.5 rounded-circle d-flex align-items-center justify-content-center" style="font-size: 1.5rem; line-height: 1;">
            <i class="bi bi-chat-left-dots-fill text-warning"></i>
        </div>
        <div>
            <h6 class="fw-bold text-dark mb-1">Ada E-Konseling / Laporan Masuk!</h6>
            <p class="mb-0 text-muted small">Anda memiliki <strong><?= $openCounselingCount ?></strong> sesi pengaduan atau konseling aktif yang memerlukan tanggapan Anda.</p>
        </div>
    </div>
    <a href="/guru/counseling" class="btn btn-warning btn-sm fw-bold text-dark px-3" style="border-radius: 6px;">
        Tanggapi Sekarang <i class="bi bi-arrow-right ms-1"></i>
    </a>
</div>
<?php endif; ?>

<!-- Sapaan -->
<div class="d-flex align-items-center gap-3 mb-4">
    <div class="bg-primary bg-opacity-10 rounded-circle d-flex align-items-center justify-content-center"
         style="width:52px;height:52px">
        <i class="bi bi-person-circle fs-2 text-primary"></i>
    </div>
    <div>
        <h5 class="mb-0 fw-semibold">Halo, <?= esc($teacher['name']) ?> 👋</h5>
        <small class="text-muted">
            Periode: <strong><?= esc($activePeriod['year_name'] ?? '') ?></strong>
            &mdash; Semester <strong><?= esc($activePeriod['semester'] ?? '') ?></strong>
            <?php if ($isTeaching): ?>
                &mdash; <span class="badge bg-primary">Guru Mapel</span>
            <?php endif; ?>
            <?php if ($isHomeroom): ?>
                &mdash; <span class="badge bg-success">Wali Kelas <?= esc($walasStats['classroom_name'] ?? '') ?></span>
            <?php endif; ?>
            <?php if ($isBk): ?>
                &mdash; <span class="badge bg-info text-dark">Guru BK</span>
            <?php endif; ?>
            <?php if ($isTahfidz): ?>
                &mdash; <span class="badge text-white" style="background-color: #6f42c1;">Guru Tahfidz</span>
            <?php endif; ?>
        </small>
    </div>
</div>

<?php if (!$isTeaching && !$isBk && !$isHomeroom && !$isTahfidz): ?>
<div class="alert alert-info">
    <i class="bi bi-info-circle me-2"></i>
    Belum ada penugasan di periode ini. Hubungi admin untuk pengaturan mengajar.
</div>
<?php return; endif; ?>

<!-- ================================================================ -->
<!-- JADWAL MENGAJAR HARI INI                                           -->
<!-- ================================================================ -->
<?php if ($isTeaching): ?>
<div class="card mb-4 border-0 shadow-sm">
    <div class="card-header bg-white py-3">
        <h6 class="mb-0 fw-bold text-primary"><i class="bi bi-calendar3-event me-2"></i>Jadwal Mengajar Hari Ini</h6>
    </div>
    <div class="card-body p-0">
        <?php if (empty($todaySchedule)): ?>
            <div class="text-center text-muted py-5">
                <i class="bi bi-emoji-smile fs-1 text-success d-block mb-2"></i>
                <p class="mb-1 fw-semibold">Tidak ada jadwal mengajar hari ini.</p>
                <small>Selamat beristirahat atau menyelesaikan administrasi kelas! 😊</small>
            </div>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0 table-sm">
                    <thead class="table-light">
                        <tr>
                            <th style="width: 150px;">Jam Ke</th>
                            <th>Kelas</th>
                            <th>Mata Pelajaran</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($todaySchedule as $sch): ?>
                        <tr>
                            <td class="fw-bold">Jam <?= esc($sch['period_number']) ?></td>
                            <td class="fw-semibold text-success"><?= esc($sch['display_class_name']) ?></td>
                            <td><?= esc($sch['display_subject_name']) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>
<?php endif; ?>

<!-- ================================================================ -->
<!-- BAGIAN WALI KELAS (HOMEROOM)                                       -->
<!-- ================================================================ -->
<?php if ($isHomeroom): ?>
<div class="card mb-4 border-0 shadow-sm">
    <div class="card-header bg-success bg-opacity-10 text-success py-3 d-flex align-items-center justify-content-between">
        <h6 class="mb-0 fw-bold"><i class="bi bi-mortarboard-fill me-2"></i>Panel Wali Kelas: <?= esc($walasStats['classroom_name']) ?></h6>
        <a href="/guru/siswa-kelas" class="btn btn-xs btn-outline-success btn-sm">Lihat Siswa Kelas</a>
    </div>
    <div class="card-body">
        <div class="row g-3 mb-4">
            <div class="col-6 col-md-3">
                <div class="card text-center border-0 bg-light h-100 shadow-none">
                    <div class="card-body py-2">
                        <div class="fs-4 fw-bold text-dark"><?= $walasStats['total_students'] ?></div>
                        <div class="small text-muted">Siswa Kelas</div>
                    </div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="card text-center border-0 bg-light h-100 shadow-none">
                    <div class="card-body py-2">
                        <div class="fs-4 fw-bold text-success"><?= $walasStats['hadir'] ?></div>
                        <div class="small text-muted">Hadir Hari Ini</div>
                    </div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="card text-center border-0 bg-light h-100 shadow-none">
                    <div class="card-body py-2">
                        <div class="fs-4 fw-bold text-warning"><?= $walasStats['sakit'] + $walasStats['izin'] ?></div>
                        <div class="small text-muted">Sakit/Izin</div>
                    </div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="card text-center border-0 bg-light h-100 shadow-none">
                    <div class="card-body py-2">
                        <div class="fs-4 fw-bold text-danger"><?= $walasStats['alpha'] ?></div>
                        <div class="small text-muted">Alpha</div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row g-3">
            <!-- Siswa Absen Hari Ini -->
            <div class="col-md-6">
                <div class="card border h-100 shadow-none">
                    <div class="card-header bg-white py-2">
                        <span class="fw-semibold text-danger small"><i class="bi bi-person-x me-1"></i>Siswa Absen Hari Ini</span>
                    </div>
                    <div class="card-body p-0">
                        <?php if (empty($walasStats['absent_list'])): ?>
                            <div class="text-center text-muted py-4 small">
                                <i class="bi bi-check-circle text-success fs-4 d-block mb-1"></i>
                                Semua siswa hadir hari ini!
                            </div>
                        <?php else: ?>
                            <div class="list-group list-group-flush small">
                                <?php foreach ($walasStats['absent_list'] as $abs): ?>
                                <div class="list-group-item d-flex align-items-center justify-content-between py-2">
                                    <div>
                                        <div class="fw-semibold"><?= esc($abs['full_name']) ?></div>
                                        <div class="text-muted" style="font-size:10px;">NIS: <?= esc($abs['nis']) ?></div>
                                    </div>
                                    <span class="badge <?= $abs['status'] === 'Alpha' ? 'bg-danger' : 'bg-warning text-dark' ?>"><?= esc($abs['status']) ?></span>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <!-- Pelanggaran Terbanyak -->
            <div class="col-md-6">
                <div class="card border h-100 shadow-none">
                    <div class="card-header bg-white py-2">
                        <span class="fw-semibold text-warning small"><i class="bi bi-exclamation-triangle me-1"></i>Pelanggaran Terbanyak</span>
                    </div>
                    <div class="card-body p-0">
                        <?php if (empty($walasStats['violation_list'])): ?>
                            <div class="text-center text-muted py-4 small">
                                <i class="bi bi-emoji-smile text-success fs-4 d-block mb-1"></i>
                                Tidak ada catatan pelanggaran di kelas ini!
                            </div>
                        <?php else: ?>
                            <div class="list-group list-group-flush small">
                                <?php foreach ($walasStats['violation_list'] as $viol): ?>
                                <div class="list-group-item d-flex align-items-center justify-content-between py-2">
                                    <div>
                                        <div class="fw-semibold"><?= esc($viol['full_name']) ?></div>
                                        <div class="text-muted" style="font-size:10px;">NIS: <?= esc($viol['nis']) ?></div>
                                    </div>
                                    <span class="badge bg-warning text-dark rounded-pill"><?= esc($viol['total_points']) ?> Poin</span>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- ================================================================ -->
<!-- BAGIAN GURU BK                                                     -->
<!-- ================================================================ -->
<?php if ($isBk): ?>
<div class="row g-3 mb-4">
    <!-- Stat: Hadir Hari Ini -->
    <div class="col-6 col-md-3">
        <div class="card text-center h-100 border-0 shadow-sm">
            <div class="card-body py-3">
                <div class="fs-2 fw-bold text-success"><?= $bkStats['hadir'] ?? 0 ?></div>
                <div class="small text-muted">Hadir Hari Ini</div>
                <i class="bi bi-person-check text-success opacity-25" style="font-size:2rem"></i>
            </div>
        </div>
    </div>

    <!-- Stat: Sakit -->
    <div class="col-6 col-md-3">
        <div class="card text-center h-100 border-0 shadow-sm">
            <div class="card-body py-3">
                <div class="fs-2 fw-bold text-info"><?= $bkStats['sakit'] ?? 0 ?></div>
                <div class="small text-muted">Sakit</div>
                <i class="bi bi-thermometer text-info opacity-25" style="font-size:2rem"></i>
            </div>
        </div>
    </div>

    <!-- Stat: Izin -->
    <div class="col-6 col-md-3">
        <div class="card text-center h-100 border-0 shadow-sm">
            <div class="card-body py-3">
                <div class="fs-2 fw-bold text-warning"><?= $bkStats['izin'] ?? 0 ?></div>
                <div class="small text-muted">Izin</div>
                <i class="bi bi-envelope text-warning opacity-25" style="font-size:2rem"></i>
            </div>
        </div>
    </div>

    <!-- Stat: Alpha -->
    <div class="col-6 col-md-3">
        <div class="card text-center h-100 border-0 shadow-sm">
            <div class="card-body py-3">
                <div class="fs-2 fw-bold <?= ($bkStats['alpha'] ?? 0) > 0 ? 'text-danger' : 'text-success' ?>">
                    <?= $bkStats['alpha'] ?? 0 ?>
                </div>
                <div class="small text-muted">Alpha</div>
                <i class="bi bi-person-x <?= ($bkStats['alpha'] ?? 0) > 0 ? 'text-danger' : 'text-success' ?> opacity-25" style="font-size:2rem"></i>
            </div>
        </div>
    </div>
</div>

<div class="row g-3 mb-4">
    <!-- Kelas Bimbingan -->
    <div class="col-md-6">
        <div class="card h-100 border-0 shadow-sm">
            <div class="card-header d-flex align-items-center justify-content-between py-2">
                <div class="d-flex align-items-center gap-2">
                    <i class="bi bi-people text-success"></i>
                    <span class="fw-semibold">Kelas Bimbingan BK</span>
                </div>
                <a href="/attendance" class="btn btn-sm btn-outline-success">
                    <i class="bi bi-calendar-check me-1"></i>Input Absensi
                </a>
            </div>
            <div class="card-body p-0">
                <?php if (empty($bkClassrooms)): ?>
                <div class="text-center text-muted py-4 small">Belum ada kelas bimbingan</div>
                <?php else: ?>
                <div class="list-group list-group-flush">
                    <?php foreach ($bkClassrooms as $cls): ?>
                    <div class="list-group-item d-flex align-items-center justify-content-between py-2">
                        <div>
                            <span class="fw-semibold"><?= esc($cls['name']) ?></span>
                            <span class="badge bg-secondary ms-2"><?= esc($cls['grade_name']) ?></span>
                        </div>
                        <a href="/attendance?classroom_id=<?= $cls['id'] ?>"
                           class="btn btn-sm btn-outline-secondary py-0">
                            <i class="bi bi-eye"></i>
                        </a>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Siswa Alpha Terbanyak Bulan Ini -->
    <div class="col-md-6">
        <div class="card h-100 border-0 shadow-sm">
            <div class="card-header d-flex align-items-center justify-content-between py-2">
                <div class="d-flex align-items-center gap-2">
                    <i class="bi bi-exclamation-triangle text-danger"></i>
                    <span class="fw-semibold">Alpha Terbanyak Bulan Ini</span>
                </div>
                <a href="/attendance/rekap-bulanan" class="btn btn-sm btn-outline-danger">
                    <i class="bi bi-graph-up me-1"></i>Rekap
                </a>
            </div>
            <div class="card-body p-0">
                <?php if (empty($bkStats['alphaList'])): ?>
                <div class="text-center text-muted py-4 small">
                    <i class="bi bi-check-circle text-success d-block fs-3 mb-2"></i>
                    Tidak ada siswa alpha bulan ini
                </div>
                <?php else: ?>
                <div class="list-group list-group-flush">
                    <?php foreach ($bkStats['alphaList'] as $s): ?>
                    <div class="list-group-item d-flex align-items-center justify-content-between py-2">
                        <div>
                            <div class="fw-semibold small"><?= esc($s['full_name']) ?></div>
                            <div class="text-muted" style="font-size:11px">
                                <?= esc($s['classroom_name']) ?> &mdash; NIS: <?= esc($s['nis']) ?>
                            </div>
                        </div>
                        <span class="badge bg-danger rounded-pill"><?= $s['alpha'] ?>x</span>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- ================================================================ -->
<!-- BAGIAN GURU TAHFIDZ                                                -->
<!-- ================================================================ -->
<?php if ($isTahfidz): ?>
<div class="card mb-4 border-0 shadow-sm">
    <div class="card-header bg-purple bg-opacity-10 text-purple py-3 d-flex align-items-center justify-content-between" style="background-color: rgba(111, 66, 193, 0.1); color: #6f42c1;">
        <h6 class="mb-0 fw-bold"><i class="bi bi-book-half me-2"></i>Program Tahfidz yang Diampu</h6>
    </div>
    <div class="card-body">
        <div class="row g-3">
            <?php foreach ($tahfidzPrograms as $prog): ?>
                <?php
                $typeName = match($prog['type']) {
                    'pk' => 'Tahfidz PK',
                    'boarding' => 'Tahfidz Boarding',
                    default => 'Tahfidz KBM Pagi'
                };
                ?>
                <div class="col-md-6 col-lg-4">
                    <div class="card border h-100 shadow-none">
                        <div class="card-body d-flex flex-column justify-content-between">
                            <div>
                                <div class="d-flex align-items-center justify-content-between mb-2">
                                    <span class="badge bg-opacity-10 text-purple" style="background-color: rgba(111, 66, 193, 0.1); color: #6f42c1;"><?= esc($typeName) ?></span>
                                    <span class="text-muted small"><i class="bi bi-people me-1"></i><?= esc($prog['student_count']) ?> Siswa</span>
                                </div>
                                <h6 class="fw-bold mb-1"><?= esc($prog['name']) ?></h6>
                                <p class="text-muted small mb-3"><?= esc($prog['description'] ?: 'Tidak ada deskripsi.') ?></p>
                            </div>
                            <div class="d-flex gap-2 mt-auto">
                                <a href="/guru/tahfidz/setoran?program_id=<?= $prog['id'] ?>" class="btn btn-sm btn-outline-purple w-50" style="color: #6f42c1; border-color: #6f42c1;">
                                    <i class="bi bi-pencil-square me-1"></i>Setoran
                                </a>
                                <a href="/guru/tahfidz/hafalan?program_id=<?= $prog['id'] ?>" class="btn btn-sm btn-outline-secondary w-50">
                                    <i class="bi bi-check2-square me-1"></i>Hafalan
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<?php endif; ?>

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