<?php
$isEdit = !empty($agenda);
$pageTitle = $isEdit ? 'Edit Agenda' : 'Tambah Agenda';
$action = $isEdit ? APP_URL . '/admin/agenda/' . $agenda['id'] : APP_URL . '/admin/agenda';
$mulaiValue = $isEdit && !empty($agenda['tanggal_mulai']) ? date('Y-m-d\TH:i', strtotime($agenda['tanggal_mulai'])) : '';
$selesaiValue = $isEdit && !empty($agenda['tanggal_selesai']) ? date('Y-m-d\TH:i', strtotime($agenda['tanggal_selesai'])) : '';
?>
<?php require BASE_PATH . '/app/Views/admin/layouts/header.php'; ?>

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

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

    <div class="form-group">
      <label for="judul">Judul Agenda</label>
      <input type="text" id="judul" name="judul" class="form-control" required
             value="<?= htmlspecialchars($agenda['judul'] ?? '') ?>" placeholder="Contoh: Wisuda Periode I 2026">
    </div>

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

    <div class="form-group">
      <label for="lokasi">Lokasi</label>
      <input type="text" id="lokasi" name="lokasi" class="form-control"
             value="<?= htmlspecialchars($agenda['lokasi'] ?? '') ?>" placeholder="Contoh: Gedung Serbaguna Fakultas">
    </div>

    <div class="form-row">
      <div class="form-group">
        <label for="tanggal_mulai">Tanggal &amp; Waktu Mulai</label>
        <input type="datetime-local" id="tanggal_mulai" name="tanggal_mulai" class="form-control" required value="<?= $mulaiValue ?>">
      </div>
      <div class="form-group">
        <label for="tanggal_selesai">Tanggal &amp; Waktu Selesai (opsional)</label>
        <input type="datetime-local" id="tanggal_selesai" name="tanggal_selesai" class="form-control" value="<?= $selesaiValue ?>">
      </div>
    </div>

    <div class="form-group">
      <label for="gambar">Gambar/Poster (opsional)</label>
      <?php if ($isEdit && !empty($agenda['gambar'])): ?>
        <div style="margin-bottom:10px;">
          <img src="<?= APP_URL . '/uploads/agenda/' . htmlspecialchars($agenda['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 Agenda' ?></button>
  </form>
</div>

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