<?php include __DIR__ . '/../../views/layouts/header.php'; ?>
<div class="wrapper">
<?php include __DIR__ . '/../../views/layouts/sidebar.php'; ?>
<div class="main-content">
<?php include __DIR__ . '/../../views/layouts/topbar.php'; ?>

<div class="page-content">
  <div class="page-header d-flex justify-content-between align-items-start flex-wrap gap-2 no-print">
    <div>
      <h1><i class="bi bi-printer me-2 text-primary"></i>Cetak Rapor</h1>
    </div>
    <?php if ($siswa && !empty($nilai)): ?>
    <button class="btn btn-primary btn-sm" data-print>
      <i class="bi bi-printer me-1"></i> Cetak / Print
    </button>
    <?php endif; ?>
  </div>

  <!-- Filter -->
  <div class="search-bar no-print">
    <form method="GET" class="row g-2 align-items-end">
      <input type="hidden" name="page" value="nilai_rapor">
      <div class="col-12 col-md-3">
        <label class="form-label mb-1">Kelas</label>
        <select class="form-select" name="kelas_id" onchange="this.form.submit()">
          <option value="">-- Pilih Kelas --</option>
          <?php foreach ($kelasList as $k): ?>
          <option value="<?= $k['id'] ?>" <?= $kelasId == $k['id'] ? 'selected' : '' ?>>
            <?= e($k['nama_kelas']) ?>
          </option>
          <?php endforeach; ?>
        </select>
      </div>
      <?php if ($kelasId): ?>
      <div class="col-12 col-md-3">
        <label class="form-label mb-1">Siswa</label>
        <select class="form-select" name="siswa_id" onchange="this.form.submit()">
          <option value="">-- Pilih Siswa --</option>
          <?php foreach ($siswaList as $s): ?>
          <option value="<?= $s['id'] ?>" <?= $siswaId == $s['id'] ? 'selected' : '' ?>>
            <?= e($s['nama']) ?>
          </option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-6 col-md-2">
        <label class="form-label mb-1">Semester</label>
        <select class="form-select" name="semester_no" onchange="this.form.submit()">
          <?php foreach ($semesterList as $no => $label): ?>
          <option value="<?= $no ?>" <?= $semesterNo == $no ? 'selected' : '' ?>>
            Semester <?= $no ?>
          </option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-6 col-md-2">
        <label class="form-label mb-1">Tahun Ajaran</label>
        <input type="text" class="form-control" name="tahun_ajaran"
               value="<?= e($tahunAjaran) ?>">
      </div>
      <input type="hidden" name="kelas_id" value="<?= $kelasId ?>">
      <?php endif; ?>
    </form>
  </div>

  <?php if ($siswa && !empty($nilai)): ?>
  <!-- Area Cetak -->
  <div class="card" id="raporArea">
    <div class="card-body p-4">

      <!-- Header Sekolah -->
      <div class="text-center border-bottom pb-3 mb-4">
        <div class="d-flex align-items-center justify-content-center gap-3 mb-2">
          <?php if (!empty($sekolah['logo'])): ?>
          <img src="<?= BASE_URL ?>/uploads/<?= e($sekolah['logo']) ?>"
               style="height:70px;object-fit:contain;">
          <?php endif; ?>
          <div>
            <h4 class="fw-700 mb-0"><?= e($sekolah['nama_sekolah'] ?? 'Nama Sekolah') ?></h4>
            <div class="text-muted"><?= e($sekolah['alamat'] ?? '') ?></div>
            <?php if (!empty($sekolah['telepon'])): ?>
            <div class="text-muted" style="font-size:.85rem;">
              Telp: <?= e($sekolah['telepon']) ?>
            </div>
            <?php endif; ?>
          </div>
        </div>
        <h5 class="fw-700 mb-0 mt-2">LAPORAN HASIL BELAJAR SISWA (RAPOR)</h5>
        <div class="text-muted">
          <?= e($semesterList[$semesterNo] ?? "Semester $semesterNo") ?>
          &nbsp;&mdash;&nbsp; Tahun Pelajaran <?= e($tahunAjaran) ?>
        </div>
      </div>

      <!-- Info Siswa -->
      <div class="row mb-4">
        <div class="col-md-6">
          <table class="table table-sm table-borderless mb-0">
            <tr>
              <td style="width:130px;color:#64748b;">Nama Siswa</td>
              <td>: <strong><?= e($siswa['nama']) ?></strong></td>
            </tr>
            <tr>
              <td style="color:#64748b;">NIS / NISN</td>
              <td>: <?= e($siswa['nis'] ?? '-') ?> / <?= e($siswa['nisn'] ?? '-') ?></td>
            </tr>
            <tr>
              <td style="color:#64748b;">Kelas</td>
              <td>: <?= e($siswa['nama_kelas'] ?? '-') ?></td>
            </tr>
          </table>
        </div>
        <div class="col-md-6">
          <table class="table table-sm table-borderless mb-0">
            <tr>
              <td style="width:130px;color:#64748b;">Semester</td>
              <td>: <?= e($semesterList[$semesterNo] ?? "Semester $semesterNo") ?></td>
            </tr>
            <tr>
              <td style="color:#64748b;">Tahun Pelajaran</td>
              <td>: <?= e($tahunAjaran) ?></td>
            </tr>
            <tr>
              <td style="color:#64748b;">Wali Kelas</td>
              <td>: <?= e($kelas['nama_wali_kelas'] ?? '-') ?></td>
            </tr>
          </table>
        </div>
      </div>

      <!-- Tabel Nilai Rapor -->
      <div class="table-responsive">
        <table class="table table-bordered" style="font-size:.9rem;">
          <thead class="table-light">
            <tr>
              <th style="width:40px">No</th>
              <th>Mata Pelajaran</th>
              <th class="text-center" style="width:120px">Nilai Rapor</th>
              <th class="text-center" style="width:80px">KKM</th>
              <th class="text-center" style="width:80px">Predikat</th>
              <th class="text-center" style="width:100px">Keterangan</th>
            </tr>
          </thead>
          <tbody>
            <?php
            $totalNilai = 0;
            $jumlahMapel = count($nilai);
            foreach ($nilai as $i => $n):
              $lulus = (float)$n['nilai_rapor'] >= (float)$n['kkm'];
            ?>
            <tr>
              <td class="text-center"><?= $i + 1 ?></td>
              <td><?= e($n['nama_mapel']) ?></td>
              <td class="text-center fw-700 <?= $lulus ? 'text-success' : 'text-danger' ?>">
                <?= $n['nilai_rapor'] ?>
              </td>
              <td class="text-center text-muted"><?= $n['kkm'] ?></td>
              <td class="text-center">
                <span class="badge <?= $lulus ? 'bg-success' : 'bg-danger' ?>">
                  <?= e($n['predikat'] ?? '-') ?>
                </span>
              </td>
              <td class="text-center">
                <?= $lulus
                  ? '<span class="text-success">Tuntas</span>'
                  : '<span class="text-danger">Belum Tuntas</span>' ?>
              </td>
            </tr>
            <?php
              $totalNilai += $n['nilai_rapor'];
            endforeach;
            $rata = $jumlahMapel > 0 ? round($totalNilai / $jumlahMapel, 2) : 0;
            ?>
          </tbody>
          <tfoot>
            <tr class="table-light fw-700">
              <td colspan="2" class="text-end">Rata-rata Nilai</td>
              <td class="text-center"><?= $rata ?></td>
              <td colspan="3"></td>
            </tr>
          </tfoot>
        </table>
      </div>

      <!-- Tanda Tangan -->
      <div class="row mt-5 text-center">
        <div class="col-4">
          <div class="mb-1">Orang Tua / Wali</div>
          <div style="height:65px;"></div>
          <div>( _________________________ )</div>
        </div>
        <div class="col-4">
          <div class="mb-1">Wali Kelas</div>
          <div style="height:65px;"></div>
          <div>( <?= e($kelas['nama_wali_kelas'] ?? '_______________') ?> )</div>
        </div>
        <div class="col-4">
          <div class="mb-1">Kepala Sekolah</div>
          <div style="height:65px;"></div>
          <div>( <?= e($sekolah['kepala_sekolah'] ?? '_______________') ?> )</div>
          <?php if (!empty($sekolah['nip_kepala'])): ?>
          <div style="font-size:.8rem;">NIP. <?= e($sekolah['nip_kepala']) ?></div>
          <?php endif; ?>
        </div>
      </div>

    </div>
  </div>

  <?php else: ?>
  <div class="empty-state card py-5">
    <div class="empty-icon">📋</div>
    <p>Pilih <strong>kelas</strong> dan <strong>siswa</strong> untuk melihat rapor</p>
  </div>
  <?php endif; ?>
</div>

<?php include __DIR__ . '/../../views/layouts/footer.php'; ?>
</div>
</div>
