<?php
$isEdit = !empty($album);
$pageTitle = $isEdit ? 'Kelola Album' : 'Buat Album';
$action = $isEdit ? APP_URL . '/admin/galeri-foto/' . $album['id'] : APP_URL . '/admin/galeri-foto';
?>
<?php require BASE_PATH . '/app/Views/admin/layouts/header.php'; ?>

<div class="page-head">
  <h2><?= $isEdit ? 'Kelola Album: ' . htmlspecialchars($album['judul']) : 'Buat Album Baru' ?></h2>
  <a href="<?= APP_URL ?>/admin/galeri-foto" class="btn btn-secondary">Kembali</a>
</div>

<div class="card" style="max-width:700px; margin-bottom:20px;">
  <h3 style="margin-top:0; font-size:14px;">Informasi Album</h3>
  <form method="POST" action="<?= $action ?>" enctype="multipart/form-data">
    <?= \App\Core\Csrf::field() ?>

    <div class="form-group">
      <label for="judul">Judul Album</label>
      <input type="text" id="judul" name="judul" class="form-control" required
             value="<?= htmlspecialchars($album['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="2"><?= htmlspecialchars($album['deskripsi'] ?? '') ?></textarea>
    </div>

    <div class="form-group">
      <label for="cover">Cover Album</label>
      <?php if ($isEdit && !empty($album['cover'])): ?>
        <div style="margin-bottom:10px;">
          <img src="<?= APP_URL . '/uploads/galeri/' . htmlspecialchars($album['cover']) ?>" alt="" style="width:120px; border-radius:8px; display:block;">
        </div>
      <?php endif; ?>
      <input type="file" id="cover" name="cover" class="form-control" accept="image/jpeg,image/png,image/webp" <?= $isEdit ? '' : 'required' ?>>
      <p class="hint">Gambar sampul yang tampil di daftar album. <?= $isEdit ? 'Kosongkan kalau tidak ingin mengganti.' : '' ?></p>
    </div>

    <button type="submit" class="btn"><?= $isEdit ? 'Simpan Info Album' : 'Buat Album' ?></button>
  </form>
</div>

<?php if ($isEdit): ?>
  <div class="card" style="max-width:700px; margin-bottom:20px;">
    <h3 style="margin-top:0; font-size:14px;">Tambah Foto ke Album</h3>
    <form method="POST" action="<?= APP_URL ?>/admin/galeri-foto/<?= $album['id'] ?>/foto" enctype="multipart/form-data">
      <?= \App\Core\Csrf::field() ?>
      <div class="form-group">
        <label for="foto">Pilih Foto (bisa pilih banyak sekaligus)</label>
        <input type="file" id="foto" name="foto[]" class="form-control" accept="image/jpeg,image/png,image/webp" multiple required>
        <p class="hint">Maksimal 2MB per foto, format JPG/PNG/WEBP.</p>
      </div>
      <button type="submit" class="btn">Upload Foto</button>
    </form>
  </div>

  <div class="card" style="max-width:900px;">
    <h3 style="margin-top:0; font-size:14px;">Foto dalam Album (<?= count($daftarFoto) ?>)</h3>
    <?php if (empty($daftarFoto)): ?>
      <p style="color:var(--admin-muted); font-size:13px;">Belum ada foto. Upload lewat form di atas.</p>
    <?php else: ?>
      <div style="display:grid; grid-template-columns:repeat(auto-fill, minmax(120px, 1fr)); gap:12px;">
        <?php foreach ($daftarFoto as $foto): ?>
          <div style="position:relative;">
            <img src="<?= APP_URL . '/uploads/galeri/' . htmlspecialchars($foto['gambar']) ?>" alt=""
                 style="width:100%; aspect-ratio:1; object-fit:cover; border-radius:8px; border:1px solid var(--admin-border);">
            <form action="<?= APP_URL ?>/admin/galeri-foto/<?= $album['id'] ?>/foto/<?= $foto['id'] ?>/delete" method="POST"
                  onsubmit="return confirm('Hapus foto ini?');" style="position:absolute; top:6px; right:6px;">
              <?= \App\Core\Csrf::field() ?>
              <button type="submit" class="btn btn-danger btn-sm" style="padding:4px 8px;">✕</button>
            </form>
          </div>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>
<?php endif; ?>

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