<?php require BASE_PATH . '/app/Views/layouts/header.php'; ?>

<!-- ============ HERO ============ -->
<section class="hero" <?= !empty($identitas['hero_image']) ? 'style="background-image: linear-gradient(var(--overlay), var(--overlay)), url(\'' . APP_URL . '/uploads/identitas/' . htmlspecialchars($identitas['hero_image']) . '\'); background-size: cover; background-position: center;"' : '' ?>>
  <span class="hero-ribbon"></span>
  <div class="container hero-inner">
    <div class="hero-emblem">
      <?php if (!empty($identitas['logo'])): ?>
        <img src="<?= APP_URL . '/uploads/identitas/' . htmlspecialchars($identitas['logo']) ?>" alt="Logo <?= htmlspecialchars($identitas['nama_fakultas'] ?? '') ?>">
      <?php else: ?>
        🎓
      <?php endif; ?>
    </div>
    <p class="hero-kicker">
      <?= htmlspecialchars($identitas['singkatan'] ?? 'FAKULTAS') ?>
    </p>
    <h1 class="hero-title"><?= htmlspecialchars($identitas['hero_judul'] ?? $identitas['nama_fakultas'] ?? APP_NAME) ?></h1>
    <p class="hero-subtitle"><?= htmlspecialchars($identitas['hero_subjudul'] ?? $identitas['tagline'] ?? '') ?></p>

    <div class="hero-cards">
      <div class="hero-card">
        <span class="icon">🎓</span>
        <div>
          <h4>Beasiswa</h4>
          <p>Informasi program beasiswa untuk mahasiswa berprestasi.</p>
        </div>
      </div>
      <div class="hero-card">
        <span class="icon">🏛️</span>
        <div>
          <h4>Kampus Kami</h4>
          <p>Kenali fasilitas dan lingkungan belajar di fakultas.</p>
        </div>
      </div>
      <div class="hero-card">
        <span class="icon">📚</span>
        <div>
          <h4>Program Studi</h4>
          <p>Pilihan program studi unggulan dan terakreditasi.</p>
        </div>
      </div>
    </div>
  </div>
</section>

<!-- ============ SAMBUTAN / ABOUT ============ -->
<section class="about">
  <div class="container about-grid">
    <div class="about-text">
      <span class="eyebrow">Tentang Kami</span>
      <h2 class="about-title"><?= htmlspecialchars($identitas['nama_fakultas'] ?? APP_NAME) ?></h2>
      <p>
        <?= htmlspecialchars($identitas['tagline'] ?? 'Fakultas kami berkomitmen mencetak lulusan yang unggul, berkarakter, dan siap bersaing di dunia kerja maupun dunia riset.') ?>
      </p>
      <p>
        Didukung oleh tenaga pengajar berpengalaman dan kurikulum yang relevan dengan perkembangan industri,
        kami hadir untuk membentuk generasi pemimpin masa depan.
      </p>
      <a href="<?= APP_URL ?>/halaman/sambutan-dekan" class="btn btn-outline">Baca Sambutan Dekan</a>
    </div>

    <div class="about-photo">
      <?php if (!empty($identitas['foto_profil'])): ?>
        <img src="<?= APP_URL . '/uploads/identitas/' . htmlspecialchars($identitas['foto_profil']) ?>" alt="Foto <?= htmlspecialchars($identitas['nama_fakultas'] ?? '') ?>">
      <?php else: ?>
        <span>Foto profil kampus &mdash; unggah melalui panel admin (Identitas Kampus)</span>
      <?php endif; ?>
    </div>
  </div>
</section>

