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

$pengaturan  = getPengaturan();
$ta          = $pengaturan['tahun_ajaran_aktif'];
$semester    = $pengaturan['semester_aktif'];
$isAdmin     = $_SESSION['role'] === 'admin';
$currentUser = getCurrentUser();
$guruId      = $currentUser['guru_id'] ?? null;
$formula     = getFormula();
$jsSumatif   = $formula['jumlah_sumatif'];

// Tanggal cetak — bisa diubah via form, default hari ini
$namaBulan  = array(1=>'Januari',2=>'Februari',3=>'Maret',4=>'April',5=>'Mei',6=>'Juni',
                    7=>'Juli',8=>'Agustus',9=>'September',10=>'Oktober',11=>'November',12=>'Desember');
$kecamatan  = trim($pengaturan['kecamatan'] ?? '');

// Ambil tanggal dari GET jika ada, validasi format YYYY-MM-DD
$tglCetakRaw = $_GET['tgl_cetak'] ?? '';
if ($tglCetakRaw && preg_match('/^\d{4}-\d{2}-\d{2}$/', $tglCetakRaw)) {
    $tglObj = date_create($tglCetakRaw);
    $tglHariIni = intval(date_format($tglObj,'j')) . ' ' . $namaBulan[intval(date_format($tglObj,'n'))] . ' ' . date_format($tglObj,'Y');
    $tglCetak = $tglCetakRaw;
} else {
    $tglHariIni = intval(date('j')) . ' ' . $namaBulan[intval(date('n'))] . ' ' . date('Y');
    $tglCetak   = date('Y-m-d');
}

// Ambil tempat dari GET, default dari kecamatan pengaturan
$tempatCetak = trim($_GET['tempat_cetak'] ?? '');
if ($tempatCetak === '') {
    $tempatCetak = $kecamatan !== '' ? $kecamatan : 'Sekaran';
}
$tempatTgl = $tempatCetak . ', ' . $tglHariIni;

$kelasId = intval($_GET['kelas_id'] ?? 0);
$mapelId = intval($_GET['mapel_id'] ?? 0);
$doCetak = isset($_GET['cetak']);

