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

$sekolah = Database::fetchAll(
    "SELECT s.*, (SELECT COUNT(*) FROM kegiatan_pendampingan k WHERE k.sekolah_id=s.id) AS jml_kegiatan
     FROM sekolah s WHERE s.aktif=1 ORDER BY s.jenis, s.nama_sekolah"
);
$profil = Database::fetchOne("SELECT * FROM profil_pendamping LIMIT 1");

$totalNegeri = count(array_filter($sekolah, fn($s) => $s['jenis']==='SMP Negeri'));
$totalSwasta = count(array_filter($sekolah, fn($s) => $s['jenis']==='SMP Swasta'));
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Laporan Data Sekolah Dampingan</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:9.5pt;color:#1a1a2e;background:#fff}
  @page{size:A4 landscape;margin:15mm 15mm 15mm 18mm}
  @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:50px;height:50px;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:8px 0 10px}
  .title-block h2{font-size:12pt;font-weight:800;text-transform:uppercase;color:#1a3a6b}
  .title-block .garis{width:80px;height:3px;background:#c0392b;margin:5px auto;border-radius:2px}
  .summary-row{display:grid;grid-template-columns:repeat(4,1fr);gap:8px;margin-bottom:10px}
  .summary-box{text-align:center;border:1px solid #d0d8e8;border-radius:6px;padding:8px}
  .summary-box .val{font-size:18pt;font-weight:800;color:#1a3a6b;line-height:1}
  .summary-box .lbl{font-size:8pt;color:#666;margin-top:2px}
  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-n{display:inline-block;padding:1px 7px;border-radius:20px;font-size:7.5pt;font-weight:700;background:#e0f2fe;color:#0369a1}
  .badge-s{display:inline-block;padding:1px 7px;border-radius:20px;font-size:7.5pt;font-weight:700;background:#fee2e2;color:#b91c1c}
  .badge-akr{display:inline-block;padding:1px 7px;border-radius:20px;font-size:7.5pt;font-weight:700;background:#dcfce7;color:#15803d}
  .ttd{display:flex;justify-content:flex-end;margin-top:20px}
  .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:3px;min-width:160px;display:inline-block}
  .doc-footer{text-align:center;font-size:8pt;color:#999;border-top:1px solid #e0e0e0;padding-top:5px;margin-top:14px}
  .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="title-block">
  <h2>Daftar Sekolah Dampingan</h2>
  <div class="garis"></div>
</div>

<div class="summary-row">
  <div class="summary-box"><div class="val"><?= count($sekolah) ?></div><div class="lbl">Total Sekolah</div></div>
  <div class="summary-box"><div class="val"><?= $totalNegeri ?></div><div class="lbl">SMP Negeri</div></div>
  <div class="summary-box"><div class="val"><?= $totalSwasta ?></div><div class="lbl">SMP Swasta</div></div>
  <div class="summary-box"><div class="val"><?= count(array_unique(array_column($sekolah,'kecamatan'))) ?></div><div class="lbl">Kecamatan</div></div>
</div>

<table>
  <thead>
    <tr>
      <th width="28">No</th>
      <th width="80">NPSN</th>
      <th>Nama Sekolah</th>
      <th width="75">Jenis</th>
      <th width="90">Kecamatan</th>
      <th>Kepala Sekolah</th>
      <th width="90">Telepon</th>
      <th width="55" style="text-align:center">Akreditasi</th>
      <th width="40" style="text-align:center">Guru</th>
      <th width="45" style="text-align:center">Siswa</th>
      <th width="45" style="text-align:center">Kegiatan</th>
    </tr>
  </thead>
  <tbody>
    <?php if(empty($sekolah)): ?>
    <tr><td colspan="11" style="text-align:center;padding:16px;color:#999">Tidak ada data</td></tr>
    <?php else: ?>
    <?php $no=1; $prevJenis=''; foreach($sekolah as $s): ?>
    <?php if($s['jenis'] !== $prevJenis): $prevJenis=$s['jenis']; ?>
    <tr><td colspan="11" style="background:#e8edf5;font-weight:700;color:#1a3a6b;padding:5px 8px"><?= e($s['jenis']) ?></td></tr>
    <?php endif; ?>
    <tr>
      <td><?= $no++ ?></td>
      <td style="font-family:monospace;font-size:8.5pt"><?= e($s['npsn']) ?></td>
      <td style="font-weight:600"><?= e($s['nama_sekolah']) ?></td>
      <td><?= $s['jenis']==='SMP Negeri' ? '<span class="badge-n">Negeri</span>' : '<span class="badge-s">Swasta</span>' ?></td>
      <td><?= e($s['kecamatan']) ?></td>
      <td><?= e($s['kepala_sekolah']) ?></td>
      <td style="font-size:8.5pt"><?= $s['telepon'] ? e($s['telepon']) : '—' ?></td>
      <td style="text-align:center"><?= $s['akreditasi'] ? '<span class="badge-akr">'.$s['akreditasi'].'</span>' : '—' ?></td>
      <td style="text-align:center"><?= $s['jumlah_guru'] ?? '—' ?></td>
      <td style="text-align:center"><?= $s['jumlah_siswa'] ?? '—' ?></td>
      <td style="text-align:center;font-weight:700"><?= $s['jml_kegiatan'] ?></td>
    </tr>
    <?php endforeach; ?>
    <?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($sekolah) ?> sekolah &bull; <?= $totalNegeri ?> Negeri, <?= $totalSwasta ?> Swasta</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>