<!-- ============ NEWS + SIDEBAR ============ -->
<section class="news">
  <div class="container">
    <div class="news-head">
      <span class="eyebrow">Berita</span>
      <h2>Kabar &amp; Informasi Terbaru</h2>
      <p class="subtitle">Pusat Informasi Fakultas</p>
      <p>Ikuti perkembangan akademik, kemahasiswaan, dan prestasi terbaru dari fakultas kami.</p>
    </div>

    <div class="news-layout">
      <div class="news-grid">
        <?php if (empty($berita)): ?>
          <p style="color:var(--text-muted)">Belum ada berita yang dipublikasikan.</p>
        <?php else: ?>
          <?php $featured = array_slice($berita, 0, 3); ?>
          <?php foreach ($featured as $i => $item): ?>
            <?php $isFeatured = $i === 0; ?>
            <article class="news-card <?= $isFeatured ? 'tall' : '' ?>">
              <div class="news-card-thumb">
                <?php if (!empty($item['thumbnail'])): ?>
                  <img src="<?= APP_URL . '/uploads/berita/' . htmlspecialchars($item['thumbnail']) ?>" alt="<?= htmlspecialchars($item['judul']) ?>">
                <?php else: ?>
                  <div class="news-card-thumb-placeholder"></div>
                <?php endif; ?>
              </div>
              <div class="news-card-body">
                <span class="tag"><?= htmlspecialchars($item['kategori'] ?? 'Berita') ?> &middot; <?= date('d M Y', strtotime($item['tanggal'])) ?></span>
                <h3><?= htmlspecialchars($item['judul']) ?></h3>
                <a href="<?= APP_URL ?>/berita/<?= htmlspecialchars($item['slug']) ?>" class="read-more">Baca Selengkapnya</a>
              </div>
            </article>
          <?php endforeach; ?>
        <?php endif; ?>

        <div class="news-card cta yellow">
          <div class="news-card-body">
            <span class="tag">Info</span>
            <h3>Bergabung Bersama Kami &mdash; Penerimaan Mahasiswa Baru</h3>
            <a href="#" class="read-more">Daftar Sekarang</a>
          </div>
        </div>
        <div class="news-card cta yellow">
          <div class="news-card-body">
            <span class="tag">Berita</span>
            <h3>Lihat Semua Berita &amp; Pengumuman Fakultas</h3>
            <a href="<?= APP_URL ?>/berita" class="read-more">Lihat Semua</a>
          </div>
        </div>
      </div>

      <aside class="sidebar">
        <?php foreach ($widgetSidebar as $widget): ?>
          <div class="widget-box">
            <?php if (!empty($widget['judul'])): ?><h4><?= htmlspecialchars($widget['judul']) ?></h4><?php endif; ?>
            <?php if ($widget['tipe'] === 'gambar' && !empty($widget['gambar'])): ?>
              <img src="<?= APP_URL . '/uploads/widget/' . htmlspecialchars($widget['gambar']) ?>" alt="<?= htmlspecialchars($widget['judul'] ?? '') ?>" style="border-radius:8px;margin-bottom:10px;">
            <?php elseif ($widget['tipe'] === 'html'): ?>
              <?= $widget['konten'] ?? '' ?>
            <?php else: ?>
              <p><?= htmlspecialchars($widget['konten'] ?? '') ?></p>
            <?php endif; ?>
            <?php if (!empty($widget['url'])): ?>
              <a href="<?= htmlspecialchars($widget['url']) ?>" class="btn" style="margin-top:12px;">Selengkapnya</a>
            <?php endif; ?>
          </div>
        <?php endforeach; ?>

        <!-- Widget kalender agenda (dibangun otomatis dari tabel agenda) -->
        <div class="widget-box">
          <?php
            $namaBulan = [1=>'Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember'];
            $jumlahHari = (int) date('t', mktime(0, 0, 0, $bulanIni, 1, $tahunIni));
            $hariPertama = (int) date('N', mktime(0, 0, 0, $bulanIni, 1, $tahunIni)); // 1 (Senin) - 7 (Minggu)
            $hariIni = (int) date('j');
            $bulanSekarang = (int) date('n') === $bulanIni && (int) date('Y') === $tahunIni;
          ?>
          <div class="calendar-head">
            <span><?= $namaBulan[$bulanIni] ?> <?= $tahunIni ?></span>
          </div>
          <div class="calendar-grid">
            <span class="dow">S</span><span class="dow">S</span><span class="dow">R</span>
            <span class="dow">K</span><span class="dow">J</span><span class="dow">S</span><span class="dow">M</span>
            <?php for ($i = 1; $i < $hariPertama; $i++): ?>
              <span class="empty">-</span>
            <?php endfor; ?>
            <?php for ($d = 1; $d <= $jumlahHari; $d++): ?>
              <?php
                $classes = ['day'];
                if (in_array($d, $tanggalAgenda)) $classes[] = 'has-agenda';
                if ($bulanSekarang && $d === $hariIni) $classes[] = 'today';
              ?>
              <span class="<?= implode(' ', $classes) ?>"><?= $d ?></span>
            <?php endfor; ?>
          </div>

          <?php if (!empty($agenda)): ?>
            <div class="agenda-list">
              <?php foreach ($agenda as $ag): ?>
                <div class="agenda-item">
                  <span class="date"><?= date('d M', strtotime($ag['tanggal_mulai'])) ?></span>
                  <span class="desc"><strong><?= htmlspecialchars($ag['judul']) ?></strong><?= htmlspecialchars($ag['lokasi'] ?? '') ?></span>
                </div>
              <?php endforeach; ?>
            </div>
          <?php endif; ?>
        </div>

        <div class="widget-box cta">
          <h4>Gabung Sekarang</h4>
          <p>Pendaftaran mahasiswa baru tahun akademik ini telah dibuka.</p>
          <a href="#" class="btn">Registrasi</a>
        </div>
      </aside>
    </div>
  </div>
