<?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));
        $existing = null;
        foreach ($items as $item) {
            if ((string)($item['id'] ?? '') === $id) { $existing = $item; break; }
        }
        $image = trim((string)($_POST['image_existing'] ?? ($existing['image'] ?? '')));
        if (!empty($_FILES['image_upload']['name'] ?? '') && is_uploaded_file($_FILES['image_upload']['tmp_name'] ?? '')) {
            $uploaded = ugfx_upload_file($_FILES['image_upload']);
            if ($uploaded) {
                $image = $uploaded;
            }
        }
        $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'] ?? '')),
            'image' => $image,
        ];
        $updated = false;
        foreach ($items as &$item) { if ((string)($item['id'] ?? '') === $id) { $item = array_merge($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" enctype="multipart/form-data"><input type="hidden" name="action" value="save"><input type="hidden" name="id" value="<?= htmlspecialchars((string)($editing['id'] ?? '')) ?>"><input type="hidden" name="image_existing" value="<?= htmlspecialchars((string)($editing['image'] ?? '')) ?>"><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-md-6"><input class="form-control" name="url" placeholder="https://..." value="<?= htmlspecialchars((string)($editing['url'] ?? '')) ?>"></div><div class="col-md-6"><input class="form-control" type="file" name="image_upload" accept=".jpg,.jpeg,.png,.gif,.webp"></div><?php if (!empty($editing['image'])): ?><div class="col-12"><img src="../<?= htmlspecialchars(ltrim((string)$editing['image'], '/')) ?>" alt="Friend image" style="max-width:220px;border-radius:14px;"></div><?php endif; ?><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><?php if (!empty($item['image'])): ?><div class="mb-3"><img src="../<?= htmlspecialchars(ltrim((string)$item['image'], '/')) ?>" alt="<?= htmlspecialchars((string)($item['name'] ?? 'Supporter')) ?>" style="width:88px;height:88px;object-fit:cover;border-radius:18px;"></div><?php endif; ?><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'; ?>