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

$notice = '';
if (request_method() === 'POST') {
    if (!verify_csrf($_POST['csrf_token'] ?? null)) {
        $notice = '<div class="notice notice--error">Ошибка безопасности.</div>';
    } else {
        $slug = post_text('slug');
        $page = cms_page($slug);
        $page['slug'] = $slug;
        $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">Страница сохранена.</div>';
    }
}

$pages = ['home' => 'Главная', 'policy' => 'Политика', 'rules' => 'Правила проживания'];
ob_start();
echo $notice;
?>
<table class="table panel">
  <thead><tr><th>Страница</th><th>Редактирование</th><th>Публичный URL</th></tr></thead>
  <tbody>
    <?php foreach ($pages as $slug => $label): ?>
      <tr>
        <td><strong><?= e($label) ?></strong><br><span class="badge"><?= e($slug) ?></span></td>
        <td><a class="button button--small" href="/admin/page.php?slug=<?= e($slug) ?>">SEO и мета</a> <a class="button button--small button--ghost" href="/admin/builder.php?page=<?= e($slug) ?>">Блоки</a></td>
        <td><?= e($slug === 'home' ? '/' : '/' . ($slug === 'policy' ? 'politica.php' : 'rools.php')) ?></td>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>
<?php
admin_layout('Страницы', ob_get_clean(), 'pages');
