<?php $pageTitle = 'Berita'; ?>
<?php require BASE_PATH . '/app/Views/admin/layouts/header.php'; ?>

<div class="page-head">
  <h2>Daftar Berita</h2>
  <div style="display:flex; gap:8px;">
    <a href="<?= APP_URL ?>/admin/kategori-berita" class="btn btn-secondary">Kelola Kategori</a>
    <a href="<?= APP_URL ?>/admin/berita/create" class="btn">+ Tulis Berita</a>
  </div>
</div>

<div class="card" style="padding:0;">
  <table>
    <thead>
      <tr>
        <th>Judul</th>
        <th>Kategori</th>
        <th>Tanggal</th>
        <th>Status</th>
        <th>Dilihat</th>
        <th style="text-align:right;">Aksi</th>
      </tr>
    </thead>
    <tbody>
      <?php if (empty($daftarBerita)): ?>
        <tr><td colspan="6" style="text-align:center; color:var(--admin-muted); padding:24px;">Belum ada berita. Klik "Tulis Berita" untuk mulai.</td></tr>
      <?php endif; ?>

      <?php foreach ($daftarBerita as $item): ?>
        <tr>
          <td style="max-width:280px;"><?= htmlspecialchars($item['judul']) ?></td>
          <td><?= htmlspecialchars($item['kategori'] ?? '—') ?></td>
          <td><?= date('d M Y', strtotime($item['tanggal'])) ?></td>
          <td>
            <?php if ($item['status'] === 'terbit'): ?>
              <span class="badge badge-active">Terbit</span>
            <?php else: ?>
              <span class="badge badge-inactive">Draft</span>
            <?php endif; ?>
          </td>
          <td><?= (int) $item['dilihat'] ?></td>
          <td style="text-align:right; white-space:nowrap;">
            <a href="<?= APP_URL ?>/berita/<?= htmlspecialchars($item['slug']) ?>" target="_blank" class="btn btn-secondary btn-sm">Lihat</a>
            <a href="<?= APP_URL ?>/admin/berita/<?= $item['id'] ?>/edit" class="btn btn-secondary btn-sm">Edit</a>
            <form action="<?= APP_URL ?>/admin/berita/<?= $item['id'] ?>/delete" method="POST" style="display:inline;"
                  onsubmit="return confirm('Hapus berita &quot;<?= htmlspecialchars(addslashes($item['judul'])) ?>&quot;?');">
              <?= \App\Core\Csrf::field() ?>
              <button type="submit" class="btn btn-danger btn-sm">Hapus</button>
            </form>
          </td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
</div>

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