<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <title>Rapor - <?= htmlspecialchars($siswa['nama']) ?></title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap">
  <style>
    * { box-sizing: border-box; margin: 0; padding: 0; }
    body { font-family: 'Plus Jakarta Sans', Arial, sans-serif; font-size: 11pt; color: #000; background: #fff; }
    .page { width: 210mm; min-height: 297mm; margin: 0 auto; padding: 15mm 20mm; }

    /* HEADER */
    .rapor-header { display: flex; align-items: center; gap: 14px; border-bottom: 3px double #000; padding-bottom: 10px; margin-bottom: 14px; }
    .rapor-header img { width: 60px; height: 60px; object-fit: contain; }
    .rapor-header .no-logo { width: 60px; height: 60px; border: 2px solid #000; display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 18pt; }
    .rapor-header-text { flex: 1; text-align: center; }
    .rapor-header-text h1 { font-size: 14pt; font-weight: 800; text-transform: uppercase; }
    .rapor-header-text p { font-size: 10pt; margin-top: 2px; }
    .rapor-title { text-align: center; font-size: 13pt; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; margin: 10px 0; text-decoration: underline; }

    /* BIODATA */
    .bio-table { width: 100%; margin-bottom: 14px; }
    .bio-table td { padding: 2px 4px; font-size: 10.5pt; vertical-align: top; }
    .bio-table td:first-child { width: 140px; font-weight: 500; }
    .bio-table td:nth-child(2) { width: 14px; }

    /* NILAI TABLE */
    .nilai-table { width: 100%; border-collapse: collapse; margin-bottom: 14px; font-size: 10pt; }
    .nilai-table th, .nilai-table td { border: 1px solid #000; padding: 5px 7px; vertical-align: middle; }
    .nilai-table th { background: #e8e8e8; font-weight: 700; text-align: center; }
    .nilai-table .center { text-align: center; }
    .nilai-table .highlight { background: #f5f5f5; font-weight: 600; }
    .nilai-table .kelompok-header td { background: #ddd; font-weight: 700; font-size: 9.5pt; }

    /* ABSENSI */
    .absensi-table { width: 100%; border-collapse: collapse; font-size: 10pt; margin-bottom: 14px; }
    .absensi-table th, .absensi-table td { border: 1px solid #000; padding: 5px 8px; text-align: center; }
    .absensi-table th { background: #e8e8e8; }

    /* TTD */
    .ttd-section { display: flex; justify-content: space-between; margin-top: 20px; }
    .ttd-box { text-align: center; width: 200px; }
    .ttd-box .ttd-line { margin: 50px 0 6px; border-bottom: 1px solid #000; }

    .badge-predikat { font-weight: 700; }
    .tuntas { color: green; }
    .belum { color: red; }

    @media print {
      .page { margin: 0; padding: 10mm 15mm; }
      body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
      @page { size: A4; margin: 0; }
    }
  </style>
</head>
<body>
<div class="page">

  <!-- HEADER -->
  <div class="rapor-header">
    <?php if (!empty($pengaturan['logo'])): ?>
    <img src="<?= BASE_URL ?>/uploads/<?= htmlspecialchars($pengaturan['logo']) ?>" alt="Logo">
    <?php else: ?>
    <div class="no-logo">S</div>
    <?php endif; ?>
    <div class="rapor-header-text">
      <h1><?= htmlspecialchars($pengaturan['nama_sekolah'] ?? 'SMP NEGERI') ?></h1>
      <p><?= htmlspecialchars($pengaturan['alamat'] ?? '') ?></p>
      <p>NSS: <?= $pengaturan['nss'] ?? '-' ?> &bull; NPSN: <?= $pengaturan['npsn'] ?? '-' ?></p>
    </div>
  </div>

  <div class="rapor-title">LAPORAN HASIL BELAJAR PESERTA DIDIK (RAPOR)</div>
  <p style="text-align:center;font-size:10pt;margin-bottom:14px;">Semester <?= $semester ?> Tahun Pelajaran <?= $tahunAjaran ?></p>

  <!-- BIODATA -->
  <table class="bio-table">
    <tr>
      <td>Nama Peserta Didik</td><td>:</td>
      <td><strong><?= htmlspecialchars($siswa['nama']) ?></strong></td>
      <td width="40"></td>
      <td width="120">Kelas</td><td width="14">:</td>
      <td><strong><?= htmlspecialchars($siswa['nama_kelas']) ?></strong></td>
    </tr>
    <tr>
      <td>NIS / NISN</td><td>:</td>
      <td><?= htmlspecialchars($siswa['nis']) ?> / <?= htmlspecialchars($siswa['nisn'] ?? '-') ?></td>
      <td></td>
      <td>Fase</td><td>:</td>
      <td>D</td>
    </tr>
    <tr>
      <td>Tempat, Tgl Lahir</td><td>:</td>
      <td><?= htmlspecialchars($siswa['tempat_lahir'] ?? '-') ?>, <?= formatTanggal($siswa['tanggal_lahir']) ?></td>
      <td></td>
      <td>Wali Kelas</td><td>:</td>
      <td><?= htmlspecialchars($siswa['wali_kelas'] ?? '-') ?></td>
    </tr>
    <tr>
      <td>Nama Orang Tua</td><td>:</td>
      <td><?= htmlspecialchars($siswa['nama_ayah'] ?? '-') ?> / <?= htmlspecialchars($siswa['nama_ibu'] ?? '-') ?></td>
      <td></td>
      <td>Alamat</td><td>:</td>
      <td><?= htmlspecialchars($siswa['alamat'] ?? '-') ?></td>
    </tr>
  </table>

  <!-- NILAI TABLE -->
  <table class="nilai-table">
    <thead>
      <tr>
        <th width="30">No</th>
        <th>Mata Pelajaran</th>
        <th width="60">Nilai Akhir</th>
        <th width="70">Predikat</th>
        <th width="60">KKM</th>
        <th width="90">Ket.</th>
        <th>Deskripsi Capaian</th>
      </tr>
    </thead>
    <tbody>
      <?php
      $kelompokAktif = '';
      $no = 0;
      $totalNilai = 0;
      $countNilai = 0;
      ?>
      <?php foreach ($nilais as $n): ?>
      <?php if ($n['kelompok'] !== $kelompokAktif): ?>
      <tr class="kelompok-header">
        <td colspan="7">
          Kelompok <?= $n['kelompok'] ?> -
          <?= ['A' => 'Mata Pelajaran Wajib Nasional', 'B' => 'Mata Pelajaran Wajib Daerah', 'C' => 'Muatan Lokal'][$n['kelompok']] ?>
        </td>
      </tr>
      <?php $kelompokAktif = $n['kelompok']; ?>
      <?php endif; ?>
      <tr>
        <td class="center"><?= ++$no ?></td>
        <td><?= htmlspecialchars($n['nama_mapel']) ?></td>
        <td class="center highlight"><?= $n['nilai_akhir'] !== null ? number_format($n['nilai_akhir'], 1) : '-' ?></td>
        <td class="center"><strong><?= $n['predikat'] ?? '-' ?></strong> (<?= getKeteranganPredikat($n['predikat'] ?? '') ?>)</td>
        <td class="center"><?= $n['kkm'] ?></td>
        <td class="center <?= ($n['nilai_akhir'] ?? 0) >= $n['kkm'] ? 'tuntas' : 'belum' ?>">
          <?= ($n['nilai_akhir'] ?? 0) >= $n['kkm'] ? 'Tuntas' : 'Belum Tuntas' ?>
        </td>
        <td style="font-size:9.5pt;"><?= htmlspecialchars($n['catatan'] ?? '') ?></td>
      </tr>
      <?php $totalNilai += $n['nilai_akhir'] ?? 0; $countNilai++; ?>
      <?php endforeach; ?>
      <tr style="background:#f0f0f0;font-weight:700;">
        <td colspan="2" style="text-align:right;">Jumlah Nilai</td>
        <td class="center"><?= round($totalNilai, 1) ?></td>
        <td colspan="4"></td>
      </tr>
      <tr style="background:#f0f0f0;font-weight:700;">
        <td colspan="2" style="text-align:right;">Rata-rata Nilai</td>
        <td class="center"><?= $countNilai > 0 ? round($totalNilai / $countNilai, 2) : '-' ?></td>
        <td colspan="4"></td>
      </tr>
    </tbody>
  </table>

  <!-- ABSENSI -->
  <p style="font-weight:700;margin-bottom:6px;">Ketidakhadiran:</p>
  <table class="absensi-table">
    <thead><tr><th>Sakit (hari)</th><th>Izin (hari)</th><th>Tanpa Keterangan (hari)</th></tr></thead>
    <tbody>
      <tr>
        <td><?= $absensi['sakit'] ?? 0 ?></td>
        <td><?= $absensi['izin'] ?? 0 ?></td>
        <td><?= $absensi['tanpa_keterangan'] ?? 0 ?></td>
      </tr>
    </tbody>
  </table>

  <!-- TTD -->
  <div class="ttd-section">
    <div class="ttd-box">
      <p>Mengetahui,</p>
      <p>Orang Tua / Wali</p>
      <div class="ttd-line"></div>
      <p>(...................................)</p>
    </div>
    <div class="ttd-box" style="text-align:center;">
      <p>Wali Kelas,</p>
      <div class="ttd-line"></div>
      <p style="font-weight:600;"><?= htmlspecialchars($siswa['wali_kelas'] ?? '.....................') ?></p>
    </div>
    <div class="ttd-box" style="text-align:center;">
      <p>Kepala Sekolah,</p>
      <div class="ttd-line"></div>
      <p style="font-weight:600;"><?= htmlspecialchars($pengaturan['kepala_sekolah'] ?? '.....................') ?></p>
      <p style="font-size:9pt;">NIP. <?= $pengaturan['nip_kepsek'] ?? '.....................' ?></p>
    </div>
  </div>

</div>
<script>window.print();</script>
</body>
</html>
