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

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

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

    <div class="form-group">
      <label for="nama">Nama Fasilitas</label>
      <input type="text" id="nama" name="nama" class="form-control" required
             value="<?= htmlspecialchars($fasilitas['nama'] ?? '') ?>" placeholder="Contoh: Laboratorium Komputer">
    </div>

    <div class="form-group">
      <label for="deskripsi">Deskripsi</label>
      <textarea id="deskripsi" name="deskripsi" class="form-control" rows="3"><?= htmlspecialchars($fasilitas['deskripsi'] ?? '') ?></textarea>
    </div>

    <div class="form-row">
      <div class="form-group">
        <label for="icon">Ikon (emoji, opsional)</label>
        <input type="text" id="icon" name="icon" class="form-control" maxlength="10"
               value="<?= htmlspecialchars($fasilitas['icon'] ?? '') ?>" placeholder="🖥️">
        <p class="hint">Dipakai sebagai fallback kalau tidak ada foto.</p>
      </div>
      <div class="form-group">
        <label for="urutan">Urutan</label>
        <input type="number" id="urutan" name="urutan" class="form-control" value="<?= (int) ($fasilitas['urutan'] ?? 0) ?>">
      </div>
    </div>

    <div class="form-group">
      <label for="gambar">Foto Fasilitas</label>
      <?php if ($isEdit && !empty($fasilitas['gambar'])): ?>
        <div style="margin-bottom:10px;">
          <img src="<?= APP_URL . '/uploads/fasilitas/' . htmlspecialchars($fasilitas['gambar']) ?>" alt="" style="width:160px; border-radius:8px; display:block;">
        </div>
      <?php endif; ?>
      <input type="file" id="gambar" name="gambar" class="form-control" accept="image/jpeg,image/png,image/webp">
      <p class="hint">Format JPG/PNG/WEBP, maksimal 2MB. <?= $isEdit ? 'Kosongkan kalau tidak ingin mengganti.' : '' ?></p>
    </div>

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

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