<?php
session_start();
require __DIR__ . '/includes/admin_lib.php';
include __DIR__ . '/includes/admin_header.php';

$friendsFile = __DIR__ . '/../data/friends.json';
$friends = smurf_read_json($friendsFile, []);

$action = $_GET['action'] ?? '';
$i = isset($_GET['i']) ? (int)$_GET['i'] : -1;

if ($action === 'delete' && $i >= 0) {
  if (!$CAN_EDIT) die("<h1>Access Denied</h1><p>You do not have permission to edit Friends.</p>");
  if (isset($friends[$i])) {
    array_splice($friends, $i, 1);
    smurf_write_json($friendsFile, $friends);
  }
  header("Location: /admin/friends.php");
  exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  if (!$CAN_EDIT) die("<h1>Access Denied</h1><p>You do not have permission to edit Friends.</p>");
  $name  = trim($_POST['name'] ?? '');
  $url   = trim($_POST['url'] ?? '');
  $image = trim($_POST['image'] ?? '');

  if ($name !== '' && $url !== '') {
    $friends[] = ['name'=>$name,'url'=>$url,'image'=>$image];
    smurf_write_json($friendsFile, $friends);
    $saved = true;
  }
}
?>
<section class="content-card">
  <h1 class="admin-title">🤝 Friends</h1>
  <p style="opacity:.8;">Front-end reads: <code>/data/friends.json</code> • Images usually in <code>/img/content/</code></p>

  <?php if (!empty($saved)): ?>
    <div class="msg success">Added.</div>
  <?php endif; ?>

  <div class="admin-wrap" style="margin-top:10px;">
    <section class="content-card" style="text-align:left;">
      <h3 style="margin-top:0;">Add Friend</h3>
      <form method="POST" class="form">
        <label>Name</label>
        <input class="input-field" name="name" required <?= $CAN_EDIT ? '' : 'disabled' ?>>

        <label>URL</label>
        <input class="input-field" name="url" placeholder="https://example.com" required <?= $CAN_EDIT ? '' : 'disabled' ?>>

        <label>Image filename (optional)</label>
        <input class="input-field" name="image" placeholder="friend.png" <?= $CAN_EDIT ? '' : 'disabled' ?>>

        <?php if ($CAN_EDIT): ?>
          <button class="admin-btn primary" type="submit" style="margin-top:12px;">Add Friend</button>
        <?php else: ?>
          <p style="opacity:.7;margin-top:10px;">View-only mode — editing disabled.</p>
        <?php endif; ?>
      </form>
    </section>

    <section class="content-card" style="text-align:left;">
      <h3 style="margin-top:0;">Current Friends</h3>

      <?php if (empty($friends)): ?>
        <p>No friends yet.</p>
      <?php else: ?>
        <?php foreach ($friends as $idx => $fr): ?>
          <div class="content-card" style="text-align:left;">
            <div style="display:flex;justify-content:space-between;gap:10px;align-items:center;">
              <strong><?= smurf_h($fr['name'] ?? '') ?></strong>
              <?php if ($CAN_EDIT): ?>
                <a class="admin-btn" href="/admin/friends.php?action=delete&i=<?= $idx ?>" onclick="return confirm('Delete this friend?');">Delete</a>
              <?php endif; ?>
            </div>
            <p style="margin:8px 0 0;opacity:.9;"><strong>URL:</strong> <?= smurf_h($fr['url'] ?? '') ?></p>
            <?php if (!empty($fr['image'])): ?>
              <p style="margin:4px 0 0;opacity:.9;"><strong>Image:</strong> <?= smurf_h($fr['image']) ?></p>
            <?php endif; ?>
          </div>
        <?php endforeach; ?>
      <?php endif; ?>
    </section>
  </div>
</section>

<?php include __DIR__ . '/includes/admin_footer.php'; ?>
