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

$id   = (int)($_GET['id'] ?? 0);
$data = Database::fetchOne("SELECT * FROM sekolah WHERE id=?", [$id]);
if (!$data) { setFlash('error','Sekolah tidak ditemukan.'); redirect(BASE_URL.'/modules/sekolah/index.php'); }

if (isSekolah() && $_SESSION['sekolah_id'] != $id) {
    setFlash('error','Anda tidak memiliki akses ke data ini.');
    redirect(BASE_URL.'/modules/sekolah/index.php');
}

$kegiatan = Database::fetchAll(
    "SELECT * FROM kegiatan_pendampingan WHERE sekolah_id=? ORDER BY tanggal_kegiatan DESC LIMIT 10",
    [$id]
);
$dokumen = Database::fetchAll(
    "SELECT * FROM dokumen WHERE sekolah_id=? ORDER BY created_at DESC LIMIT 5",
    [$id]
);
$totalKeg = Database::count("SELECT COUNT(*) FROM kegiatan_pendampingan WHERE sekolah_id=?", [$id]);

$pageTitle  = e($data['nama_sekolah']);
$activePage = 'sekolah';
require_once ROOT_PATH . '/views/layouts/header.php';
?>

<div class="page-header">
  <div class="page-header-title">
    <h1><i class="bi bi-building me-2 text-primary"></i><?= e($data['nama_sekolah']) ?></h1>
    <nav><ol class="breadcrumb">
      <li class="breadcrumb-item"><a href="<?= BASE_URL ?>/modules/dashboard/index.php">Dashboard</a></li>
      <li class="breadcrumb-item"><a href="<?= BASE_URL ?>/modules/sekolah/index.php">Sekolah</a></li>
      <li class="breadcrumb-item active">Detail</li>
    </ol></nav>
  </div>
  <?php if(isAdmin()): ?>
  <a href="form.php?id=<?= $id ?>" class="btn btn-outline-warning btn-sm"><i class="bi bi-pencil me-1"></i>Edit</a>
  <?php endif; ?>
</div>

