
<?php
session_start(); require __DIR__ . '/includes/header.php';
$dataFile = __DIR__ . '/../data/pages.json'; $pages = admin_read_json($dataFile, []); if (!is_array($pages)) $pages = [];
function slugify($s){ $s = strtolower(trim((string)$s)); $s = preg_replace('/[^a-z0-9\s\-]/', '', $s); $s = preg_replace('/\s+/', '-', $s); $s = preg_replace('/\-+/', '-', $s); return trim($s,'-') ?: 'page'; }
$action = $_GET['action'] ?? ''; $id = trim((string)($_GET['id'] ?? '')); $edit = ($action === 'edit' && isset($pages[$id])) ? $pages[$id] : ['title'=>'','slug'=>'','summary'=>'','content'=>'','button_text'=>'','button_url'=>'','image'=>'','published'=>1,'show_in_nav'=>1];
if ($action === 'delete' && $id !== '' && isset($pages[$id]) && $CAN_EDIT) { unset($pages[$id]); admin_write_json($dataFile, $pages); admin_redirect_self(['deleted'=>1], ['action','id']); }
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $CAN_EDIT) {
  $oldId = trim((string)($_POST['old_id'] ?? '')); $title = trim($_POST['title'] ?? 'Untitled'); $slug = slugify($_POST['slug'] ?? $title); $image = trim($_POST['image'] ?? ''); if ($image === '') { $up = admin_handle_upload('image_file', __DIR__ . '/../img/content'); if ($up !== '') $image = '/img/content/' . $up; }
  $row = ['title'=>$title,'summary'=>trim($_POST['summary'] ?? ''),'content'=>(string)($_POST['content'] ?? ''),'button_text'=>trim($_POST['button_text'] ?? ''),'button_url'=>trim($_POST['button_url'] ?? ''),'image'=>$image,'published'=>!empty($_POST['published']),'show_in_nav'=>!empty($_POST['show_in_nav'])];
  if ($oldId !== '' && isset($pages[$oldId]) && $oldId !== $slug) unset($pages[$oldId]);
  $pages[$slug] = $row; admin_write_json($dataFile, $pages); admin_redirect_self(['saved'=>1], ['action','id']);
}
?>
<section class="content-card"><h1 class="admin-title">🧩 Page Builder</h1><?php if (!empty($_GET['saved'])): ?><div class="msg success">Page saved.</div><?php endif; ?><?php if (!empty($_GET['deleted'])): ?><div class="msg success">Page deleted.</div><?php endif; ?><div class="page-builder-grid"><section class="content-card" style="text-align:left;"><h3 style="margin-top:0;"><?= $action === 'edit' ? 'Edit Page' : 'Create Page' ?></h3><form method="post" enctype="multipart/form-data" class="form"><input type="hidden" name="old_id" value="<?= admin_h($id) ?>"><label>Title</label><input class="input-field" name="title" value="<?= admin_h($edit['title'] ?? '') ?>" required><label>Slug</label><input class="input-field" name="slug" value="<?= admin_h($id ?: ($edit['slug'] ?? '')) ?>" placeholder="summer-dance-party"><label>Summary</label><textarea class="input-field" name="summary" rows="4"><?= admin_h($edit['summary'] ?? '') ?></textarea><label>Hero Image URL</label><input class="input-field" name="image" value="<?= admin_h($edit['image'] ?? '') ?>"><label>Or upload image</label><input class="input-field" type="file" name="image_file" accept="image/*"><label>Content HTML</label><textarea class="input-field" name="content" rows="14" placeholder="<p>Your content here</p>"><?= admin_h($edit['content'] ?? '') ?></textarea><label>Button Text</label><input class="input-field" name="button_text" value="<?= admin_h($edit['button_text'] ?? '') ?>"><label>Button URL</label><input class="input-field" name="button_url" value="<?= admin_h($edit['button_url'] ?? '') ?>"><label style="display:flex;gap:10px;align-items:center;margin:10px 0;"><input type="checkbox" name="published" <?= !empty($edit['published']) ? 'checked' : '' ?>> Published</label><label style="display:flex;gap:10px;align-items:center;margin:10px 0;"><input type="checkbox" name="show_in_nav" <?= array_key_exists('show_in_nav',$edit) ? (!empty($edit['show_in_nav']) ? 'checked' : '') : 'checked' ?>> Save page for navigation use</label><button class="admin-btn primary" type="submit" <?= !$CAN_EDIT ? 'disabled' : '' ?>><?= $action === 'edit' ? 'Save Page' : 'Create Page' ?></button><?php if ($action === 'edit'): ?> <a class="admin-btn" href="/admin/pages.php">Cancel</a><?php endif; ?></form></section><section class="content-card" style="text-align:left;"><h3 style="margin-top:0;">Existing pages</h3><?php if (!$pages): ?><p>No custom pages yet.</p><?php else: foreach ($pages as $slug => $page): ?><div class="page-list-row"><div><strong><?= admin_h($page['title'] ?? $slug) ?></strong><div style="opacity:.8;">/page.php?slug=<?= admin_h($slug) ?></div><div style="opacity:.75;"><?= !empty($page['published']) ? 'Published' : 'Draft' ?></div></div><div style="display:flex;gap:8px;justify-content:flex-end;"><a class="admin-btn" href="/page.php?slug=<?= admin_h($slug) ?>" target="_blank" rel="noopener">View</a><a class="admin-btn" href="/admin/pages.php?action=edit&id=<?= admin_h($slug) ?>">Edit</a><?php if ($CAN_EDIT): ?><a class="admin-btn" href="/admin/pages.php?action=delete&id=<?= admin_h($slug) ?>" onclick="return confirm('Delete this page?');">Delete</a><?php endif; ?></div></div><?php endforeach; endif; ?></section></div></section>
<?php include __DIR__ . '/includes/footer.php'; ?>