</section>

<!-- ============ GALERI ============ -->
<section class="galeri-preview">
  <div class="container">
    <div class="section-head-row">
      <div>
        <span class="eyebrow">Galeri</span>
        <h2>Dokumentasi Kegiatan</h2>
      </div>
      <div class="section-head-links">
        <a href="<?= APP_URL ?>/galeri-foto" class="btn btn-outline">Lihat Semua Foto</a>
        <a href="<?= APP_URL ?>/galeri-video" class="btn btn-outline">Lihat Semua Video</a>
      </div>
    </div>

    <?php if (!empty($albumGaleri)): ?>
      <div class="galeri-preview-grid">
        <?php foreach ($albumGaleri as $album): ?>
          <a href="<?= APP_URL ?>/galeri-foto/<?= $album['id'] ?>" class="galeri-preview-card">
            <div class="galeri-preview-cover">
              <?php if (!empty($album['cover'])): ?>
                <img src="<?= APP_URL . '/uploads/galeri/' . htmlspecialchars($album['cover']) ?>" alt="<?= htmlspecialchars($album['judul']) ?>">
              <?php endif; ?>
            </div>
            <h4><?= htmlspecialchars($album['judul']) ?></h4>
            <span class="count"><?= (int) $album['jumlah_foto'] ?> Foto</span>
          </a>
        <?php endforeach; ?>

        <?php foreach ($videoGaleri as $video): ?>
          <a href="<?= APP_URL ?>/galeri-video" class="galeri-preview-card">
            <div class="galeri-preview-cover">
              <?php if (!empty($video['thumbnail'])): ?>
                <img src="<?= htmlspecialchars($video['thumbnail']) ?>" alt="<?= htmlspecialchars($video['judul']) ?>">
              <?php endif; ?>
              <span class="play-icon">▶</span>
            </div>
            <h4><?= htmlspecialchars($video['judul']) ?></h4>
            <span class="count">Video</span>
          </a>
        <?php endforeach; ?>
      </div>
    <?php else: ?>
      <p style="color:#52586a;">Belum ada dokumentasi galeri.</p>
    <?php endif; ?>
  </div>
</section>

<!-- ============ FASILITAS ============ -->
<section class="fasilitas-preview">
  <div class="container">
    <span class="eyebrow">Fasilitas</span>
    <h2>Fasilitas Penunjang Akademik</h2>
    <p class="section-desc">Sarana dan prasarana yang mendukung proses belajar-mengajar di fakultas kami.</p>

    <?php if (!empty($daftarFasilitas)): ?>
      <div class="fasilitas-preview-grid">
        <?php foreach ($daftarFasilitas as $item): ?>
          <div class="fasilitas-preview-card">
            <div class="fasilitas-preview-media">
              <?php if (!empty($item['gambar'])): ?>
                <img src="<?= APP_URL . '/uploads/fasilitas/' . htmlspecialchars($item['gambar']) ?>" alt="<?= htmlspecialchars($item['nama']) ?>">
              <?php else: ?>
                <span class="icon"><?= htmlspecialchars($item['icon'] ?: '🏢') ?></span>
              <?php endif; ?>
            </div>
            <h4><?= htmlspecialchars($item['nama']) ?></h4>
          </div>
        <?php endforeach; ?>
      </div>
      <div style="text-align:center; margin-top:36px;">
        <a href="<?= APP_URL ?>/fasilitas" class="btn btn-outline light">Lihat Semua Fasilitas</a>
      </div>
    <?php else: ?>
      <p style="color:var(--text-muted);">Belum ada data fasilitas.</p>
    <?php endif; ?>
  </div>
</section>

<?php require BASE_PATH . '/app/Views/layouts/footer.php'; ?>
