<?php
require_once __DIR__ . '/../includes/functions.php';
requireAdmin();
require __DIR__ . '/includes/ui.php';
$pageTitle = 'SEO';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrfVerify($_POST['csrf_token'] ?? null)) {
$pageKey = $_POST['page_key'] ?? '';
db()->prepare('UPDATE seo_pages SET title=?, meta_description=?, meta_keywords=?, canonical_url=? WHERE page_key=?')->execute([
trim($_POST['title'] ?? ''),
trim($_POST['meta_description'] ?? ''),
trim($_POST['meta_keywords'] ?? ''),
trim($_POST['canonical_url'] ?? ''),
$pageKey,
]);
flash('success', 'SEO settings updated for "' . $pageKey . '".');
redirect('seo.php?page=' . urlencode($pageKey));
}
require __DIR__ . '/includes/admin-header.php';
adminFlashBox();
$pages = db()->query('SELECT * FROM seo_pages ORDER BY id ASC')->fetchAll();
$activeKey = $_GET['page'] ?? ($pages[0]['page_key'] ?? '');
$active = null;
foreach ($pages as $p) { if ($p['page_key'] === $activeKey) $active = $p; }
$active = $active ?? ($pages[0] ?? []);
?>
<div class="flex flex-wrap gap-2 mb-6">
<?php foreach ($pages as $p): ?>
<a href="?page=<?= e($p['page_key']) ?>" class="px-4 py-2 rounded-lg text-sm font-semibold <?= $p['page_key'] === $activeKey ? 'bg-primary text-white' : 'bg-white text-gray-600 border border-gray-200' ?>"><?= e(ucfirst($p['page_key'])) ?></a>
<?php endforeach; ?>
</div>
<form method="post" class="bg-white rounded-2xl border border-gray-100 p-6 max-w-2xl">
<input type="hidden" name="csrf_token" value="<?= e(csrfToken()) ?>" />
<input type="hidden" name="page_key" value="<?= e($active['page_key'] ?? '') ?>" />
<?php inputField('Page Title (<title> tag)', 'title', $active['title'] ?? '', 'text', true); ?>
<?php textareaField('Meta Description', 'meta_description', $active['meta_description'] ?? '', 3); ?>
<?php inputField('Meta Keywords (comma separated)', 'meta_keywords', $active['meta_keywords'] ?? ''); ?>
<?php inputField('Canonical URL', 'canonical_url', $active['canonical_url'] ?? ''); ?>
<button class="btn-primary-brand bg-primary text-white px-6 py-3 rounded-lg font-semibold mt-2">Save SEO Settings</button>
</form>
<?php require __DIR__ . '/includes/admin-footer.php'; ?>