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

<div class="page-head">
  <h2>Daftar Menu &amp; Submenu</h2>
  <a href="<?= APP_URL ?>/admin/menu/create" class="btn">+ Tambah Menu</a>
</div>

<div class="card" style="padding:0;">
  <table>
    <thead>
      <tr>
        <th>Label</th>
        <th>Tipe</th>
        <th>URL</th>
        <th>Urutan</th>
        <th>Status</th>
        <th style="text-align:right;">Aksi</th>
      </tr>
    </thead>
    <tbody>
      <?php if (empty($menus)): ?>
        <tr><td colspan="6" style="text-align:center; color:var(--admin-muted); padding:24px;">Belum ada menu.</td></tr>
      <?php endif; ?>

      <?php foreach ($menus as $menu): ?>
        <tr>
          <td>
            <?= $menu['parent_id'] ? '&nbsp;&nbsp;&nbsp;&nbsp;↳ ' : '' ?>
            <?= htmlspecialchars($menu['label']) ?>
            <?php if ($menu['parent_id']): ?><span class="badge badge-sub">Submenu</span><?php endif; ?>
          </td>
          <td><?= htmlspecialchars($menu['tipe']) ?></td>
          <td style="max-width:220px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; color:var(--admin-muted);"><?= htmlspecialchars($menu['url']) ?></td>
          <td><?= (int) $menu['urutan'] ?></td>
          <td>
            <?php if ($menu['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/menu/<?= $menu['id'] ?>/edit" class="btn btn-secondary btn-sm">Edit</a>
            <form action="<?= APP_URL ?>/admin/menu/<?= $menu['id'] ?>/delete" method="POST" style="display:inline;"
                  onsubmit="return confirm('Hapus menu &quot;<?= htmlspecialchars(addslashes($menu['label'])) ?>&quot;? Submenu di bawahnya juga akan terhapus.');">
              <?= \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'; ?>
