<?php
require_once '../config/config.php';
require_once '../config/database.php';
require_once '../core/helpers.php';
requireLogin();

$tahun = sanitize($_GET['tahun'] ?? date('Y'));
$jenis = sanitize($_GET['jenis'] ?? '');

$where  = ["YEAR(tanggal_mulai) = ?"];
$params = [$tahun];
if ($jenis) { $where[] = 'jenis_kegiatan = ?'; $params[] = $jenis; }

$data   = Database::fetchAll("SELECT * FROM pengembangan_diri WHERE ".implode(' AND ',$where)." ORDER BY tanggal_mulai", $params);
$profil = Database::fetchOne("SELECT * FROM profil_pendamping LIMIT 1");
$totalJP = array_sum(array_column($data,'jumlah_jp'));
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Laporan Pengembangan Diri <?= $tahun ?></title>
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700&display=swap" rel="stylesheet">
<style>
  *{box-sizing:border-box;margin:0;padding:0}
  body{font-family:'Plus Jakarta Sans',sans-serif;font-size:10pt;color:#1a1a2e;background:#fff}
  @page{size:A4 landscape;margin:15mm 15mm 15mm 20mm}
  @media print{.no-print{display:none!important}}
  .kop{display:flex;align-items:center;gap:12px;border-bottom:3px solid #1a3a6b;padding-bottom:8px;margin-bottom:10px}
  .kop-logo{width:52px;height:52px;background:#1a3a6b;border-radius:10px;display:flex;align-items:center;justify-content:center;flex-shrink:0}
  .kop-logo svg{width:28px;height:28px;fill:#fff}
  .kop-info h1{font-size:12.5pt;font-weight:800;color:#1a3a6b}
  .kop-info p{font-size:8pt;color:#555;margin-top:2px}
  .title-block{text-align:center;margin:10px 0 12px}
  .title-block h2{font-size:12pt;font-weight:800;text-transform:uppercase;color:#1a3a6b}
  .title-block .sub{font-size:10pt;color:#444;margin-top:4px}
  .title-block .garis{width:80px;height:3px;background:#c0392b;margin:6px auto;border-radius:2px}
  .info-row{display:flex;gap:40px;margin-bottom:12px;font-size:9.5pt;border:1px solid #d0d8e8;padding:7px 12px;border-radius:6px}
  .info-row span{color:#555}.info-row strong{color:#1a3a6b}
  table{width:100%;border-collapse:collapse;font-size:9pt}
  thead th{background:#1a3a6b;color:#fff;padding:6px 7px;font-weight:600;text-align:left}
  tbody td{padding:5px 7px;border-bottom:1px solid #e8edf5;vertical-align:top}
  tbody tr:nth-child(even) td{background:#f5f8ff}
  .badge{display:inline-block;padding:1px 7px;border-radius:20px;font-size:8pt;font-weight:600;background:#e0f2fe;color:#0369a1}
  .total-row td{font-weight:700;background:#f5f8ff!important;border-top:2px solid #1a3a6b}
  .ttd{display:flex;justify-content:flex-end;margin-top:22px}
  .ttd-box{text-align:center;font-size:10pt}
  .ttd-box .space{height:52px}
  .ttd-box .name{font-weight:700;border-top:1px solid #333;padding-top:4px;min-width:160px;display:inline-block}
  .ctrl-bar{position:fixed;bottom:0;left:0;right:0;background:#1a3a6b;padding:10px 20px;display:flex;justify-content:space-between;align-items:center;z-index:999}
  .ctrl-bar span{color:rgba(255,255,255,.75);font-size:.85rem}
  .ctrl-bar .btns{display:flex;gap:10px}
  .btn-ctrl{padding:8px 20px;border-radius:6px;border:none;font-family:inherit;font-weight:700;font-size:.85rem;cursor:pointer}
  .btn-print{background:#c0392b;color:#fff}
  .btn-back{background:rgba(255,255,255,.15);color:#fff;border:1px solid rgba(255,255,255,.3)}
  .doc-footer{text-align:center;font-size:8pt;color:#999;border-top:1px solid #e0e0e0;padding-top:6px;margin-top:14px}
</style>
</head>
<body>

<div class="kop">
  <div class="kop-logo"><svg viewBox="0 0 24 24"><path d="M12 3L1 9l11 6 9-4.91V17h2V9L12 3zM5 13.18v4L12 21l7-3.82v-4L12 17l-7-3.82z"/></svg></div>
  <div class="kop-info">
    <h1><?= APP_INSTANSI ?></h1>
    <p>Jl. Ki Ageng Gribig No. 45, Malang, Jawa Timur &nbsp;|&nbsp; Telp. (0341) 123456</p>
  </div>
</div>

<div class="title-block">
  <h2>Laporan Pengembangan Diri</h2>
  <div class="sub">Tahun <?= e($tahun) ?><?= $jenis ? ' &mdash; '.$jenis : '' ?></div>
  <div class="garis"></div>
</div>

<div class="info-row">
  <div><span>Nama: </span><strong><?= e($profil['nama_lengkap'] ?? '—') ?></strong></div>
  <div><span>NIP: </span><strong><?= e($profil['nip'] ?? '—') ?></strong></div>
  <div><span>Jabatan: </span><strong><?= e($profil['jabatan'] ?? '—') ?></strong></div>
  <div><span>Total JP: </span><strong><?= $totalJP ?> JP</strong></div>
  <div><span>Jumlah Kegiatan: </span><strong><?= count($data) ?></strong></div>
</div>

<table>
  <thead>
    <tr>
      <th width="28">No</th>
      <th width="70">Jenis</th>
      <th>Judul Kegiatan</th>
      <th>Penyelenggara</th>
      <th>Tempat</th>
      <th width="90">Pelaksanaan</th>
      <th width="35" style="text-align:center">JP</th>
      <th width="100">No. Sertifikat</th>
      <th width="80">Tgl Sertifikat</th>
    </tr>
  </thead>
  <tbody>
    <?php if(empty($data)): ?>
    <tr><td colspan="9" style="text-align:center;padding:20px;color:#999">Tidak ada data</td></tr>
    <?php else: ?>
    <?php $no=1; foreach($data as $row): ?>
    <tr>
      <td><?= $no++ ?></td>
      <td><span class="badge"><?= e($row['jenis_kegiatan']) ?></span></td>
      <td><?= e($row['judul_kegiatan']) ?></td>
      <td><?= e($row['penyelenggara']) ?></td>
      <td><?= e($row['tempat'] ?? '—') ?></td>
      <td style="white-space:nowrap;font-size:8.5pt"><?= formatTanggal($row['tanggal_mulai'],'d M Y') ?><br><?= formatTanggal($row['tanggal_selesai'],'d M Y') ?></td>
      <td style="text-align:center;font-weight:700"><?= $row['jumlah_jp'] ?? '—' ?></td>
      <td style="font-size:8.5pt"><?= $row['nomor_sertifikat'] ? e($row['nomor_sertifikat']) : '—' ?></td>
      <td style="font-size:8.5pt;white-space:nowrap"><?= $row['tanggal_sertifikat'] ? formatTanggal($row['tanggal_sertifikat'],'d M Y') : '—' ?></td>
    </tr>
    <?php endforeach; ?>
    <tr class="total-row">
      <td colspan="6" style="text-align:right">Total Jam Pelajaran:</td>
      <td style="text-align:center"><?= $totalJP ?></td>
      <td colspan="2"></td>
    </tr>
    <?php endif; ?>
  </tbody>
</table>

<div class="ttd">
  <div class="ttd-box">
    <p>Malang, <?= formatTanggal(date('Y-m-d'),'d F Y') ?></p>
    <p>Pendamping Sekolah,</p>
    <div class="space"></div>
    <span class="name"><?= e($profil['nama_lengkap'] ?? 'Nama Pendamping') ?></span>
    <p style="font-size:8.5pt;color:#555;margin-top:2px">NIP. <?= e($profil['nip'] ?? '—') ?></p>
  </div>
</div>

<div class="doc-footer">
  Dicetak: <?= date('d/m/Y H:i') ?> &nbsp;|&nbsp; <?= APP_NAME ?> v<?= APP_VERSION ?>
</div>

<div class="ctrl-bar no-print">
  <span><?= count($data) ?> kegiatan &bull; <?= $totalJP ?> JP total &bull; Tahun <?= e($tahun) ?></span>
  <div class="btns">
    <button class="btn-ctrl btn-back" onclick="window.history.back()">← Kembali</button>
    <button class="btn-ctrl btn-print" onclick="window.print()">🖨 Cetak / Simpan PDF</button>
  </div>
</div>

</body>
</html>
