<?php
/**
 * SisPen Snesaka - Dashboard Guru
 */
require_once 'includes/functions.php';
requireLogin();

$pageTitle   = 'Dashboard Guru';
$activeMenu  = 'dashboard';

$currentUser = getCurrentUser();
$guruId      = $currentUser['guru_id'] ?? null;

$pengaturan  = getPengaturan();
$ta          = $pengaturan['tahun_ajaran_aktif'] ?? '2024/2025';
$sem         = $pengaturan['semester_aktif'] ?? '1';

// ── Deteksi role guru ───────────────────────────────────────
$isBK        = false;
$isWaliKelas = false;
$kelasBK     = [];
$kelasWali   = null;

if ($guruId) {
    $guruData    = db_fetch("SELECT is_bk FROM guru WHERE id=?", [$guruId]);
    $isBK        = !empty($guruData['is_bk']);

    // Cek wali kelas
    $kelasWali = db_fetch(
        "SELECT k.*, COUNT(s.id) as jumlah_siswa
         FROM kelas k
         LEFT JOIN siswa s ON s.kelas_id=k.id AND s.status='aktif'
         WHERE k.wali_kelas_id=? AND k.tahun_ajaran=? AND k.semester=?
         GROUP BY k.id LIMIT 1",
        [$guruId, $ta, $sem]
    );
    $isWaliKelas = !empty($kelasWali);

    // Kelas BK yang ditugaskan
    // Pisahkan subquery siswa & absensi agar tidak terjadi Cartesian product
    if ($isBK) {
        $kelasBK = db_fetchAll(
            "SELECT k.*,
                    (SELECT COUNT(*) FROM siswa s
                     WHERE s.kelas_id=k.id AND s.status='aktif') as jumlah_siswa,
                    (SELECT COALESCE(SUM(a.sakit),0) FROM absensi a
                     WHERE a.kelas_id=k.id AND a.tahun_ajaran=? AND a.semester=?) as tot_sakit,
                    (SELECT COALESCE(SUM(a.izin),0) FROM absensi a
                     WHERE a.kelas_id=k.id AND a.tahun_ajaran=? AND a.semester=?) as tot_izin,
                    (SELECT COALESCE(SUM(a.tanpa_keterangan),0) FROM absensi a
                     WHERE a.kelas_id=k.id AND a.tahun_ajaran=? AND a.semester=?) as tot_alpha
             FROM bk_kelas bk
             JOIN kelas k ON k.id=bk.kelas_id
             WHERE bk.guru_id=? AND bk.tahun_ajaran=? AND bk.semester=?
             ORDER BY k.tingkat, k.nama_kelas",
            [$ta, $sem, $ta, $sem, $ta, $sem, $guruId, $ta, $sem]
        );
    }
}

// ── Data untuk guru mapel ───────────────────────────────────
$kelasDiampu     = [];
$totalSiswa      = 0;
$totalNilaiInput = 0;

