<?php
declare(strict_types=1);

require_once __DIR__ . '/../../includes/start.php';
require_role(['admin']);

require_once __DIR__ . '/../_helpers.php';
require_once __DIR__ . '/../_sidebar.php';
require_once __DIR__ . '/_lib.php';

$pdo = seo_admin_db();
$me  = seo_admin_me();
$adminUserId = (int)($me['id'] ?? 0);
$settings = seo_admin_load_settings($pdo);
$flash = seo_admin_get_flash();

$title = admin_tt('seo_title', 'Meta-шаблони', 'Meta-шаблоны', 'Meta templates');
$seoAdminActive = 'meta';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!seo_admin_csrf_check_local((string)($_POST['csrf'] ?? ''))) {
        seo_admin_set_flash('danger', seo_admin_t('Помилка CSRF.', 'Ошибка CSRF.', 'CSRF error.'));
        header('Location: ' . seo_admin_url('meta.php'));
        exit;
    }

    $keys = [
        'seo_profile_title_tpl',
        'seo_profile_description_tpl',
        'seo_group_title_tpl',
        'seo_group_description_tpl',
        'seo_post_title_tpl',
        'seo_post_description_tpl',
        'seo_album_title_tpl',
        'seo_album_description_tpl',
    ];

    $payload = [];
    foreach ($keys as $key) {
        $payload[$key] = trim((string)($_POST[$key] ?? ''));
    }

    try {
        $old = $settings;
        seo_admin_save_settings($pdo, $payload);
        seo_admin_add_log($pdo, $adminUserId, 'meta', null, 'meta.save_templates', $old, $payload);
        seo_admin_set_flash('success', seo_admin_t('Meta-шаблони збережено.', 'Meta-шаблоны сохранены.', 'Meta templates saved.'));
    } catch (Throwable $e) {
        seo_admin_set_flash('danger', seo_admin_t('Не вдалося зберегти meta-шаблони.', 'Не удалось сохранить meta-шаблоны.', 'Failed to save meta templates.'));
    }

    header('Location: ' . seo_admin_url('meta.php'));
    exit;
}

require __DIR__ . '/../../includes/header.php';
?>
<div class="row g-3">
  <div class="col-lg-9">
    <?php require __DIR__ . '/_nav.php'; ?>

    <?php if ($flash): ?>
      <div class="alert alert-<?= seo_admin_h((string)($flash['type'] ?? 'info')) ?>">
        <?= seo_admin_h((string)($flash['message'] ?? '')) ?>
      </div>
    <?php endif; ?>

    <div class="card card-app p-3">
      <div class="d-flex align-items-center justify-content-between gap-2 mb-3 flex-wrap">
        <h1 class="h4 mb-0"><?= seo_admin_h($title) ?></h1>
        <div class="text-muted small"><?= seo_admin_h(seo_admin_t('Плейсхолдери: {display_name}, {username}, {group_name}, {author_name}, {post_excerpt}, {album_title}', 'Плейсхолдеры: {display_name}, {username}, {group_name}, {author_name}, {post_excerpt}, {album_title}', 'Placeholders: {display_name}, {username}, {group_name}, {author_name}, {post_excerpt}, {album_title}')) ?></div>
      </div>

      <form method="post">
        <input type="hidden" name="csrf" value="<?= seo_admin_h(seo_admin_csrf_token_local()) ?>">

        <div class="row g-3">
          <div class="col-12 col-xl-6">
            <div class="border rounded p-3 h-100">
              <div class="fw-semibold mb-2"><?= seo_admin_h(seo_admin_t('Профілі', 'Профили', 'Profiles')) ?></div>
              <div class="mb-3">
                <label class="form-label">Title</label>
                <input type="text" class="form-control" name="seo_profile_title_tpl" value="<?= seo_admin_h($settings['seo_profile_title_tpl'] ?? '') ?>">
              </div>
              <div class="mb-0">
                <label class="form-label">Description</label>
                <textarea class="form-control" rows="3" name="seo_profile_description_tpl"><?= seo_admin_h($settings['seo_profile_description_tpl'] ?? '') ?></textarea>
              </div>
            </div>
          </div>
          <div class="col-12 col-xl-6">
            <div class="border rounded p-3 h-100">
              <div class="fw-semibold mb-2"><?= seo_admin_h(seo_admin_t('Групи', 'Группы', 'Groups')) ?></div>
              <div class="mb-3">
                <label class="form-label">Title</label>
                <input type="text" class="form-control" name="seo_group_title_tpl" value="<?= seo_admin_h($settings['seo_group_title_tpl'] ?? '') ?>">
              </div>
              <div class="mb-0">
                <label class="form-label">Description</label>
                <textarea class="form-control" rows="3" name="seo_group_description_tpl"><?= seo_admin_h($settings['seo_group_description_tpl'] ?? '') ?></textarea>
              </div>
            </div>
          </div>
          <div class="col-12 col-xl-6">
            <div class="border rounded p-3 h-100">
              <div class="fw-semibold mb-2"><?= seo_admin_h(seo_admin_t('Пости', 'Посты', 'Posts')) ?></div>
              <div class="mb-3">
                <label class="form-label">Title</label>
                <input type="text" class="form-control" name="seo_post_title_tpl" value="<?= seo_admin_h($settings['seo_post_title_tpl'] ?? '') ?>">
              </div>
              <div class="mb-0">
                <label class="form-label">Description</label>
                <textarea class="form-control" rows="3" name="seo_post_description_tpl"><?= seo_admin_h($settings['seo_post_description_tpl'] ?? '') ?></textarea>
              </div>
            </div>
          </div>
          <div class="col-12 col-xl-6">
            <div class="border rounded p-3 h-100">
              <div class="fw-semibold mb-2"><?= seo_admin_h(seo_admin_t('Фотоальбоми', 'Фотоальбомы', 'Photo albums')) ?></div>
              <div class="mb-3">
                <label class="form-label">Title</label>
                <input type="text" class="form-control" name="seo_album_title_tpl" value="<?= seo_admin_h($settings['seo_album_title_tpl'] ?? '') ?>">
              </div>
              <div class="mb-0">
                <label class="form-label">Description</label>
                <textarea class="form-control" rows="3" name="seo_album_description_tpl"><?= seo_admin_h($settings['seo_album_description_tpl'] ?? '') ?></textarea>
              </div>
            </div>
          </div>
          <div class="col-12">
            <div class="border rounded p-3">
              <div class="fw-semibold mb-2"><?= seo_admin_h(seo_admin_t('Приклади', 'Примеры', 'Examples')) ?></div>
              <ul class="mb-0 small">
                <li><code>{display_name} — Профіль</code></li>
                <li><code>{group_name} — Група</code></li>
                <li><code>Пост від {author_name}</code></li>
                <li><code>{album_title} — Фотоальбом</code></li>
              </ul>
            </div>
          </div>
        </div>

        <div class="mt-3">
          <button class="btn btn-primary" type="submit"><?= seo_admin_h(seo_admin_t('Зберегти шаблони', 'Сохранить шаблоны', 'Save templates')) ?></button>
        </div>
      </form>
    </div>

  </div>
  <div class="col-lg-3">
    <?php admin_sidebar('seo'); ?>
  </div>
</div>
<?php require __DIR__ . '/../../includes/footer.php'; ?>
