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

<section class="berita-detail-page">
  <div class="container">
    <div class="berita-detail-layout">
      <article class="berita-detail">
        <div class="berita-meta">
          <span class="tag"><?= htmlspecialchars($berita['kategori'] ?? 'Berita') ?></span>
          <span><?= date('d M Y', strtotime($berita['tanggal'])) ?></span>
          <?php if (!empty($berita['penulis'])): ?><span>&middot; oleh <?= htmlspecialchars($berita['penulis']) ?></span><?php endif; ?>
          <span>&middot; <?= (int) $berita['dilihat'] ?> dilihat</span>
        </div>

        <h1><?= htmlspecialchars($berita['judul']) ?></h1>

        <?php if (!empty($berita['thumbnail'])): ?>
          <div class="berita-thumbnail">
            <img src="<?= APP_URL . '/uploads/berita/' . htmlspecialchars($berita['thumbnail']) ?>" alt="<?= htmlspecialchars($berita['judul']) ?>">
          </div>
        <?php endif; ?>

        <div class="berita-content">
          <?= $berita['konten'] ?? '' /* konten HTML dari TinyMCE, hanya diisi admin terpercaya */ ?>
        </div>

        <div style="margin-top:40px;">
          <a href="<?= APP_URL ?>/berita" class="btn btn-outline">&larr; Kembali ke Daftar Berita</a>
        </div>
      </article>

      <aside class="sidebar">
        <div class="widget-box">
          <h4>Berita Lainnya</h4>
          <div class="related-list">
            <?php foreach ($beritaLain as $lain): ?>
              <?php if ($lain['slug'] === $berita['slug']) continue; ?>
              <a href="<?= APP_URL ?>/berita/<?= htmlspecialchars($lain['slug']) ?>" class="related-item" style="text-decoration:none; color:inherit;">
                <div class="thumb">
                  <?php if (!empty($lain['thumbnail'])): ?>
                    <img src="<?= APP_URL . '/uploads/berita/' . htmlspecialchars($lain['thumbnail']) ?>" alt="">
                  <?php endif; ?>
                </div>
                <div>
                  <h4><?= htmlspecialchars($lain['judul']) ?></h4>
                  <span class="date"><?= date('d M Y', strtotime($lain['tanggal'])) ?></span>
                </div>
              </a>
            <?php endforeach; ?>
          </div>
        </div>

        <?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; ?>
      </aside>
    </div>
  </div>
</section>

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