<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/flash.php';
require_admin();
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
$keys = [
    'home_about_title' => 'About section — main heading',
    'home_about_text' => 'About section — body text',
    'home_highlight_1' => 'Bullet highlight 1',
    'home_highlight_2' => 'Bullet highlight 2',
    'home_highlight_3' => 'Bullet highlight 3',
    'meta_description' => 'Meta description (SEO snippet)',
];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrf_verify($_POST['csrf'] ?? null)) {
    foreach ($keys as $k => $_) {
        $v = (string)($_POST[$k] ?? '');
        db()->prepare('INSERT INTO site_settings (setting_key, setting_value) VALUES (?,?) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)')->execute([$k, $v]);
    }
    admin_flash_set('success', 'Homepage content saved.');
    header('Location: home.php');
    exit;
}
$page = 'home';
$pageTitle = 'Homepage content';
$pageSubtitle = 'Text and bullets for the “About” block on the home page. Hero slides are under Hero banners.';
$breadcrumbs = [['label' => 'Dashboard', 'href' => 'index.php'], ['label' => 'Homepage content', 'href' => null]];
$headerActions = '<a href="../index.php" class="btn btn-outline-primary btn-sm" target="_blank" rel="noopener"><i class="bi bi-box-arrow-up-right me-1"></i>Preview home</a>';
require __DIR__ . '/includes/layout-header.php';
$vals = [];
foreach (array_keys($keys) as $k) {
    $vals[$k] = setting($k, '');
}
?>
<form method="post">
  <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
  <div class="row g-4">
    <div class="col-lg-8">
      <div class="admin-card rounded mb-4">
        <div class="card-header">About section</div>
        <div class="card-body">
          <div class="mb-3">
            <label class="form-label fw-semibold"><?= h($keys['home_about_title']) ?></label>
            <input type="text" name="home_about_title" class="form-control form-control-lg" value="<?= h($vals['home_about_title']) ?>">
          </div>
          <div class="mb-0">
            <label class="form-label fw-semibold"><?= h($keys['home_about_text']) ?></label>
            <textarea name="home_about_text" class="form-control" rows="6"><?= h($vals['home_about_text']) ?></textarea>
          </div>
        </div>
      </div>
    </div>
    <div class="col-lg-4">
      <div class="admin-card rounded mb-4">
        <div class="card-header">Highlights (bullets)</div>
        <div class="card-body">
          <?php foreach (['home_highlight_1', 'home_highlight_2', 'home_highlight_3'] as $hk): ?>
            <div class="mb-3">
              <label class="form-label small text-muted"><?= h($keys[$hk]) ?></label>
              <input type="text" name="<?= h($hk) ?>" class="form-control" value="<?= h($vals[$hk]) ?>">
            </div>
          <?php endforeach; ?>
        </div>
      </div>
      <div class="admin-card rounded mb-4">
        <div class="card-header">SEO</div>
        <div class="card-body">
          <label class="form-label fw-semibold">Meta description</label>
          <textarea name="meta_description" class="form-control" rows="4"><?= h($vals['meta_description']) ?></textarea>
          <div class="form-text">Shown in search engine results (keep under ~160 characters).</div>
        </div>
      </div>
      <button type="submit" class="btn btn-primary btn-lg w-100"><i class="bi bi-check-lg me-1"></i>Save all</button>
    </div>
  </div>
</form>
<?php require __DIR__ . '/includes/layout-footer.php'; ?>
