<?php require_once __DIR__ . '/includes/admin_bootstrap.php';
ugfx_require_permission('friends');
$items = ugfx_read_json($ugfx['storage']['friends'], []);
$message = '';
$editing = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? 'save';
    if ($action === 'delete') {
        $id = (string)($_POST['id'] ?? '');
        $items = array_values(array_filter($items, fn($item) => (string)($item['id'] ?? '') !== $id));
        ugfx_write_json($ugfx['storage']['friends'], $items);
        $message = 'Friend/supporter entry deleted.';
    } else {
        $id = trim((string)($_POST['id'] ?? '')) ?: (string)(time() . random_int(100,999));
        $entry = [
            'id' => $id,
            'name' => trim((string)($_POST['name'] ?? '')),
            'type' => trim((string)($_POST['type'] ?? 'Partner')),
            'tagline' => trim((string)($_POST['tagline'] ?? '')),
            'description' => trim((string)($_POST['description'] ?? '')),
            'url' => trim((string)($_POST['url'] ?? '')),
        ];
        $updated = false;
        foreach ($items as &$item) { if ((string)($item['id'] ?? '') === $id) { $item = $entry; $updated = true; break; } }
        unset($item);
        if (!$updated) $items[] = $entry;
        ugfx_write_json($ugfx['storage']['friends'], array_values($items));
        $message = $updated ? 'Friend/supporter entry updated.' : 'Friend/supporter entry created.';
    }
    $items = ugfx_read_json($ugfx['storage']['friends'], []);
}
$editId = (string)($_GET['edit'] ?? '');
foreach ($items as $item) { if ((string)($item['id'] ?? '') === $editId) { $editing = $item; break; } }
$adminTitle='Friends'; include __DIR__ . '/includes/admin_header.php'; ?>
<?php if ($message): ?><div class="ugfx-admin-alert"><?= htmlspecialchars($message) ?></div><?php endif; ?>
<div class="ugfx-admin-grid">
  <div class="ugfx-card p-4"><div class="ugfx-micro-label">FRIENDS / SUPPORTERS</div><h3><?= $editing ? 'Edit supporter card' : 'Add supporter card' ?></h3><form method="post" class="ugfx-admin-form row g-3"><input type="hidden" name="action" value="save"><input type="hidden" name="id" value="<?= htmlspecialchars((string)($editing['id'] ?? '')) ?>"><div class="col-12"><input class="form-control" name="name" placeholder="Name" required value="<?= htmlspecialchars((string)($editing['name'] ?? '')) ?>"></div><div class="col-md-6"><input class="form-control" name="type" placeholder="Type" value="<?= htmlspecialchars((string)($editing['type'] ?? '')) ?>"></div><div class="col-md-6"><input class="form-control" name="tagline" placeholder="Tagline" value="<?= htmlspecialchars((string)($editing['tagline'] ?? '')) ?>"></div><div class="col-12"><input class="form-control" name="url" placeholder="https://..." value="<?= htmlspecialchars((string)($editing['url'] ?? '')) ?>"></div><div class="col-12"><textarea class="form-control" name="description" rows="7" placeholder="Description"><?= htmlspecialchars((string)($editing['description'] ?? '')) ?></textarea></div><div class="col-12 d-flex gap-2 flex-wrap"><button class="btn ugfx-btn-primary">Save card</button><?php if ($editing): ?><a class="btn ugfx-btn-outline" href="friends.php">New card</a><?php endif; ?></div></form></div>
  <div class="ugfx-card p-4"><div class="ugfx-admin-toolbar"><div><div class="ugfx-micro-label">CURRENT RAIL</div><h3 class="mb-0">Friends and supporters</h3></div><span class="ugfx-chip"><?= count($items) ?> cards</span></div><div class="ugfx-admin-list"><?php foreach ($items as $item): ?><div class="ugfx-admin-item"><h4><?= htmlspecialchars((string)($item['name'] ?? '')) ?></h4><div class="ugfx-admin-meta"><span><?= htmlspecialchars((string)($item['type'] ?? '')) ?></span><span><?= htmlspecialchars((string)($item['tagline'] ?? '')) ?></span></div><div class="mt-2 text-secondary"><?= htmlspecialchars((string)($item['description'] ?? '')) ?></div><div class="ugfx-admin-actions"><a class="btn ugfx-btn-outline btn-sm" href="friends.php?edit=<?= urlencode((string)($item['id'] ?? '')) ?>">Edit</a><?php if (!empty($item['url'])): ?><a class="btn ugfx-btn-outline btn-sm" href="<?= htmlspecialchars((string)$item['url']) ?>" target="_blank" rel="noopener">Open link</a><?php endif; ?><form method="post" onsubmit="return confirm('Delete this card?');"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?= htmlspecialchars((string)($item['id'] ?? '')) ?>"><button class="btn btn-outline-danger btn-sm">Delete</button></form></div></div><?php endforeach; ?><?php if (!$items): ?><div class="ugfx-admin-empty">No supporter cards yet.</div><?php endif; ?></div></div>
</div>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>