<?php $pageTitle = 'Tautan: ' . $kolom['judul']; ?>
<?php require BASE_PATH . '/app/Views/admin/layouts/header.php'; ?>

<div class="page-head">
  <h2>Tautan dalam Kolom "<?= htmlspecialchars($kolom['judul']) ?>"</h2>
  <a href="<?= APP_URL ?>/admin/footer" class="btn btn-secondary">Kembali</a>
</div>

<div class="card" style="max-width:600px; margin-bottom:20px;">
  <h3 style="margin-top:0; font-size:14px;">Tambah Tautan</h3>
  <form method="POST" action="<?= APP_URL ?>/admin/footer/<?= $kolom['id'] ?>/tautan/tambah" enctype="multipart/form-data">
    <?= \App\Core\Csrf::field() ?>
    <div class="form-row">
      <div class="form-group">
        <label for="label">Label (opsional)</label>
        <input type="text" id="label" name="label" class="form-control" placeholder="Contoh: Instagram">
      </div>
      <div class="form-group">
        <label for="url">URL</label>
        <input type="text" id="url" name="url" class="form-control" required placeholder="https://... atau /halaman/...">
      </div>
    </div>
    <div class="form-row">
      <div class="form-group">
        <label for="gambar">Gambar/Ikon (opsional)</label>
        <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. Cocok untuk ikon sosial media atau logo mitra.</p>
      </div>
      <div class="form-group">
        <label for="urutan">Urutan</label>
        <input type="number" id="urutan" name="urutan" class="form-control" value="0">
      </div>
    </div>
    <button type="submit" class="btn">Tambah Tautan</button>
  </form>
</div>

<div class="card" style="padding:0; max-width:600px;">
  <table>
    <thead><tr><th>Gambar</th><th>Label</th><th>URL</th><th style="text-align:right;">Aksi</th></tr></thead>
    <tbody>
      <?php if (empty($daftarTautan)): ?>
        <tr><td colspan="4" style="text-align:center; color:var(--admin-muted); padding:20px;">Belum ada tautan.</td></tr>
      <?php endif; ?>
      <?php foreach ($daftarTautan as $tautan): ?>
        <tr>
          <td>
            <?php if (!empty($tautan['gambar'])): ?>
              <img src="<?= APP_URL . '/uploads/footer/' . htmlspecialchars($tautan['gambar']) ?>" alt="" style="width:32px; height:32px; object-fit:cover; border-radius:6px;">
            <?php else: ?>
              <span style="color:var(--admin-muted); font-size:12px;">&mdash;</span>
            <?php endif; ?>
          </td>
          <td><?= $tautan['label'] !== '' ? htmlspecialchars($tautan['label']) : '<span style="color:var(--admin-muted);">(tanpa label)</span>' ?></td>
          <td style="color:var(--admin-muted); max-width:200px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;"><?= htmlspecialchars($tautan['url']) ?></td>
          <td style="text-align:right;">
            <form action="<?= APP_URL ?>/admin/footer/<?= $kolom['id'] ?>/tautan/<?= $tautan['id'] ?>/delete" method="POST"
                  onsubmit="return confirm('Hapus tautan &quot;<?= htmlspecialchars(addslashes($tautan['label'] ?: 'ini')) ?>&quot;?');">
              <?= \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'; ?>
