<?php
// admin/addon_settings.php
require_once '../config/db.php';


// 1. SEGURIDAD
if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
    header("Location: ../index.php");
    exit;
}

$addon_name = $_GET['name'] ?? 'global_announcements';

// 2. PROCESAR GUARDADO
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['settings'])) {
    try {
        foreach ($_POST['settings'] as $key => $value) {
            $update = $pdo->prepare("UPDATE addon_settings SET setting_value = ? WHERE addon_name = ? AND setting_key = ?");
            $update->execute([$value, $addon_name, $key]);
        }
        // Redirección por JS para evitar errores de cabeceras enviadas por el addon activo
        echo "<script>window.location.href='addon_settings.php?name=$addon_name&success=1';</script>";
        exit;
    } catch (PDOException $e) {
        $error_msg = "Error: " . $e->getMessage();
    }
}

// 3. OBTENER AJUSTES (Aquí estaba el error)
$stmt = $pdo->prepare("SELECT * FROM addon_settings WHERE addon_name = ?");
$stmt->execute([$addon_name]);
// IMPORTANTE: FETCH_ASSOC hace que cada fila sea un array ['id', 'setting_key', 'setting_value']
$settings = $stmt->fetchAll(PDO::FETCH_ASSOC);

include '../includes/header.php';
?>

<div style="max-width: 800px; margin: 40px auto; padding: 0 20px;">
    
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px;">
        <div>
            <h2 style="color: white; margin: 0; font-size: 24px;">Configurar Addon</h2>
            <p style="color: var(--primary); margin: 5px 0 0 0; font-family: monospace; font-size: 14px;">
                Addon: <?php echo htmlspecialchars($addon_name); ?>
            </p>
        </div>
        <a href="addons.php" style="color: var(--text-gray); text-decoration: none; font-size: 13px; border: 1px solid var(--border); padding: 8px 15px; border-radius: 8px;">
            <i class="fa-solid fa-arrow-left"></i> Volver
        </a>
    </div>

    <?php if(isset($_GET['success'])): ?>
        <div style="background: rgba(0, 255, 136, 0.1); border: 1px solid var(--primary); color: var(--primary); padding: 15px; border-radius: 10px; margin-bottom: 20px;">
            <i class="fa-solid fa-check-circle"></i> ¡Cambios guardados y aplicados!
        </div>
    <?php endif; ?>

    <div style="background: var(--nav-bg); border: 1px solid var(--border); border-radius: 15px; padding: 35px;">
        <?php if($settings): ?>
            <form method="POST">
                <?php foreach($settings as $s): ?>
                    <div style="margin-bottom: 30px;">
                        <label style="display: block; color: var(--text-gray); font-size: 11px; text-transform: uppercase; margin-bottom: 12px; font-weight: bold;">
                            <?php echo str_replace('_', ' ', $s['setting_key']); ?>
                        </label>

                        <?php 
                        // LÍNEA 64: Ahora $s['setting_key'] es accesible porque usamos FETCH_ASSOC
                        if (strpos($s['setting_key'], 'color') !== false): 
                        ?>
                            <div style="display: flex; gap: 20px; align-items: center; background: var(--bg-dark); padding: 15px; border-radius: 10px; border: 1px solid var(--border);">
                                <input type="color" name="settings[<?php echo $s['setting_key']; ?>]" value="<?php echo htmlspecialchars($s['setting_value']); ?>" style="width: 55px; height: 55px; cursor: pointer; border: none; background: none;">
                                <span style="color: white; font-family: monospace; font-size: 16px;"><?php echo $s['setting_value']; ?></span>
                            </div>
                        <?php else: ?>
                            <textarea name="settings[<?php echo $s['setting_key']; ?>]" style="width: 100%; background: var(--bg-dark); border: 1px solid var(--border); border-radius: 10px; padding: 15px; color: white; outline: none; font-family: inherit;" rows="3"><?php echo htmlspecialchars($s['setting_value']); ?></textarea>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>

                <button type="submit" style="width: 100%; padding: 18px; background: var(--primary); color: black; border: none; border-radius: 10px; font-weight: 900; cursor: pointer; text-transform: uppercase;">
                    Guardar Configuración
                </button>
            </form>
        <?php else: ?>
            <p style="color: var(--text-gray); text-align: center;">No hay ajustes para este addon en la base de datos.</p>
        <?php endif; ?>
    </div>
</div>

<?php include '../includes/footer.php'; ?>