<?php
$isEdit = !empty($menu);
$pageTitle = $isEdit ? 'Edit Menu' : 'Tambah Menu';
$action = $isEdit ? APP_URL . '/admin/menu/' . $menu['id'] : APP_URL . '/admin/menu';
?>
<?php require BASE_PATH . '/app/Views/admin/layouts/header.php'; ?>

<div class="page-head">
  <h2><?= $isEdit ? 'Edit Menu' : 'Tambah Menu Baru' ?></h2>
  <a href="<?= APP_URL ?>/admin/menu" class="btn btn-secondary">Kembali</a>
</div>

<div class="card" style="max-width:640px;">
  <form method="POST" action="<?= $action ?>">
    <?= \App\Core\Csrf::field() ?>

    <div class="form-group">
      <label for="label">Label Menu</label>
      <input type="text" id="label" name="label" class="form-control" required
             value="<?= htmlspecialchars($menu['label'] ?? '') ?>" placeholder="Contoh: Tentang Kami">
    </div>

    <div class="form-row">
      <div class="form-group">
        <label for="tipe">Tipe</label>
        <select id="tipe" name="tipe" class="form-control">
          <?php
            $tipeOptions = [
              'custom'    => 'Custom / Tautan biasa',
              'halaman'   => 'Halaman Statis',
              'berita'    => 'Daftar Berita',
              'eksternal' => 'Tautan Eksternal',
            ];
            $currentTipe = $menu['tipe'] ?? 'custom';
          ?>
          <?php foreach ($tipeOptions as $val => $label): ?>
            <option value="<?= $val ?>" <?= $currentTipe === $val ? 'selected' : '' ?>><?= $label ?></option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="form-group">
        <label for="parent_id">Induk Menu (opsional)</label>
        <select id="parent_id" name="parent_id" class="form-control">
          <option value="">Tidak ada &mdash; jadi menu utama</option>
          <?php foreach ($parentOptions as $opt): ?>
            <option value="<?= $opt['id'] ?>" <?= (!empty($menu['parent_id']) && (int) $menu['parent_id'] === (int) $opt['id']) ? 'selected' : '' ?>>
              <?= htmlspecialchars($opt['label']) ?>
            </option>
          <?php endforeach; ?>
        </select>
        <p class="hint">Pilih induk kalau menu ini ingin jadi submenu (dropdown).</p>
      </div>
    </div>

    <div class="form-group">
      <label for="url">URL / Tautan</label>
      <input type="text" id="url" name="url" class="form-control"
             value="<?= htmlspecialchars($menu['url'] ?? '') ?>" placeholder="/halaman/visi-misi atau https://...">
      <p class="hint">Untuk halaman statis gunakan format <code>/halaman/slug-halaman</code>.</p>
    </div>

    <div class="form-row">
      <div class="form-group">
        <label for="urutan">Urutan</label>
        <input type="number" id="urutan" name="urutan" class="form-control" value="<?= (int) ($menu['urutan'] ?? 0) ?>">
        <p class="hint">Angka lebih kecil tampil lebih dulu.</p>
      </div>

      <div class="form-group">
        <label>&nbsp;</label>
        <div class="form-check" style="margin-top:10px;">
          <input type="checkbox" id="target_blank" name="target_blank" value="1" <?= !empty($menu['target_blank']) ? 'checked' : '' ?>>
          <label for="target_blank" style="margin:0; font-weight:400;">Buka di tab baru</label>
        </div>
        <div class="form-check" style="margin-top:10px;">
          <input type="checkbox" id="is_active" name="is_active" value="1" <?= ($menu['is_active'] ?? 1) ? 'checked' : '' ?>>
          <label for="is_active" style="margin:0; font-weight:400;">Aktifkan menu ini</label>
        </div>
      </div>
    </div>

    <button type="submit" class="btn"><?= $isEdit ? 'Simpan Perubahan' : 'Tambah Menu' ?></button>
  </form>
</div>

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