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

$profil  = Database::fetchOne("SELECT * FROM profil_pendamping LIMIT 1");
$riwayat = $profil ? Database::fetchAll("SELECT * FROM riwayat_tugas WHERE profil_id=? ORDER BY tahun_mulai DESC", [$profil['id']]) : [];
$pengembangan = Database::fetchAll("SELECT * FROM pengembangan_diri ORDER BY tanggal_mulai DESC LIMIT 10");
$totalKeg = Database::count("SELECT COUNT(*) FROM kegiatan_pendampingan");
$totalSek = Database::count("SELECT COUNT(*) FROM sekolah WHERE aktif=1");
$totalJP  = Database::count("SELECT SUM(jumlah_jp) FROM pengembangan_diri");
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Profil Pendamping Sekolah</title>
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700;800&family=Playfair+Display:wght@600&display=swap" rel="stylesheet">
<style>
  *{box-sizing:border-box;margin:0;padding:0}
  body{font-family:'Plus Jakarta Sans',sans-serif;font-size:10.5pt;color:#1a1a2e;background:#fff}
  @page{size:A4 portrait;margin:18mm 15mm 18mm 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:12px}
  .kop-logo{width:54px;height:54px;background:#1a3a6b;border-radius:10px;display:flex;align-items:center;justify-content:center;flex-shrink:0}
  .kop-logo svg{width:30px;height:30px;fill:#fff}
  .kop-info h1{font-size:13pt;font-weight:800;color:#1a3a6b}
  .kop-info p{font-size:8.5pt;color:#555;margin-top:2px}

  .hero{display:grid;grid-template-columns:auto 1fr;gap:20px;align-items:start;margin-bottom:14px;padding:14px;border:1px solid #d0d8e8;border-radius:8px;background:#f5f8ff}
  .hero-avatar{width:100px;height:100px;border-radius:12px;background:#1a3a6b;display:flex;align-items:center;justify-content:center;font-size:2.5rem;color:rgba(255,255,255,.9);flex-shrink:0;overflow:hidden}
  .hero-avatar img{width:100%;height:100%;object-fit:cover;border-radius:12px}
  .hero-name{font-family:'Playfair Display',serif;font-size:16pt;font-weight:600;color:#1a3a6b;margin-bottom:4px}
  .hero-jabatan{font-size:10pt;color:#444;margin-bottom:6px}
  .hero-instansi{font-size:9pt;color:#666}
  .hero-badges{display:flex;gap:8px;margin-top:6px;flex-wrap:wrap}
  .badge{display:inline-block;padding:2px 10px;border-radius:20px;font-size:8pt;font-weight:700;background:#1a3a6b22;color:#1a3a6b}
  .badge-red{background:#c0392b22;color:#c0392b}

  .stats-row{display:grid;grid-template-columns:repeat(3,1fr);gap:10px;margin-bottom:14px}
  .stat-box{text-align:center;border:1px solid #d0d8e8;border-radius:8px;padding:10px}
  .stat-box .val{font-size:20pt;font-weight:800;color:#1a3a6b;line-height:1}
  .stat-box .lbl{font-size:8.5pt;color:#666;margin-top:3px}

  .section-head{font-size:10pt;font-weight:700;color:#fff;background:#1a3a6b;padding:5px 10px;margin:12px 0 8px;border-radius:4px}
  
  .info-grid{display:grid;grid-template-columns:1fr 1fr;gap:5px 20px;margin-bottom:12px;font-size:9.5pt}
  .info-item{display:flex;gap:8px}
  .info-item .lbl{color:#555;font-weight:600;min-width:90px;flex-shrink:0}

  table{width:100%;border-collapse:collapse;font-size:9.5pt;margin-bottom:12px}
  thead th{background:#1a3a6b;color:#fff;padding:6px 8px;font-weight:600;text-align:left}
  tbody td{padding:5px 8px;border-bottom:1px solid #e8edf5}
  tbody tr:nth-child(even) td{background:#f5f8ff}
  .ttd{display:flex;justify-content:flex-end;margin-top:22px}
  .ttd-box{text-align:center;font-size:10pt}
  .ttd-box .space{height:55px}
  .ttd-box .name{font-weight:700;border-top:1px solid #333;padding-top:4px;min-width:170px;display:inline-block}
  .doc-footer{text-align:center;font-size:8pt;color:#999;border-top:1px solid #e0e0e0;padding-top:6px;margin-top:16px}
  .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)}
</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="section-head" style="margin-top:0">Profil Pendamping Sekolah</div>

<!-- HERO -->
<div class="hero">
  <div class="hero-avatar">
    <?php if($profil && $profil['foto']): ?>
    <img src="<?= ROOT_PATH ?>/uploads/foto_profil/<?= e($profil['foto']) ?>" alt="Foto">
    <?php else: ?>
    <span style="font-size:2.5rem">👤</span>
    <?php endif; ?>
  </div>
  <div>
    <div class="hero-name"><?= e($profil['nama_lengkap'] ?? 'Nama Pendamping') ?></div>
    <div class="hero-jabatan"><?= e($profil['jabatan'] ?? 'Jabatan') ?></div>
    <div class="hero-instansi"><?= e($profil['unit_kerja'] ?? APP_INSTANSI) ?></div>
    <div class="hero-badges">
      <?php if($profil['pangkat']): ?><span class="badge"><?= e($profil['pangkat']) ?> / <?= e($profil['golongan']) ?></span><?php endif; ?>
      <?php if($profil['pendidikan']): ?><span class="badge badge-red"><?= e($profil['pendidikan']) ?></span><?php endif; ?>
    </div>
  </div>
</div>

<!-- STATS -->
<div class="stats-row">
  <div class="stat-box"><div class="val"><?= $totalSek ?></div><div class="lbl">Sekolah Dampingan</div></div>
  <div class="stat-box"><div class="val"><?= $totalKeg ?></div><div class="lbl">Total Kegiatan</div></div>
  <div class="stat-box"><div class="val"><?= $totalJP ?></div><div class="lbl">Jam Pengembangan Diri</div></div>
</div>

<!-- BIODATA -->
<div class="section-head">Biodata</div>
<div class="info-grid">
  <div class="info-item"><span class="lbl">NIP</span><span><?= e($profil['nip'] ?? '—') ?></span></div>
  <div class="info-item"><span class="lbl">Telepon</span><span><?= e($profil['telepon'] ?? '—') ?></span></div>
  <div class="info-item"><span class="lbl">Email</span><span><?= e($profil['email'] ?? '—') ?></span></div>
  <div class="info-item"><span class="lbl">Pangkat/Gol</span><span><?= e(($profil['pangkat']??'').' / '.($profil['golongan']??'—')) ?></span></div>
  <div class="info-item" style="grid-column:1/-1"><span class="lbl">Alamat</span><span><?= e($profil['alamat'] ?? '—') ?></span></div>
  <?php if($profil['tentang']): ?>
  <div class="info-item" style="grid-column:1/-1"><span class="lbl">Tentang</span><span style="text-align:justify"><?= e($profil['tentang']) ?></span></div>
  <?php endif; ?>
</div>

<!-- RIWAYAT TUGAS -->
<?php if($riwayat): ?>
<div class="section-head">Riwayat Tugas</div>
<table>
  <thead><tr><th width="28">No</th><th>Jabatan</th><th>Unit Kerja</th><th width="80">Periode</th></tr></thead>
  <tbody>
    <?php $no=1; foreach($riwayat as $rw): ?>
    <tr>
      <td><?= $no++ ?></td>
      <td><?= e($rw['jabatan']) ?></td>
      <td><?= e($rw['unit_kerja']) ?></td>
      <td style="white-space:nowrap"><?= $rw['tahun_mulai'] ?> – <?= $rw['tahun_selesai'] ?? 'Sekarang' ?></td>
    </tr>
    <?php endforeach; ?>
  </tbody>
</table>
<?php endif; ?>

<!-- PENGEMBANGAN DIRI (ringkas) -->
<?php if($pengembangan): ?>
<div class="section-head">Pengembangan Diri (10 Terakhir)</div>
<table>
  <thead><tr><th width="28">No</th><th width="70">Jenis</th><th>Judul</th><th>Penyelenggara</th><th width="80">Tahun</th><th width="35" style="text-align:center">JP</th></tr></thead>
  <tbody>
    <?php $no=1; foreach($pengembangan as $p): ?>
    <tr>
      <td><?= $no++ ?></td>
      <td><span class="badge" style="font-size:7.5pt"><?= e($p['jenis_kegiatan']) ?></span></td>
      <td><?= e(mb_strimwidth($p['judul_kegiatan'],0,55,'…')) ?></td>
      <td><?= e(mb_strimwidth($p['penyelenggara'],0,35,'…')) ?></td>
      <td><?= date('Y', strtotime($p['tanggal_mulai'])) ?></td>
      <td style="text-align:center;font-weight:700"><?= $p['jumlah_jp'] ?? '—' ?></td>
    </tr>
    <?php endforeach; ?>
  </tbody>
</table>
<?php endif; ?>

<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>Profil Pendamping — <?= e($profil['nama_lengkap'] ?? '') ?></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>
