<?php
require_once __DIR__ . '/config/secure_public.php';

// ===== Ambil ID =====
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if (!$id) {
  header("Location: spme.php");
  exit;
}

// ===== Ambil Data Akreditasi =====
$stmt = $pdo->prepare("
  SELECT id, type, institution_name, program_studi, strata, decision_number, rating,
         valid_from, valid_until, accreditation_body, document_file, created_at
  FROM accreditation
  WHERE id = ?
  LIMIT 1
");
$stmt->execute([$id]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$data) {
  header("Location: spme.php?error=notfound");
  exit;
}

// ===== Tentukan status masa berlaku =====
$now = new DateTime();
$status_label = "Tidak diketahui";
$status_color = "secondary";

if ($data['valid_until']) {
  $valid_until = new DateTime($data['valid_until']);
  $days_left = $now->diff($valid_until)->days;
  $is_expired = $valid_until < $now;
  $status_label = $is_expired ? "Kadaluarsa" : ($days_left <= 90 ? "Segera Habis" : "Aktif");
  $status_color = $is_expired ? "danger" : ($days_left <= 90 ? "warning" : "success");
}

// ===== Meta =====
$page_title = "Detail Akreditasi | " . ($data['program_studi'] ?: $data['institution_name']);
$page_description = "Detail akreditasi " . e($data['program_studi'] ?: $data['institution_name']) . " Universitas Riyadlul Ulum Tasikmalaya.";
$page_url = base_url('spme_detail.php?id=' . $id);

include __DIR__ . '/includes/header.php';
?>

<!-- HERO -->
<section class="hero-spme-detail d-flex align-items-center justify-content-center text-center text-white position-relative">
  <div class="overlay"></div>
  <div class="content position-relative z-2">
    <h1 class="fw-bold display-6 mb-2">Detail Akreditasi</h1>
    <p class="text-light small">Universitas Riyadlul Ulum Tasikmalaya</p>
  </div>
</section>

<div class="container my-5 fade-section">
  <div class="row justify-content-center">
    <div class="col-lg-8">
      <div class="card border-0 shadow-sm rounded-4 overflow-hidden">
        <div class="card-body p-4 p-md-5">
          <h4 class="fw-bold text-success mb-3">
            <?= e($data['type'] === 'universitas' ? $data['institution_name'] : $data['program_studi']); ?>
          </h4>

          <div class="mb-4">
            <span class="badge bg-success me-2"><?= e(ucfirst($data['type'])); ?></span>
            <span class="badge bg-<?= e($status_color); ?>"><?= e($status_label); ?></span>
          </div>

          <table class="table table-borderless small mb-4">
            <tr>
              <th width="30%">Lembaga Akreditasi</th>
              <td><?= e($data['accreditation_body'] ?: '-'); ?></td>
            </tr>
            <tr>
              <th>Nomor SK</th>
              <td><?= e($data['decision_number'] ?: '-'); ?></td>
            </tr>
            <tr>
              <th>Nilai Akreditasi</th>
              <td><strong><?= e($data['rating'] ?: '-'); ?></strong></td>
            </tr>
            <tr>
              <th>Berlaku Dari</th>
              <td><?= e(indoDate($data['valid_from'])); ?></td>
            </tr>
            <tr>
              <th>Berlaku Hingga</th>
              <td><?= e(indoDate($data['valid_until'])); ?></td>
            </tr>
            <?php if ($data['type'] === 'prodi'): ?>
            <tr>
              <th>Strata</th>
              <td><?= e($data['strata'] ?: '-'); ?></td>
            </tr>
            <?php endif; ?>
            <tr>
              <th>Diperbarui</th>
              <td><?= e(indoDate(substr($data['created_at'], 0, 10))); ?></td>
            </tr>
          </table>

          <?php if (!empty($data['document_file'])): ?>
            <div class="text-center my-4">
              <a href="<?= base_url('uploads/accreditation/' . e($data['document_file'])); ?>" 
                 class="btn btn-outline-success" target="_blank">
                <i class="fa fa-download me-2"></i> Unduh Sertifikat Akreditasi
              </a>
            </div>
          <?php else: ?>
            <div class="alert alert-secondary text-center small">Dokumen sertifikat belum tersedia.</div>
          <?php endif; ?>

          <div class="text-center mt-4">
            <a href="<?= base_url('spme.php'); ?>" class="btn btn-link text-success">
              <i class="fa fa-arrow-left me-1"></i> Kembali ke daftar akreditasi
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<?php include __DIR__ . '/includes/footer.php'; ?>

<!-- ===== CUSTOM STYLE ===== -->
<style>
.hero-spme-detail {
  height: 40vh;
  background: linear-gradient(120deg, var(--green) 0%, var(--brown) 100%);
  position: relative;
  padding-top: 4rem;
}
.hero-spme-detail .overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.45);
}
.table th {
  width: 35%;
  color: #444;
}
.table td {
  color: #333;
}
.fade-section {
  opacity: 0;
  transform: translateY(30px);
  transition: opacity 0.8s ease, transform 0.8s ease;
}
.fade-section.visible {
  opacity: 1;
  transform: translateY(0);
}
</style>

<!-- ===== SCROLL ANIMATION ===== -->
<script>
document.addEventListener("DOMContentLoaded", () => {
  const fadeSections = document.querySelectorAll(".fade-section");
  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) entry.target.classList.add("visible");
    });
  }, { threshold: 0.2 });
  fadeSections.forEach(section => observer.observe(section));
});
</script>
