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

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = [
        'page_name' => trim($_POST['page_name'] ?? ''),
        'page_title' => trim($_POST['page_title'] ?? ''),
        'meta_description' => trim($_POST['meta_description'] ?? ''),
        'meta_keywords' => trim($_POST['meta_keywords'] ?? ''),
        'meta_robots' => $_POST['meta_robots'] ?? 'index,follow',
        'canonical_url' => trim($_POST['canonical_url'] ?? ''),
        'og_title' => trim($_POST['og_title'] ?? ''),
        'og_description' => trim($_POST['og_description'] ?? ''),
        'og_image' => trim($_POST['og_image'] ?? ''),
        'og_type' => $_POST['og_type'] ?? 'website',
        'twitter_card' => $_POST['twitter_card'] ?? 'summary_large_image',
        'twitter_title' => trim($_POST['twitter_title'] ?? ''),
        'twitter_description' => trim($_POST['twitter_description'] ?? ''),
        'twitter_image' => trim($_POST['twitter_image'] ?? ''),
        'schema_type' => $_POST['schema_type'] ?? 'WebPage',
        'custom_head' => trim($_POST['custom_head'] ?? ''),
        'status' => $_POST['status'] ?? 'active'
    ];
    if (!$data['page_name'] || !$data['page_title']) {
        $error = 'Le nom et le titre de la page sont obligatoires.';
    } else {
        $stmt = $db->prepare('INSERT INTO seo_pages (page_name, page_title, meta_description, meta_keywords, meta_robots, canonical_url, og_title, og_description, og_image, og_type, twitter_card, twitter_title, twitter_description, twitter_image, schema_type, custom_head, status) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
        $success = $stmt->execute(array_values($data));
    }
}

$page_content = '<div class="row justify-content-center">'
    . '<div class="col-lg-10 col-md-12">'
    . '<div class="card border border-primary mt-4">'
    . '<div class="card-header"><h4 class="mb-0">Ajouter une page SEO</h4></div>'
    . '<div class="card-body">';
if ($success) {
    $page_content .= '<div class="alert alert-success">Page 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_name" class="form-label">Nom de la page (ex: index.php)</label>'
    . '<input type="text" class="form-control" id="page_name" name="page_name" required>'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="page_title" class="form-label">Titre SEO</label>'
    . '<input type="text" class="form-control" id="page_title" name="page_title" required>'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="meta_description" class="form-label">Description SEO</label>'
    . '<textarea class="form-control" id="meta_description" name="meta_description" rows="2"></textarea>'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="meta_keywords" class="form-label">Mots-clés (séparés par des virgules)</label>'
    . '<input type="text" class="form-control" id="meta_keywords" name="meta_keywords">'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="meta_robots" class="form-label">Meta Robots</label>'
    . '<input type="text" class="form-control" id="meta_robots" name="meta_robots" value="index,follow">'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="canonical_url" class="form-label">URL canonique</label>'
    . '<input type="text" class="form-control" id="canonical_url" name="canonical_url">'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="og_title" class="form-label">OG Title</label>'
    . '<input type="text" class="form-control" id="og_title" name="og_title">'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="og_description" class="form-label">OG Description</label>'
    . '<textarea class="form-control" id="og_description" name="og_description" rows="2"></textarea>'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="og_image" class="form-label">OG Image (URL)</label>'
    . '<input type="text" class="form-control" id="og_image" name="og_image">'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="og_type" class="form-label">OG Type</label>'
    . '<input type="text" class="form-control" id="og_type" name="og_type" value="website">'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="twitter_card" class="form-label">Twitter Card</label>'
    . '<input type="text" class="form-control" id="twitter_card" name="twitter_card" value="summary_large_image">'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="twitter_title" class="form-label">Twitter Title</label>'
    . '<input type="text" class="form-control" id="twitter_title" name="twitter_title">'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="twitter_description" class="form-label">Twitter Description</label>'
    . '<textarea class="form-control" id="twitter_description" name="twitter_description" rows="2"></textarea>'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="twitter_image" class="form-label">Twitter Image (URL)</label>'
    . '<input type="text" class="form-control" id="twitter_image" name="twitter_image">'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="schema_type" class="form-label">Schema Type</label>'
    . '<input type="text" class="form-control" id="schema_type" name="schema_type" value="WebPage">'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="custom_head" class="form-label">Custom Head (HTML)</label>'
    . '<textarea class="form-control" id="custom_head" name="custom_head" rows="2"></textarea>'
    . '</div>'
    . '<div class="mb-3">'
    . '<label for="status" class="form-label">Statut</label>'
    . '<select class="form-select" id="status" name="status">'
    . '<option value="active">Active</option>'
    . '<option value="inactive">Inactive</option>'
    . '</select>'
    . '</div>'
    . '<button type="submit" class="btn btn-primary">Ajouter</button>'
    . '</form>'
    . '</div></div></div></div>';

include 'template.php';
