<?php
require '../config/db.php';


// Seguridad: Solo admins (puedes ajustar según tu lógica de roles)
if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
    die("Acceso denegado.");
}

$message = "";

// 1. Procesar la actualización
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $image = $_POST['image_url'];
    $title = $_POST['title'];
    $subtitle = $_POST['subtitle'];
    $slug = $_POST['target_game_slug'];

    $sql = "UPDATE hero_promo SET image_url = ?, title = ?, subtitle = ?, target_game_slug = ? WHERE id = 1";
    $stmt = $pdo->prepare($sql);
    if ($stmt->execute([$image, $title, $subtitle, $slug])) {
        $message = "¡Hero Section actualizada correctamente!";
    }
}

// 2. Obtener datos actuales
$stmt = $pdo->query("SELECT * FROM hero_promo WHERE id = 1");
$current = $stmt->fetch();
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Edit Hero | VVAULT Admin</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body { background: #090b0f; color: white; font-family: 'Inter', sans-serif; padding: 40px; }
        .admin-card { background: #11141b; border: 1px solid #2d333b; padding: 30px; border-radius: 12px; max-width: 600px; margin: 0 auto; }
        .form-group { margin-bottom: 20px; }
        label { display: block; margin-bottom: 8px; color: #8b949e; font-size: 12px; font-weight: bold; text-transform: uppercase; }
        input, textarea { width: 100%; padding: 12px; background: #0d1117; border: 1px solid #2d333b; border-radius: 8px; color: white; box-sizing: border-box; }
        .btn-save { background: #00ff88; color: black; border: none; padding: 15px 30px; border-radius: 8px; font-weight: 800; cursor: pointer; width: 100%; margin-top: 10px; }
        .preview-img { width: 100%; height: 150px; object-fit: cover; border-radius: 8px; margin-top: 10px; border: 1px solid #2d333b; }
        .alert { background: rgba(0,255,136,0.1); color: #00ff88; padding: 15px; border-radius: 8px; margin-bottom: 20px; border: 1px solid #00ff88; }
    </style>
</head>
<body>

<div class="admin-card">
    <h2><i class="fa-solid fa-pen-to-square"></i> Edit Hero Promo</h2>
    
    <?php if($message): ?>
        <div class="alert"><?php echo $message; ?></div>
    <?php endif; ?>

    <form method="POST">
        <div class="form-group">
            <label>Imagen de Fondo (URL)</label>
            <input type="text" name="image_url" value="<?php echo htmlspecialchars($current['image_url']); ?>" placeholder="https://...">
            <img src="<?php echo htmlspecialchars($current['image_url']); ?>" class="preview-img">
        </div>

        <div class="form-group">
            <label>Título Gigante (Usa \n para salto de línea)</label>
            <textarea name="title" rows="2"><?php echo htmlspecialchars($current['title']); ?></textarea>
        </div>

        <div class="form-group">
            <label>Subtítulo (Ej: PRE-ORDER NOW)</label>
            <input type="text" name="subtitle" value="<?php echo htmlspecialchars($current['subtitle']); ?>">
        </div>

        <div class="form-group">
            <label>Game Slug (A dónde redirige al clickear)</label>
            <input type="text" name="target_game_slug" value="<?php echo htmlspecialchars($current['target_game_slug']); ?>" placeholder="ej: elden-ring">
        </div>

        <button type="submit" class="btn-save">GUARDAR CAMBIOS</button>
        <a href="../" style="display:block; text-align:center; color:#8b949e; margin-top:20px; text-decoration:none; font-size:13px;">Ver sitio →</a>
    </form>
</div>

</body>
</html>