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

<div class="page-head">
  <h2>Daftar Widget</h2>
  <a href="<?= APP_URL ?>/admin/widget/create" class="btn">+ Tambah Widget</a>
</div>

<div class="card" style="padding:0;">
  <table>
    <thead>
      <tr>
        <th>Judul</th>
        <th>Tipe</th>
        <th>Posisi</th>
        <th>Urutan</th>
        <th>Status</th>
        <th style="text-align:right;">Aksi</th>
      </tr>
    </thead>
    <tbody>
      <?php if (empty($daftarWidget)): ?>
        <tr><td colspan="6" style="text-align:center; color:var(--admin-muted); padding:24px;">
          Belum ada widget. Widget akan tampil di sidebar halaman Beranda, Berita, dan Halaman Statis.
        </td></tr>
      <?php endif; ?>

      <?php foreach ($daftarWidget as $item): ?>
        <tr>
          <td><?= htmlspecialchars($item['judul'] ?: '(tanpa judul)') ?></td>
          <td><span class="badge badge-sub"><?= htmlspecialchars(ucfirst($item['tipe'])) ?></span></td>
          <td><?= htmlspecialchars(ucfirst($item['posisi'])) ?></td>
          <td><?= (int) $item['urutan'] ?></td>
          <td>
            <?php if ($item['is_active']): ?>
              <span class="badge badge-active">Aktif</span>
            <?php else: ?>
              <span class="badge badge-inactive">Nonaktif</span>
            <?php endif; ?>
          </td>
          <td style="text-align:right; white-space:nowrap;">
            <a href="<?= APP_URL ?>/admin/widget/<?= $item['id'] ?>/edit" class="btn btn-secondary btn-sm">Edit</a>
            <form action="<?= APP_URL ?>/admin/widget/<?= $item['id'] ?>/delete" method="POST" style="display:inline;"
                  onsubmit="return confirm('Hapus widget &quot;<?= htmlspecialchars(addslashes($item['judul'] ?: 'ini')) ?>&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'; ?>
