<?php
declare(strict_types=1);
require_once __DIR__ . '/bootstrap.php';
admin_require_login();

$slug = $_GET['slug'] ?? 'home';
$page = cms_page($slug);
$page['slug'] = $slug;
$page['name'] = $page['name'] ?? ($slug === 'home' ? 'Главная' : $slug);
$notice = '';

if (request_method() === 'POST') {
    if (!verify_csrf($_POST['csrf_token'] ?? null)) {
        $notice = '<div class="notice notice--error">Ошибка безопасности.</div>';
    } else {
        $page['name'] = post_text('name');
        $page['seo'] = [
            'title' => post_text('seo_title'),
            'description' => post_text('seo_description'),
            'keywords' => post_text('seo_keywords'),
        ];
        cms_save_page($slug, $page);
        $notice = '<div class="notice">SEO сохранено.</div>';
    }
}

ob_start();
echo $notice;
?>
<form class="panel grid" method="post">
  <input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
  <label><span>Название страницы</span><input name="name" value="<?= e($page['name'] ?? '') ?>"></label>
  <label><span>SEO Title</span><input name="seo_title" value="<?= e($page['seo']['title'] ?? '') ?>"></label>
  <label><span>SEO Description</span><textarea name="seo_description"><?= e($page['seo']['description'] ?? '') ?></textarea></label>
  <label><span>SEO Keywords</span><input name="seo_keywords" value="<?= e($page['seo']['keywords'] ?? '') ?>"></label>
  <button class="button" type="submit">Сохранить</button>
</form>
<?php
admin_layout('SEO страницы: ' . ($page['name'] ?? $slug), ob_get_clean(), 'pages');
