<?php
// cms/modals-update.php
header('Content-Type: application/json');

require_once('db_connection.php');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['success' => false, 'message' => 'Método No Permitido']);
    exit;
}

try {
    // Iniciar transacción
    $pdo->beginTransaction();

    // Sanitizar y DECODIFICAR inputs (para evitar el problema de &amp;)
    $floating_badge = strip_tags(htmlspecialchars_decode($_POST['floating-badge'] ?? ''));
    $floating_title = strip_tags(htmlspecialchars_decode($_POST['floating-title'] ?? ''));
    $floating_subtitle = strip_tags(htmlspecialchars_decode($_POST['floating-subtitle'] ?? ''));
    $exit_badge = strip_tags(htmlspecialchars_decode($_POST['exit-badge'] ?? ''));
    $exit_title = strip_tags(htmlspecialchars_decode($_POST['exit-title'] ?? ''));
    $exit_subtitle = strip_tags(htmlspecialchars_decode($_POST['exit-subtitle'] ?? ''));

    $sql = "UPDATE cms_modals SET 
        floating_badge=?, floating_title=?, floating_subtitle=?, 
        exit_badge=?, exit_title=?, exit_subtitle=? 
    WHERE id=1";
    
    $stmt = $pdo->prepare($sql);
    $stmt->execute([
        $floating_badge, $floating_title, $floating_subtitle, 
        $exit_badge, $exit_title, $exit_subtitle
    ]);

    $pdo->commit();
    echo json_encode(['success' => true, 'message' => 'SiteFlow Actualizado Exitosamente']);
} catch (Exception $e) {
    if ($pdo->inTransaction()) {
        $pdo->rollBack();
    }
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => 'Error Al Guardar: ' . $e->getMessage()]);
}
?>