<div class="row g-3">
  <!-- Info Utama -->
  <div class="col-lg-4">
    <div class="card mb-3">
      <div class="card-body text-center py-4">
        <div style="width:70px;height:70px;border-radius:16px;background:var(--primary);display:grid;place-items:center;font-size:1.8rem;color:#fff;margin:0 auto 1rem">
          <i class="bi bi-building"></i>
        </div>
        <h5 class="fw-700 mb-1"><?= e($data['nama_sekolah']) ?></h5>
        <span class="badge <?= $data['jenis']==='SMP Negeri'?'bg-primary':'bg-danger' ?>-subtle text-<?= $data['jenis']==='SMP Negeri'?'primary':'danger' ?> mb-2">
          <?= e($data['jenis']) ?>
        </span>
        <?php if($data['akreditasi']): ?>
        <br><span class="badge bg-success-subtle text-success">Akreditasi <?= e($data['akreditasi']) ?></span>
        <?php endif; ?>
        <div class="mt-3 text-muted" style="font-size:.82rem">
          <div><i class="bi bi-upc me-1"></i>NPSN: <strong><?= e($data['npsn']) ?></strong></div>
        </div>
      </div>
    </div>

    <!-- Stat mini -->
    <div class="card mb-3">
      <div class="card-body p-0">
        <div class="row g-0 text-center">
          <div class="col-4 p-3 border-end">
            <div class="fw-800 fs-4 text-primary"><?= $totalKeg ?></div>
            <div class="text-muted" style="font-size:.72rem">Kegiatan</div>
          </div>
          <div class="col-4 p-3 border-end">
            <div class="fw-800 fs-4 text-success"><?= $data['jumlah_guru'] ?? '—' ?></div>
            <div class="text-muted" style="font-size:.72rem">Guru</div>
          </div>
          <div class="col-4 p-3">
            <div class="fw-800 fs-4 text-warning"><?= $data['jumlah_siswa'] ?? '—' ?></div>
            <div class="text-muted" style="font-size:.72rem">Siswa</div>
          </div>
        </div>
      </div>
    </div>

    <!-- Kontak -->
    <div class="card">
      <div class="card-header"><span class="card-header-title"><i class="bi bi-telephone me-2"></i>Kontak</span></div>
      <div class="card-body" style="font-size:.85rem">
        <?php if($data['telepon']): ?>
        <div class="mb-2"><i class="bi bi-telephone text-muted me-2"></i><?= e($data['telepon']) ?></div>
        <?php endif; ?>
        <?php if($data['email']): ?>
        <div class="mb-2"><i class="bi bi-envelope text-muted me-2"></i><?= e($data['email']) ?></div>
        <?php endif; ?>
        <?php if($data['website']): ?>
        <div class="mb-2"><i class="bi bi-globe text-muted me-2"></i><a href="<?= e($data['website']) ?>" target="_blank"><?= e($data['website']) ?></a></div>
        <?php endif; ?>
        <div class="mb-2"><i class="bi bi-geo-alt text-muted me-2"></i><?= e($data['alamat']) ?></div>
        <div><i class="bi bi-map text-muted me-2"></i>Kec. <?= e($data['kecamatan']) ?></div>
      </div>
    </div>
  </div>

  <!-- Kanan -->
  <div class="col-lg-8">
    <!-- Info Kepala -->
    <div class="card mb-3">
      <div class="card-header"><span class="card-header-title"><i class="bi bi-person-badge me-2"></i>Kepala Sekolah</span></div>
      <div class="card-body">
        <div class="d-flex align-items-center gap-3">
          <div style="width:50px;height:50px;border-radius:50%;background:var(--gold);display:grid;place-items:center;font-size:1.2rem;color:#fff;font-weight:800;flex-shrink:0">
            <?= strtoupper(substr($data['kepala_sekolah'],0,1)) ?>
          </div>
          <div>
            <div class="fw-700"><?= e($data['kepala_sekolah']) ?></div>
            <?php if($data['nip_kepsek']): ?>
            <div class="text-muted small">NIP: <?= e($data['nip_kepsek']) ?></div>
            <?php endif; ?>
          </div>
        </div>
      </div>
    </div>

    <!-- Kegiatan terbaru -->
    <div class="card mb-3">
      <div class="card-header">
        <span class="card-header-title"><i class="bi bi-journal-text me-2"></i>Kegiatan Pendampingan Terbaru</span>
        <a href="<?= BASE_URL ?>/modules/kegiatan/index.php?sekolah=<?= $id ?>" class="btn btn-sm btn-outline-primary">Semua</a>
      </div>
      <div class="table-responsive">
        <table class="table table-hover mb-0">
          <thead><tr><th>Tanggal</th><th>Kegiatan</th><th>Sasaran</th><th>Status</th></tr></thead>
          <tbody>
            <?php if(empty($kegiatan)): ?>
            <tr><td colspan="4" class="text-center py-3 text-muted">Belum ada kegiatan</td></tr>
            <?php else: ?>
            <?php foreach($kegiatan as $k):
              $sc=['Draft'=>'secondary','Terkirim'=>'info','Disetujui'=>'success'][$k['status']]??'secondary'; ?>
            <tr>
              <td style="white-space:nowrap;font-size:.82rem"><?= formatTanggal($k['tanggal_kegiatan'],'d M Y') ?></td>
              <td><?= e(mb_strimwidth($k['nama_kegiatan'],0,40,'…')) ?></td>
              <td><?= e(mb_strimwidth($k['sasaran'],0,25,'…')) ?></td>
              <td><span class="badge bg-<?= $sc ?>-subtle text-<?= $sc ?>"><?= e($k['status']) ?></span></td>
            </tr>
            <?php endforeach; ?>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>

    <!-- Dokumen -->
    <?php if($dokumen): ?>
    <div class="card">
      <div class="card-header">
        <span class="card-header-title"><i class="bi bi-folder2-open me-2"></i>Dokumen</span>
        <a href="<?= BASE_URL ?>/modules/dokumen/index.php?sekolah=<?= $id ?>" class="btn btn-sm btn-outline-primary">Semua</a>
      </div>
      <div class="list-group list-group-flush">
        <?php foreach($dokumen as $dok): ?>
        <div class="list-group-item border-0 d-flex align-items-center gap-2 py-2">
          <i class="bi bi-file-earmark-pdf text-danger fs-5"></i>
          <div class="flex-1">
            <div class="fw-600" style="font-size:.84rem"><?= e($dok['nama_dokumen']) ?></div>
            <div class="text-muted" style="font-size:.73rem"><?= e($dok['jenis']) ?> • <?= formatTanggal($dok['created_at'],'d M Y') ?></div>
          </div>
          <a href="<?= BASE_URL ?>/uploads/dokumen/<?= e($dok['nama_file']) ?>" target="_blank" class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-download"></i>
          </a>
        </div>
        <?php endforeach; ?>
      </div>
    </div>
    <?php endif; ?>
  </div>
</div>

<?php require_once ROOT_PATH . '/views/layouts/footer.php'; ?>