// Kelas & mapel sesuai role
if ($isAdmin) {
    $kelasList = db_fetchAll("SELECT * FROM kelas WHERE tahun_ajaran=? AND semester=? ORDER BY tingkat, nama_kelas", [$ta, $semester]);
    $mapelList = $kelasId ? db_fetchAll("SELECT * FROM mata_pelajaran WHERE status='aktif' ORDER BY kelompok, urutan") : array();
} else {
    $kelasList = $guruId ? db_fetchAll("
        SELECT DISTINCT k.* FROM pengampu p JOIN kelas k ON k.id=p.kelas_id
        WHERE p.guru_id=? AND p.tahun_ajaran=? AND p.semester=? ORDER BY k.tingkat, k.nama_kelas
    ", [$guruId, $ta, $semester]) : array();
    $mapelList = ($kelasId && $guruId) ? db_fetchAll("
        SELECT m.* FROM pengampu p JOIN mata_pelajaran m ON m.id=p.mapel_id
        WHERE p.guru_id=? AND p.kelas_id=? AND p.tahun_ajaran=? AND p.semester=?
    ", [$guruId, $kelasId, $ta, $semester]) : array();
}

// Data siswa & nilai
$siswas = array();
$nilais = array();
$selectedKelas = null;
$selectedMapel = null;
$guruMapel     = null;

if ($kelasId && $mapelId) {
    $selectedKelas = db_fetch("SELECT * FROM kelas WHERE id=?", [$kelasId]);
    $selectedMapel = db_fetch("SELECT * FROM mata_pelajaran WHERE id=?", [$mapelId]);

    // Cari guru pengampu mapel ini
    $pengampu = db_fetch("
        SELECT g.nama, g.nip FROM pengampu p JOIN guru g ON g.id=p.guru_id
        WHERE p.mapel_id=? AND p.kelas_id=? AND p.tahun_ajaran=? AND p.semester=?
        LIMIT 1
    ", [$mapelId, $kelasId, $ta, $semester]);
    $guruMapel = $pengampu;

    $siswas = db_fetchAll(
        "SELECT * FROM siswa WHERE kelas_id=? AND status='aktif' ORDER BY nama",
        [$kelasId]
    );
    foreach ($siswas as $s) {
        $n = db_fetch("SELECT * FROM nilai WHERE siswa_id=? AND mapel_id=? AND kelas_id=? AND tahun_ajaran=? AND semester=?",
            [$s['id'], $mapelId, $kelasId, $ta, $semester]);
        $nilais[$s['id']] = $n;
    }
}

$semesterLabel = $semester === '1' ? 'Ganjil' : 'Genap';

// Jika mode cetak langsung, tampilkan hanya print view
if ($doCetak && $kelasId && $mapelId): ?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Daftar Nilai - <?= htmlspecialchars($selectedKelas['nama_kelas'] ?? '') ?></title>
<style>
  * { margin:0; padding:0; box-sizing:border-box; }
  body { font-family: Arial, sans-serif; font-size:10pt; color:#000; background:#fff; }

  /* ── Tampilan layar (preview) ── */
  .page { width:210mm; margin:0 auto; padding:14mm 14mm 14mm; }

  h1.judul     { text-align:center; font-size:13pt; font-weight:bold; text-transform:uppercase; letter-spacing:.5px; margin-bottom:2px; }
  h2.sub-judul { text-align:center; font-size:11pt; font-weight:bold; margin-bottom:10px; }

  .info-table { width:100%; border-collapse:collapse; margin-bottom:10px; }
  .info-table td { padding:2px 4px; font-size:10pt; vertical-align:top; }
  .info-table td:nth-child(1) { width:110px; }
  .info-table td:nth-child(2) { width:8px; }
  .info-table td:nth-child(3) { width:auto; }
  .info-table td:nth-child(4) { width:100px; }
  .info-table td:nth-child(5) { width:8px; }

  .nilai-table { width:100%; border-collapse:collapse; margin-top:6px; }
  .nilai-table th, .nilai-table td {
    border:1px solid #000;
    padding:3px 5px;
    text-align:center;
    font-size:9pt;
    white-space:nowrap;        /* nama siswa 1 baris */
  }
  .nilai-table th { font-weight:bold; background:#fff; }
  .nilai-table td.nama {
    text-align:left;
    padding-left:6px;
    white-space:nowrap;        /* paksa 1 baris */
    overflow:hidden;
    text-overflow:ellipsis;
    max-width:180px;
  }
  .nilai-table .no-col   { width:26px; }
  .nilai-table .nama-col { width:180px; text-align:left; }
  .nilai-table .val-col  { width:38px; }
  .nilai-table .nr-col   { width:48px; font-weight:bold; }

  .ttd-table { width:100%; border-collapse:collapse; margin-top:16px; }
  .ttd-table td { padding:3px 6px; vertical-align:top; font-size:10pt; }
  .ttd-table .ttd-space { height:44px; }

  /* ── PRINT: hilangkan header/footer browser, paksa 1 halaman ── */
  @media print {
    /* Hilangkan header & footer bawaan browser */
    @page {
      size: A4 portrait;
      margin: 13mm 13mm 13mm 13mm;  /* simetris semua sisi */
    }

    html, body { margin:0; padding:0; }

    .page {
      width:100%;
      padding:0;
      min-height:unset;
    }

    /* Kompres agar 32 siswa muat 1 halaman */
    h1.judul     { font-size:12pt; margin-bottom:1px; }
    h2.sub-judul { font-size:10.5pt; margin-bottom:6px; }

    .info-table         { margin-bottom:5px; }
    .info-table td      { font-size:9pt; padding:1px 3px; line-height:1.4; }

    .nilai-table th,
    .nilai-table td     {
      font-size:8pt;
      padding:0 3px;
      line-height:1.2;
      white-space:nowrap;
      height: var(--row-h, 7.5mm);  /* tinggi baris otomatis dari PHP */
    }
    .nilai-table thead th {
      height: auto;
      padding: 2px 3px;
    }
    .nilai-table td.nama {
      font-size:8pt;
      white-space:nowrap;
      overflow:hidden;
      text-overflow:ellipsis;
      max-width:160px;
    }

    .ttd-table          { margin-top:8px; }
    .ttd-table td       { font-size:9pt; padding:1px 4px; line-height:1.5; }
    .ttd-table .ttd-space { height:34px; }

    /* Jangan biarkan tabel & baris terpotong */
    .nilai-table        { page-break-inside:avoid; }
    .ttd-table          { page-break-inside:avoid; page-break-before:avoid; }
    tr                  { page-break-inside:avoid; }
  }
</style>
</head>
<body>
<div class="page" id="mainpage">

<script>
// Hitung tinggi baris otomatis berdasarkan jumlah siswa
(function(){
  var n = <?= max(count($siswas), 5) ?>;
  // A4 content area ~247mm, dikurangi: judul+info~30mm, thead~14mm, ttd~28mm = ~175mm untuk tbody
  var available = 175; // mm
  var rowH = Math.min(8.5, Math.max(5, available / n));
  document.documentElement.style.setProperty('--row-h', rowH.toFixed(1) + 'mm');
})();
</script>

  <h1 class="judul">DAFTAR NILAI SISWA</h1>
  <h2 class="sub-judul"><?= htmlspecialchars($pengaturan['nama_sekolah'] ?? 'Nama Sekolah') ?></h2>

  <table class="info-table">
    <tr>
      <td>Kelas</td><td>:</td>
      <td><strong><?= htmlspecialchars($selectedKelas['nama_kelas'] ?? '') ?></strong></td>
      <td style="text-align:left;padding-left:60px;">Semester</td><td>:</td>
      <td><strong><?= $semesterLabel ?></strong></td>
    </tr>
    <tr>
      <td>Mata Pelajaran</td><td>:</td>
      <td><strong><?= htmlspecialchars($selectedMapel['nama_mapel'] ?? '') ?></strong></td>
      <td style="text-align:left;padding-left:60px;">T.P.</td><td>:</td>
      <td><strong><?= htmlspecialchars($ta) ?></strong></td>
    </tr>
  </table>

  <table class="nilai-table">
    <thead>
      <tr>
        <th rowspan="2" class="no-col">NO</th>
        <th rowspan="2" class="nama-col">NAMA SISWA</th>
        <th colspan="<?= $jsSumatif ?>">Sumatif ke</th>
        <th rowspan="2" class="val-col">Rata2<br>Sumatif</th>
        <th rowspan="2" class="val-col">STS</th>
        <th rowspan="2" class="val-col">SAS</th>
        <th rowspan="2" class="nr-col">NILAI<br>RAPOR<br>(NR)</th>
      </tr>
      <tr>
        <?php for ($i = 1; $i <= $jsSumatif; $i++): ?>
        <th class="val-col"><?= $i ?></th>
        <?php endfor; ?>
      </tr>
    </thead>
    <tbody>
      <?php if (empty($siswas)): ?>
      <tr><td colspan="<?= 3 + $jsSumatif + 3 ?>" style="padding:20px;text-align:center;">Belum ada data</td></tr>
      <?php else: ?>
      <?php foreach ($siswas as $i => $s):
        $n = $nilais[$s['id']] ?? null;
      ?>
      <tr>
        <td><?= $i + 1 ?></td>
        <td class="nama"><?= htmlspecialchars($s['nama']) ?></td>
        <?php for ($j = 1; $j <= $jsSumatif; $j++):
          $key = 'sumatif_' . $j;
          $val = ($n && $n[$key] !== null) ? number_format($n[$key], 0) : '';
        ?>
        <td><?= $val ?></td>
        <?php endfor; ?>
        <td><?= ($n && $n['rata_sumatif'] !== null) ? number_format($n['rata_sumatif'], 1) : '' ?></td>
        <td><?= ($n && $n['sts']          !== null) ? number_format($n['sts'],          0) : '' ?></td>
        <td><?= ($n && $n['sas']          !== null) ? number_format($n['sas'],          0) : '' ?></td>
        <td style="font-weight:bold;"><?= ($n && $n['nilai_akhir'] !== null) ? number_format($n['nilai_akhir'], 0) : '' ?></td>
      </tr>
      <?php endforeach; ?>
      <!-- Baris kosong tambahan jika siswa sedikit -->
      <?php for ($extra = count($siswas); $extra < 5; $extra++): ?>
      <tr>
        <td><?= $extra + 1 ?></td>
        <td></td>
        <?php for ($j = 0; $j < $jsSumatif + 4; $j++): ?><td></td><?php endfor; ?>
      </tr>
      <?php endfor; ?>
      <?php endif; ?>
    </tbody>
  </table>

  <!-- TTD -->
  <table class="ttd-table">
    <tr>
      <td style="width:50%;">Mengetahui,<br>Kepala Sekolah</td>
      <td style="width:50%;padding-left:60px;"><?= htmlspecialchars($tempatTgl) ?><br>Guru Mata Pelajaran</td>
    </tr>
    <tr>
      <td class="ttd-space"></td>
      <td class="ttd-space"></td>
    </tr>
    <tr>
      <td>
        <?php
  $_nm = htmlspecialchars($pengaturan['kepala_sekolah'] ?? '');
  $_pos = strpos($_nm, ',');
  if ($_pos !== false) {
    echo '<strong style="text-transform:uppercase;">' . substr($_nm, 0, $_pos) . '</strong><strong>' . substr($_nm, $_pos) . '</strong>';
  } else {
    echo '<strong style="text-transform:uppercase;">' . $_nm . '</strong>';
  }
?><br>
        NIP. <?= htmlspecialchars($pengaturan['nip_kepsek'] ?? '') ?>
      </td>
      <td style="padding-left:60px;">
        <?php if ($guruMapel): ?>
        <?php
  $_nm = htmlspecialchars($guruMapel['nama']);
  $_pos = strpos($_nm, ',');
  if ($_pos !== false) {
    echo '<strong style="text-transform:uppercase;">' . substr($_nm, 0, $_pos) . '</strong><strong>' . substr($_nm, $_pos) . '</strong>';
  } else {
    echo '<strong style="text-transform:uppercase;">' . $_nm . '</strong>';
  }
?><br>
        NIP. <?= htmlspecialchars($guruMapel['nip'] ?? '') ?>
        <?php else: ?>
        <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong><br>
        NIP.
        <?php endif; ?>
      </td>
    </tr>
  </table>

</div>
<script>window.onload = function(){ window.print(); }</script>
</body>
</html>
<?php
    exit;
endif;

// ---- Tampilan normal (dengan sidebar SisPen Snesaka) ----
$pageTitle  = 'Cetak Daftar Nilai';
$activeMenu = 'cetak';
include 'templates/header.php';
?>

<div style="display:grid;grid-template-columns:280px 1fr;gap:20px;align-items:start;">

  <!-- Panel kiri: filter -->
  <div class="card" style="position:sticky;top:80px;">
    <div class="card-header">
      <span class="card-title"><i class="fas fa-filter" style="color:var(--primary);margin-right:8px;"></i>Filter</span>
    </div>
    <div class="card-body">
      <form method="GET">
        <div class="form-group">
          <label class="form-label">Kelas</label>
          <select name="kelas_id" class="form-control" onchange="this.form.submit()">
            <option value="">-- Pilih Kelas --</option>
            <?php foreach ($kelasList as $k): ?>
            <option value="<?= $k['id'] ?>" <?= $kelasId==$k['id']?'selected':'' ?>>
              <?= htmlspecialchars($k['nama_kelas']) ?>
            </option>
            <?php endforeach; ?>
          </select>
        </div>
        <?php if ($kelasId): ?>
        <div class="form-group">
          <label class="form-label">Mata Pelajaran</label>
          <select name="mapel_id" class="form-control" onchange="this.form.submit()">
            <option value="">-- Pilih --</option>
            <?php foreach ($mapelList as $m): ?>
            <option value="<?= $m['id'] ?>" <?= $mapelId==$m['id']?'selected':'' ?>>
              <?= htmlspecialchars($m['nama_mapel']) ?>
            </option>
            <?php endforeach; ?>
          </select>
        </div>
        <input type="hidden" name="kelas_id" value="<?= $kelasId ?>">
        <?php endif; ?>

        <!-- Tempat & Tanggal Cetak -->
        <div style="margin-top:14px;border-top:1px solid var(--border);padding-top:14px;">
          <div class="form-group">
            <label class="form-label">
              <i class="fas fa-map-marker-alt" style="color:var(--primary);margin-right:5px;"></i>
              Tempat
            </label>
            <input type="text" name="tempat_cetak"
                   class="form-control"
                   value="<?= htmlspecialchars($tempatCetak) ?>"
                   placeholder="Contoh: Kalitengah">
          </div>
          <div class="form-group">
            <label class="form-label">
              <i class="fas fa-calendar-alt" style="color:var(--primary);margin-right:5px;"></i>
              Tanggal Cetak
            </label>
            <input type="date" name="tgl_cetak"
                   class="form-control"
                   value="<?= htmlspecialchars($tglCetak) ?>">
            <small style="font-size:11px;color:var(--text-muted);margin-top:4px;display:block;">
              <i class="fas fa-info-circle"></i>
              Tampil di TTD hasil cetak
            </small>
          </div>
          <button type="submit" class="btn btn-sm" style="width:100%;background:linear-gradient(135deg,#1e4d8c,#2563eb);color:#fff;border:none;font-weight:700;">
            <i class="fas fa-sync-alt"></i> Terapkan
          </button>
        </div>
      </form>

  </div>
  </div>

  <!-- Panel kanan: preview -->
  <div class="card">
    <div class="card-header">
      <span class="card-title">
        <i class="fas fa-eye" style="color:var(--primary);margin-right:8px;"></i>
        Preview Daftar Nilai
      </span>
      <?php if ($kelasId && $mapelId): ?>
      <a href="?kelas_id=<?= $kelasId ?>&mapel_id=<?= $mapelId ?>&cetak=1&tgl_cetak=<?= urlencode($tglCetak) ?>&tempat_cetak=<?= urlencode($tempatCetak) ?>"
         target="_blank" class="btn btn-accent btn-sm">
        <i class="fas fa-print"></i> Print Daftar Nilai
      </a>
      <?php endif; ?>
    </div>

    <?php if ($kelasId && $mapelId && $selectedKelas && $selectedMapel): ?>
    <!-- Preview dalam kartu -->
    <div style="padding:24px;background:#f8f9fa;">
      <div style="background:#fff;border:1px solid #ccc;border-radius:4px;padding:20px 24px;max-width:720px;margin:0 auto;font-family:Arial,sans-serif;font-size:11pt;">

        <div style="text-align:center;font-weight:bold;font-size:13pt;text-transform:uppercase;margin-bottom:2px;">DAFTAR NILAI SISWA</div>
        <div style="text-align:center;font-weight:bold;font-size:11pt;margin-bottom:14px;"><?= htmlspecialchars($pengaturan['nama_sekolah'] ?? 'Nama Sekolah') ?></div>

        <table style="width:100%;border-collapse:collapse;margin-bottom:12px;font-size:10.5pt;">
          <tr>
            <td style="width:120px;padding:2px 4px;">Kelas</td>
            <td style="width:8px;">:</td>
            <td style="padding:2px 4px;"><strong><?= htmlspecialchars($selectedKelas['nama_kelas']) ?></strong></td>
            <td style="width:90px;padding:2px 4px;">Semester</td>
            <td style="width:8px;">:</td>
            <td style="padding:2px 4px;"><strong><?= $semesterLabel ?></strong></td>
          </tr>
          <tr>
            <td style="padding:2px 4px;">Mata Pelajaran</td>
            <td>:</td>
            <td style="padding:2px 4px;"><strong><?= htmlspecialchars($selectedMapel['nama_mapel']) ?></strong></td>
            <td style="padding:2px 4px;text-align:right;">T.P.</td>
            <td>:</td>
            <td style="padding:2px 4px;"><strong><?= htmlspecialchars($ta) ?></strong></td>
          </tr>
        </table>

        <table style="width:100%;border-collapse:collapse;font-size:10pt;">
          <thead>
            <tr style="border:1px solid #000;">
              <th rowspan="2" style="border:1px solid #000;padding:5px 6px;text-align:center;width:28px;">NO</th>
              <th rowspan="2" style="border:1px solid #000;padding:5px 8px;text-align:center;">NAMA SISWA</th>
              <th colspan="<?= $jsSumatif ?>" style="border:1px solid #000;padding:5px;text-align:center;">Sumatif ke</th>
              <th rowspan="2" style="border:1px solid #000;padding:5px 4px;text-align:center;width:46px;">Rata2<br>Sumatif</th>
              <th rowspan="2" style="border:1px solid #000;padding:5px 4px;text-align:center;width:40px;">STS</th>
              <th rowspan="2" style="border:1px solid #000;padding:5px 4px;text-align:center;width:40px;">SAS</th>
              <th rowspan="2" style="border:1px solid #000;padding:5px 4px;text-align:center;width:52px;font-weight:bold;">NILAI<br>RAPOR<br>(NR)</th>
            </tr>
            <tr>
              <?php for ($i=1; $i<=$jsSumatif; $i++): ?>
              <th style="border:1px solid #000;padding:4px;text-align:center;width:38px;"><?= $i ?></th>
              <?php endfor; ?>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($siswas as $i => $s):
              $n = $nilais[$s['id']] ?? null;
            ?>
            <tr>
              <td style="border:1px solid #000;padding:4px 6px;text-align:center;"><?= $i+1 ?></td>
              <td style="border:1px solid #000;padding:4px 8px;text-align:left;"><?= htmlspecialchars($s['nama']) ?></td>
              <?php for ($j=1; $j<=$jsSumatif; $j++):
                $key = 'sumatif_'.$j;
                $val = ($n && $n[$key] !== null) ? number_format($n[$key],0) : '';
              ?>
              <td style="border:1px solid #000;padding:4px;text-align:center;"><?= $val ?></td>
              <?php endfor; ?>
              <td style="border:1px solid #000;padding:4px;text-align:center;"><?= ($n && $n['rata_sumatif']!==null) ? number_format($n['rata_sumatif'],1) : '' ?></td>
              <td style="border:1px solid #000;padding:4px;text-align:center;"><?= ($n && $n['sts']!==null) ? number_format($n['sts'],0) : '' ?></td>
              <td style="border:1px solid #000;padding:4px;text-align:center;"><?= ($n && $n['sas']!==null) ? number_format($n['sas'],0) : '' ?></td>
              <td style="border:1px solid #000;padding:4px;text-align:center;font-weight:bold;"><?= ($n && $n['nilai_akhir']!==null) ? number_format($n['nilai_akhir'],0) : '' ?></td>
            </tr>
            <?php endforeach; ?>
            <?php if (empty($siswas)): ?>
            <tr><td colspan="<?= 3+$jsSumatif+3 ?>" style="border:1px solid #000;padding:20px;text-align:center;color:#999;">Belum ada data siswa</td></tr>
            <?php endif; ?>
          </tbody>
        </table>

        <!-- TTD preview -->
        <table style="width:100%;border-collapse:collapse;margin-top:24px;font-size:10.5pt;">
          <tr>
            <td style="width:50%;vertical-align:top;">Mengetahui,<br>Kepala Sekolah</td>
            <td style="width:50%;vertical-align:top;padding-left:60px;"><?= htmlspecialchars($tempatTgl) ?><br>Guru Mata Pelajaran</td>
          </tr>
          <tr><td style="height:50px;"></td><td></td></tr>
          <tr>
            <td>
              <?php
  $_nm = htmlspecialchars($pengaturan['kepala_sekolah'] ?? '________________________');
  $_pos = strpos($_nm, ',');
  if ($_pos !== false) {
    echo '<strong style="text-transform:uppercase;">' . substr($_nm, 0, $_pos) . '</strong><strong>' . substr($_nm, $_pos) . '</strong>';
  } else {
    echo '<strong style="text-transform:uppercase;">' . $_nm . '</strong>';
  }
?><br>
              NIP. <?= htmlspecialchars($pengaturan['nip_kepsek'] ?? '') ?>
            </td>
            <td style="padding-left:60px;">
              <?php if ($guruMapel): ?>
              <?php
  $_nm = htmlspecialchars($guruMapel['nama']);
  $_pos = strpos($_nm, ',');
  if ($_pos !== false) {
    echo '<strong style="text-transform:uppercase;">' . substr($_nm, 0, $_pos) . '</strong><strong>' . substr($_nm, $_pos) . '</strong>';
  } else {
    echo '<strong style="text-transform:uppercase;">' . $_nm . '</strong>';
  }
?><br>
              NIP. <?= htmlspecialchars($guruMapel['nip'] ?? '') ?>
              <?php else: ?>
              <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong><br>
              NIP.
              <?php endif; ?>
            </td>
          </tr>
        </table>

      </div>
    </div>

    <?php else: ?>
    <div style="padding:60px;text-align:center;color:var(--text-muted);">
      <i class="fas fa-file-alt" style="font-size:48px;display:block;margin-bottom:16px;opacity:.25;"></i>
      <p>Pilih kelas dan mata pelajaran untuk melihat preview.</p>
    </div>
    <?php endif; ?>
  </div>

</div>

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