if ($guruId) {
    $kelasDiampu = db_fetchAll("
        SELECT DISTINCT k.*, m.nama_mapel, m.id as mapel_id,
               COUNT(DISTINCT s.id)  as jumlah_siswa,
               COUNT(DISTINCT n.id)  as sudah_input
        FROM pengampu p
        JOIN kelas k ON k.id=p.kelas_id
        JOIN mata_pelajaran m ON m.id=p.mapel_id
        LEFT JOIN siswa s ON s.kelas_id=k.id AND s.status='aktif'
        LEFT JOIN nilai n ON n.siswa_id=s.id AND n.mapel_id=m.id AND n.tahun_ajaran=? AND n.semester=?
        WHERE p.guru_id=? AND p.tahun_ajaran=? AND p.semester=?
        GROUP BY p.id
        ORDER BY k.nama_kelas
    ", [$ta, $sem, $guruId, $ta, $sem]);

    foreach ($kelasDiampu as $k) {
        $totalSiswa      += $k['jumlah_siswa'];
        $totalNilaiInput += $k['sudah_input'];
    }
}

// ── Absensi wali kelas (ringkasan) ─────────────────────────
$absensiWali = null;
if ($isWaliKelas && $kelasWali) {
    $absensiWali = db_fetch(
        "SELECT SUM(sakit) as tot_s, SUM(izin) as tot_i, SUM(tanpa_keterangan) as tot_a
         FROM absensi WHERE kelas_id=? AND tahun_ajaran=? AND semester=?",
        [$kelasWali['id'], $ta, $sem]
    );
}

include 'templates/header.php';
?>

<!-- ── Welcome Banner ─────────────────────────────────────── -->
<div style="background:linear-gradient(135deg,<?= $isBK?'#065f46,#059669':'var(--primary-dark),var(--primary-light)' ?>);border-radius:12px;padding:24px 28px;margin-bottom:20px;color:#fff;display:flex;align-items:center;justify-content:space-between;gap:20px;flex-wrap:wrap;">
  <div style="display:flex;align-items:center;gap:16px;">
    <div style="width:56px;height:56px;background:rgba(255,255,255,.15);border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:24px;flex-shrink:0;">
      <i class="fas fa-<?= $isBK?'user-md':'user-tie' ?>"></i>
    </div>
    <div>
      <h2 style="font-size:18px;font-weight:700;margin-bottom:4px;">
        Selamat Datang, <?= htmlspecialchars($currentUser['nama_lengkap'] ?? '') ?>!
      </h2>
      <p style="font-size:13px;opacity:.8;">
        TA <?= $ta ?> &bull; Semester <?= $sem ?>
        <?php if ($isBK): ?>
          &bull; <span style="background:rgba(255,255,255,.2);padding:2px 10px;border-radius:20px;font-size:12px;">
            <i class="fas fa-user-md"></i> Guru BK &mdash; <?= count($kelasBK) ?> kelas bimbingan
          </span>
        <?php elseif ($isWaliKelas): ?>
          &bull; <span style="background:rgba(255,255,255,.2);padding:2px 10px;border-radius:20px;font-size:12px;">
            <i class="fas fa-chalkboard-teacher"></i> Wali Kelas <?= htmlspecialchars($kelasWali['nama_kelas']) ?>
          </span>
        <?php else: ?>
          &bull; <?= count($kelasDiampu) ?> mata pelajaran diampu
        <?php endif; ?>
      </p>
    </div>
  </div>
  <?php if ($isBK): ?>
  <a href="<?= BASE_URL ?>/modules/absensi/input.php" class="btn" style="background:rgba(255,255,255,.2);color:#fff;border:1px solid rgba(255,255,255,.4);font-size:13px;">
    <i class="fas fa-calendar-check"></i> Input Absensi
  </a>
  <?php elseif ($isWaliKelas): ?>
  <a href="<?= BASE_URL ?>/modules/absensi/input.php?kelas_id=<?= $kelasWali['id'] ?>" class="btn" style="background:rgba(255,255,255,.2);color:#fff;border:1px solid rgba(255,255,255,.4);font-size:13px;">
    <i class="fas fa-calendar-check"></i> Input Absensi Kelas
  </a>
  <?php endif; ?>
</div>

<?php if ($isBK): ?>
<!-- ═══════════════════════════════════════════════════════════
     TAMPILAN KHUSUS GURU BK
     ═══════════════════════════════════════════════════════ -->

<!-- Stat cards BK -->
<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:14px;margin-bottom:20px;">
  <div style="background:linear-gradient(135deg,#065f46,#059669);color:#fff;border-radius:12px;padding:18px;display:flex;align-items:center;gap:12px;box-shadow:0 4px 15px rgba(0,0,0,.1);">
    <div style="width:48px;height:48px;background:rgba(255,255,255,.2);border-radius:10px;display:flex;align-items:center;justify-content:center;font-size:20px;flex-shrink:0;">
      <i class="fas fa-door-open"></i>
    </div>
    <div>
      <div style="font-size:11px;opacity:.8;">Kelas Bimbingan</div>
      <div style="font-size:30px;font-weight:800;line-height:1.1;"><?= count($kelasBK) ?></div>
      <div style="font-size:11px;opacity:.7;">kelas</div>
    </div>
  </div>
  <div style="background:linear-gradient(135deg,#1e40af,#3b82f6);color:#fff;border-radius:12px;padding:18px;display:flex;align-items:center;gap:12px;box-shadow:0 4px 15px rgba(0,0,0,.1);">
    <div style="width:48px;height:48px;background:rgba(255,255,255,.2);border-radius:10px;display:flex;align-items:center;justify-content:center;font-size:20px;flex-shrink:0;">
      <i class="fas fa-users"></i>
    </div>
    <div>
      <div style="font-size:11px;opacity:.8;">Total Siswa</div>
      <div style="font-size:30px;font-weight:800;line-height:1.1;"><?= array_sum(array_column($kelasBK,'jumlah_siswa')) ?></div>
      <div style="font-size:11px;opacity:.7;">dibimbing</div>
    </div>
  </div>
  <div style="background:linear-gradient(135deg,#b45309,#f59e0b);color:#fff;border-radius:12px;padding:18px;display:flex;align-items:center;gap:12px;box-shadow:0 4px 15px rgba(0,0,0,.1);">
    <div style="width:48px;height:48px;background:rgba(255,255,255,.2);border-radius:10px;display:flex;align-items:center;justify-content:center;font-size:20px;flex-shrink:0;">
      <i class="fas fa-calendar-check"></i>
    </div>
    <div>
      <div style="font-size:11px;opacity:.8;">Total Absensi</div>
      <?php
        $totAbsBK = array_sum(array_column($kelasBK,'tot_sakit'))
                  + array_sum(array_column($kelasBK,'tot_izin'))
                  + array_sum(array_column($kelasBK,'tot_alpha'));
      ?>
      <div style="font-size:30px;font-weight:800;line-height:1.1;"><?= $totAbsBK ?></div>
      <div style="font-size:11px;opacity:.7;">hari (S+I+A)</div>
    </div>
  </div>
  <div style="background:linear-gradient(135deg,#dc2626,#f87171);color:#fff;border-radius:12px;padding:18px;display:flex;align-items:center;gap:12px;box-shadow:0 4px 15px rgba(0,0,0,.1);">
    <div style="width:48px;height:48px;background:rgba(255,255,255,.2);border-radius:10px;display:flex;align-items:center;justify-content:center;font-size:20px;flex-shrink:0;">
      <i class="fas fa-times-circle"></i>
    </div>
    <div>
      <div style="font-size:11px;opacity:.8;">Total Alpha</div>
      <div style="font-size:30px;font-weight:800;line-height:1.1;"><?= array_sum(array_column($kelasBK,'tot_alpha')) ?></div>
      <div style="font-size:11px;opacity:.7;">hari</div>
    </div>
  </div>
</div>

<!-- Tabel kelas bimbingan BK -->
<div class="card">
  <div class="card-header">
    <span class="card-title">
      <i class="fas fa-user-md" style="color:#059669;margin-right:8px;"></i>
      Kelas Bimbingan
    </span>
    <a href="<?= BASE_URL ?>/modules/absensi/input.php" class="btn btn-primary btn-sm">
      <i class="fas fa-calendar-check"></i> Input Absensi
    </a>
  </div>

  <?php if (empty($kelasBK)): ?>
  <div class="card-body" style="text-align:center;padding:40px;color:var(--text-muted);">
    <i class="fas fa-inbox" style="font-size:36px;opacity:.2;display:block;margin-bottom:12px;"></i>
    <p>Belum ada kelas bimbingan yang ditugaskan.</p>
    <p style="font-size:12px;">Hubungi Administrator untuk mengatur kelas bimbingan Anda.</p>
  </div>
  <?php else: ?>
  <div style="overflow-x:auto;">
    <table style="width:100%;border-collapse:collapse;font-size:13px;">
      <thead>
        <tr style="background:var(--primary-dark);color:#fff;">
          <th style="padding:10px 14px;">Kelas</th>
          <th style="padding:10px 10px;text-align:center;">Siswa</th>
          <th style="padding:10px 10px;text-align:center;background:rgba(59,130,246,.3);">Sakit</th>
          <th style="padding:10px 10px;text-align:center;background:rgba(245,158,11,.3);">Izin</th>
          <th style="padding:10px 10px;text-align:center;background:rgba(239,68,68,.3);">Alpha</th>
          <th style="padding:10px 10px;text-align:center;">Total Absen</th>
          <th style="padding:10px 14px;text-align:center;">Aksi</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($kelasBK as $idx => $k): ?>
        <tr style="<?= $idx%2===0?'':'background:var(--surface-2);' ?>">
          <td style="padding:10px 14px;">
            <span class="badge badge-blue" style="font-size:13px;"><?= htmlspecialchars($k['nama_kelas']) ?></span>
          </td>
          <td style="padding:10px;text-align:center;font-weight:700;"><?= $k['jumlah_siswa'] ?></td>
          <td style="padding:10px;text-align:center;font-weight:700;color:#1d4ed8;"><?= intval($k['tot_sakit']) ?></td>
          <td style="padding:10px;text-align:center;font-weight:700;color:#b45309;"><?= intval($k['tot_izin']) ?></td>
          <td style="padding:10px;text-align:center;font-weight:700;color:#dc2626;"><?= intval($k['tot_alpha']) ?></td>
          <?php $totKelas = intval($k['tot_sakit'])+intval($k['tot_izin'])+intval($k['tot_alpha']); ?>
          <td style="padding:10px;text-align:center;">
            <span style="font-weight:800;font-size:15px;color:<?= $totKelas>20?'#dc2626':($totKelas>10?'#d97706':'#16a34a') ?>;"><?= $totKelas ?></span>
          </td>
          <td style="padding:10px;text-align:center;">
            <a href="<?= BASE_URL ?>/modules/absensi/input.php?kelas_id=<?= $k['id'] ?>" class="btn btn-primary btn-xs">
              <i class="fas fa-edit"></i> Input
            </a>
            <a href="<?= BASE_URL ?>/modules/absensi/report.php?kelas_id=<?= $k['id'] ?>" class="btn btn-outline btn-xs">
              <i class="fas fa-chart-bar"></i> Rekap
            </a>
          </td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
  <?php endif; ?>
</div>

<?php else: ?>
<!-- ═══════════════════════════════════════════════════════════
     TAMPILAN GURU MAPEL / WALI KELAS
     ═══════════════════════════════════════════════════════ -->

<!-- Stat cards guru mapel -->
<div class="stat-grid" style="grid-template-columns:repeat(<?= $isWaliKelas?4:3 ?>,1fr);margin-bottom:20px;">

  <!-- Card 1: Kelas Diampu — Deep Navy -->
  <div style="background:linear-gradient(135deg,#0f2544 0%,#1a3a6b 55%,#1e4d8c 100%);border:1px solid rgba(99,179,237,.18);border-radius:16px;padding:22px 20px;display:flex;align-items:center;gap:16px;position:relative;overflow:hidden;box-shadow:0 8px 28px rgba(10,30,80,.25),inset 0 1px 0 rgba(255,255,255,.07);transition:transform .2s;" onmouseover="this.style.transform='translateY(-3px)'" onmouseout="this.style.transform='translateY(0)'">
    <div style="position:absolute;top:-28px;right:-28px;width:100px;height:100px;border-radius:50%;background:radial-gradient(circle,rgba(99,179,237,.2) 0%,transparent 70%);pointer-events:none;"></div>
    <div style="width:50px;height:50px;flex-shrink:0;border-radius:14px;background:rgba(99,179,237,.18);border:1px solid rgba(99,179,237,.3);display:flex;align-items:center;justify-content:center;font-size:22px;color:#90cdf4;">
      <i class="fas fa-chalkboard"></i>
    </div>
    <div style="position:relative;z-index:1;">
      <div style="font-size:10px;font-weight:700;color:rgba(147,197,253,.75);text-transform:uppercase;letter-spacing:.8px;margin-bottom:3px;">Kelas Diampu</div>
      <div style="font-size:36px;font-weight:900;color:#fff;line-height:1;letter-spacing:-1px;"><?= count($kelasDiampu) ?></div>
      <div style="font-size:11px;font-weight:600;color:rgba(147,197,253,.6);margin-top:2px;">Kelas</div>
    </div>
  </div>

  <!-- Card 2: Total Siswa — Emerald -->
  <div style="background:linear-gradient(135deg,#064e3b 0%,#065f46 50%,#047857 100%);border:1px solid rgba(52,211,153,.18);border-radius:16px;padding:22px 20px;display:flex;align-items:center;gap:16px;position:relative;overflow:hidden;box-shadow:0 8px 28px rgba(4,60,40,.3),inset 0 1px 0 rgba(255,255,255,.07);transition:transform .2s;" onmouseover="this.style.transform='translateY(-3px)'" onmouseout="this.style.transform='translateY(0)'">
    <div style="position:absolute;top:-28px;right:-28px;width:100px;height:100px;border-radius:50%;background:radial-gradient(circle,rgba(52,211,153,.2) 0%,transparent 70%);pointer-events:none;"></div>
    <div style="width:50px;height:50px;flex-shrink:0;border-radius:14px;background:rgba(52,211,153,.18);border:1px solid rgba(52,211,153,.3);display:flex;align-items:center;justify-content:center;font-size:22px;color:#6ee7b7;">
      <i class="fas fa-users"></i>
    </div>
    <div style="position:relative;z-index:1;">
      <div style="font-size:10px;font-weight:700;color:rgba(110,231,183,.75);text-transform:uppercase;letter-spacing:.8px;margin-bottom:3px;">Total Siswa</div>
      <div style="font-size:36px;font-weight:900;color:#fff;line-height:1;letter-spacing:-1px;"><?= $totalSiswa ?></div>
      <div style="font-size:11px;font-weight:600;color:rgba(110,231,183,.6);margin-top:2px;">Siswa</div>
    </div>
  </div>

  <!-- Card 3: Nilai Terinput — Amber Gold -->
  <div style="background:linear-gradient(135deg,#78350f 0%,#92400e 50%,#b45309 100%);border:1px solid rgba(251,191,36,.18);border-radius:16px;padding:22px 20px;display:flex;align-items:center;gap:16px;position:relative;overflow:hidden;box-shadow:0 8px 28px rgba(80,40,0,.3),inset 0 1px 0 rgba(255,255,255,.07);transition:transform .2s;" onmouseover="this.style.transform='translateY(-3px)'" onmouseout="this.style.transform='translateY(0)'">
    <div style="position:absolute;top:-28px;right:-28px;width:100px;height:100px;border-radius:50%;background:radial-gradient(circle,rgba(251,191,36,.25) 0%,transparent 70%);pointer-events:none;"></div>
    <div style="width:50px;height:50px;flex-shrink:0;border-radius:14px;background:rgba(251,191,36,.18);border:1px solid rgba(251,191,36,.3);display:flex;align-items:center;justify-content:center;font-size:22px;color:#fcd34d;">
      <i class="fas fa-check-circle"></i>
    </div>
    <div style="position:relative;z-index:1;">
      <div style="font-size:10px;font-weight:700;color:rgba(252,211,77,.75);text-transform:uppercase;letter-spacing:.8px;margin-bottom:3px;">Nilai Terinput</div>
      <div style="font-size:36px;font-weight:900;color:#fff;line-height:1;letter-spacing:-1px;"><?= $totalNilaiInput ?></div>
      <div style="font-size:11px;font-weight:600;color:rgba(252,211,77,.6);margin-top:2px;">Dari total siswa</div>
    </div>
  </div>

  <!-- Card 4: Absensi Wali Kelas — Deep Violet -->
  <?php if ($isWaliKelas && $kelasWali): ?>
  <?php $totAbs = intval($absensiWali['tot_s'])+intval($absensiWali['tot_i'])+intval($absensiWali['tot_a']); ?>
  <div style="background:linear-gradient(135deg,#3b0764 0%,#4c1d95 50%,#5b21b6 100%);border:1px solid rgba(167,139,250,.18);border-radius:16px;padding:22px 20px;display:flex;align-items:center;gap:16px;position:relative;overflow:hidden;box-shadow:0 8px 28px rgba(40,10,80,.3),inset 0 1px 0 rgba(255,255,255,.07);transition:transform .2s;" onmouseover="this.style.transform='translateY(-3px)'" onmouseout="this.style.transform='translateY(0)'">
    <div style="position:absolute;top:-28px;right:-28px;width:100px;height:100px;border-radius:50%;background:radial-gradient(circle,rgba(167,139,250,.22) 0%,transparent 70%);pointer-events:none;"></div>
    <div style="width:50px;height:50px;flex-shrink:0;border-radius:14px;background:rgba(167,139,250,.18);border:1px solid rgba(167,139,250,.3);display:flex;align-items:center;justify-content:center;font-size:22px;color:#c4b5fd;">
      <i class="fas fa-calendar-check"></i>
    </div>
    <div style="position:relative;z-index:1;">
      <div style="font-size:10px;font-weight:700;color:rgba(196,181,253,.75);text-transform:uppercase;letter-spacing:.8px;margin-bottom:3px;">Total Absensi</div>
      <div style="font-size:36px;font-weight:900;color:#fff;line-height:1;letter-spacing:-1px;"><?= $totAbs ?></div>
      <div style="font-size:11px;font-weight:600;color:rgba(196,181,253,.6);margin-top:2px;">Kelas <?= htmlspecialchars($kelasWali['nama_kelas']) ?></div>
    </div>
  </div>
  <?php endif; ?>

</div>

<!-- Card wali kelas (jika ada) -->
<?php if ($isWaliKelas && $kelasWali): ?>
<div class="card" style="margin-bottom:16px;">
  <div class="card-header">
    <span class="card-title">
      <i class="fas fa-chalkboard-teacher" style="color:#0f766e;margin-right:8px;"></i>
      Wali Kelas &mdash; <strong><?= htmlspecialchars($kelasWali['nama_kelas']) ?></strong>
      <span class="badge badge-success" style="margin-left:6px;"><?= $kelasWali['jumlah_siswa'] ?> siswa</span>
    </span>
    <div style="display:flex;gap:8px;">
      <a href="<?= BASE_URL ?>/modules/absensi/input.php?kelas_id=<?= $kelasWali['id'] ?>" class="btn btn-primary btn-sm">
        <i class="fas fa-calendar-check"></i> Input Absensi
      </a>
      <a href="<?= BASE_URL ?>/modules/absensi/report.php?kelas_id=<?= $kelasWali['id'] ?>" class="btn btn-outline btn-sm">
        <i class="fas fa-chart-bar"></i> Rekap
      </a>
    </div>
  </div>
  <div style="padding:14px 20px;display:flex;gap:24px;font-size:13px;flex-wrap:wrap;">
    <div style="display:flex;align-items:center;gap:8px;">
      <span style="width:32px;height:32px;background:#dbeafe;border-radius:8px;display:flex;align-items:center;justify-content:center;color:#1d4ed8;"><i class="fas fa-thermometer-half"></i></span>
      <div><div style="font-size:10px;color:var(--text-muted);">Sakit</div><div style="font-weight:800;font-size:18px;color:#1d4ed8;"><?= intval($absensiWali['tot_s']) ?></div></div>
    </div>
    <div style="display:flex;align-items:center;gap:8px;">
      <span style="width:32px;height:32px;background:#fef3c7;border-radius:8px;display:flex;align-items:center;justify-content:center;color:#b45309;"><i class="fas fa-file-alt"></i></span>
      <div><div style="font-size:10px;color:var(--text-muted);">Izin</div><div style="font-weight:800;font-size:18px;color:#b45309;"><?= intval($absensiWali['tot_i']) ?></div></div>
    </div>
    <div style="display:flex;align-items:center;gap:8px;">
      <span style="width:32px;height:32px;background:#fee2e2;border-radius:8px;display:flex;align-items:center;justify-content:center;color:#dc2626;"><i class="fas fa-times-circle"></i></span>
      <div><div style="font-size:10px;color:var(--text-muted);">Alpha</div><div style="font-weight:800;font-size:18px;color:#dc2626;"><?= intval($absensiWali['tot_a']) ?></div></div>
    </div>
  </div>
</div>
<?php endif; ?>

<!-- Tabel kelas diampu -->
<div class="card">
  <div class="card-header">
    <span class="card-title"><i class="fas fa-chalkboard-teacher" style="color:var(--primary);margin-right:8px;"></i>Kelas yang Diampu</span>
  </div>
  <div class="table-responsive">
    <table class="table">
      <thead>
        <tr>
          <th>Kelas</th>
          <th>Mata Pelajaran</th>
          <th>Jumlah Siswa</th>
          <th>Progress Nilai</th>
          <th>Aksi</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($kelasDiampu)): ?>
        <tr><td colspan="5" class="text-center" style="padding:32px;color:var(--text-muted);">
          <i class="fas fa-inbox" style="font-size:28px;margin-bottom:8px;display:block;opacity:.4;"></i>
          Belum ada kelas yang diampu
        </td></tr>
        <?php else: ?>
        <?php foreach ($kelasDiampu as $k):
          $pct = $k['jumlah_siswa'] > 0 ? round(($k['sudah_input']/$k['jumlah_siswa'])*100) : 0;
        ?>
        <tr>
          <td><span class="badge badge-blue"><?= htmlspecialchars($k['nama_kelas']) ?></span></td>
          <td><?= htmlspecialchars($k['nama_mapel']) ?></td>
          <td><strong><?= $k['jumlah_siswa'] ?></strong> siswa</td>
          <td style="min-width:160px;">
            <div style="display:flex;align-items:center;gap:8px;">
              <div class="progress" style="flex:1;">
                <div class="progress-bar" style="width:<?= $pct ?>%;background:<?= $pct>=100?'var(--success)':'var(--primary)' ?>;"></div>
              </div>
              <span style="font-size:12px;color:var(--text-muted);white-space:nowrap;"><?= $pct ?>%</span>
            </div>
          </td>
          <td>
            <a href="<?= BASE_URL ?>/modules/nilai/input.php?kelas_id=<?= $k['id'] ?>&mapel_id=<?= $k['mapel_id'] ?>" class="btn btn-primary btn-xs">
              <i class="fas fa-edit"></i> Input Nilai
            </a>
            <a href="<?= BASE_URL ?>/cetak_nilai_guru.php?kelas_id=<?= $k['id'] ?>&mapel_id=<?= $k['mapel_id'] ?>" class="btn btn-outline btn-xs">
              <i class="fas fa-print"></i> Cetak
            </a>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<?php endif; ?>

<?php include 'templates/footer.php'; ?>