<?php
require_once '../config/config.php';
require_once '../config/db.php';
$page_title = 'Ajouter une configuration SEO';
$current_page = 'admin-add-seo';

$db = getDbConnection();
$success = false;
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $page = trim($_POST['page'] ?? '');
    $title = trim($_POST['title'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $keywords = trim($_POST['keywords'] ?? '');
    if ($page && $title) {
        $stmt = $db->prepare('INSERT INTO seo (page, title, description, keywords) VALUES (?, ?, ?, ?)');
        $success = $stmt->execute([$page, $title, $description, $keywords]);
    } else {
        $error = 'Veuillez renseigner au moins la page et le titre.';
    }
}

$page_content = '<div class="row justify-content-center">'
    . '<div class="col-lg-8 col-md-10">'
    . '<div class="card border border-primary mt-4">'
    . '<div class="card-header"><h4 class="mb-0">Ajouter une configuration SEO</h4></div>'
    . '<div class="card-body">';
if ($success) {
    $page_content .= '<div class="alert alert-success">Configuration SEO ajoutée avec succès !</div>';
}
if ($error) {
    $page_content .= '<div class="alert alert-danger">' . htmlspecialchars($error) . '</div>';
}
$page_content .= '<form method="post">'
    . '<div class="mb-3">'
    . '<label for="page" class="form-label">Page concernée (ex: index.php)</label>'
    . '<input type="text" class="form-control" id="page" name="page" required>'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="title" class="form-label">Titre SEO</label>'
    . '<input type="text" class="form-control" id="title" name="title" required>'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="description" class="form-label">Description SEO</label>'
    . '<textarea class="form-control" id="description" name="description" rows="2"></textarea>'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="keywords" class="form-label">Mots-clés (séparés par des virgules)</label>'
    . '<input type="text" class="form-control" id="keywords" name="keywords">'
    . '</div>'
    . '<button type="submit" class="btn btn-primary">Ajouter</button>'
    . '</form>'
    . '</div></div></div></div>';

include 'template.php';
