<?php
/*
desenvolvedor: luciano donizeti
mania de script: www.maniadescript.com.br
whatsapp 43998240911
*/

/**
 * Helper de temas do painel.
 * Cada usuario salva o proprio tema em usuarios.tema_painel, deixando admin,
 * revendedores e sub-revendedores independentes entre si.
 */

function painelTemasDisponiveis() {
    return [
        'azul_noite' => [
            'nome' => 'Azul Noite',
            'descricao' => 'Visual escuro premium com azul vivo.',
            'page_bg' => '#101827',
            'sidebar_bg' => 'rgba(16, 24, 39, 0.94)',
            'card_bg' => 'rgba(30, 41, 59, 0.86)',
            'text_color' => '#f8fafc',
            'text_muted' => '#cbd5e1',
            'accent_color' => '#3b82f6',
            'success_color' => '#22c55e',
            'danger_color' => '#ef4444',
            'warning_color' => '#f59e0b',
            'border_color' => 'rgba(148, 163, 184, 0.22)',
            'shadow_color' => 'rgba(0, 0, 0, 0.35)',
            'gradient' => 'radial-gradient(circle at top left, rgba(59,130,246,.28), transparent 34%), radial-gradient(circle at bottom right, rgba(14,165,233,.20), transparent 38%), linear-gradient(135deg, #0f172a 0%, #111827 100%)',
        ],
        'roxo_neon' => [
            'nome' => 'Roxo Neon',
            'descricao' => 'Roxo intenso com destaque moderno.',
            'page_bg' => '#150f2f',
            'sidebar_bg' => 'rgba(21, 15, 47, 0.94)',
            'card_bg' => 'rgba(49, 32, 91, 0.84)',
            'text_color' => '#fbf7ff',
            'text_muted' => '#d8c9ff',
            'accent_color' => '#a855f7',
            'success_color' => '#34d399',
            'danger_color' => '#fb7185',
            'warning_color' => '#fbbf24',
            'border_color' => 'rgba(216, 201, 255, 0.20)',
            'shadow_color' => 'rgba(36, 12, 74, 0.42)',
            'gradient' => 'radial-gradient(circle at 20% 15%, rgba(168,85,247,.36), transparent 34%), radial-gradient(circle at 85% 80%, rgba(236,72,153,.20), transparent 36%), linear-gradient(135deg, #170f35 0%, #261447 100%)',
        ],
        'verde_esmeralda' => [
            'nome' => 'Verde Esmeralda',
            'descricao' => 'Escuro elegante com verde esportivo.',
            'page_bg' => '#06281f',
            'sidebar_bg' => 'rgba(6, 40, 31, 0.94)',
            'card_bg' => 'rgba(12, 69, 54, 0.84)',
            'text_color' => '#ecfdf5',
            'text_muted' => '#b7f7d7',
            'accent_color' => '#10b981',
            'success_color' => '#22c55e',
            'danger_color' => '#f43f5e',
            'warning_color' => '#f59e0b',
            'border_color' => 'rgba(167, 243, 208, 0.22)',
            'shadow_color' => 'rgba(0, 48, 32, 0.42)',
            'gradient' => 'radial-gradient(circle at 18% 20%, rgba(16,185,129,.34), transparent 32%), radial-gradient(circle at 90% 75%, rgba(20,184,166,.22), transparent 38%), linear-gradient(135deg, #052e22 0%, #064e3b 100%)',
        ],
        'vermelho_rubi' => [
            'nome' => 'Vermelho Rubi',
            'descricao' => 'Layout escuro com vermelho forte.',
            'page_bg' => '#2a0b12',
            'sidebar_bg' => 'rgba(42, 11, 18, 0.94)',
            'card_bg' => 'rgba(86, 24, 37, 0.84)',
            'text_color' => '#fff1f2',
            'text_muted' => '#fecdd3',
            'accent_color' => '#e11d48',
            'success_color' => '#22c55e',
            'danger_color' => '#f43f5e',
            'warning_color' => '#f97316',
            'border_color' => 'rgba(254, 205, 211, 0.22)',
            'shadow_color' => 'rgba(65, 6, 20, 0.44)',
            'gradient' => 'radial-gradient(circle at top left, rgba(225,29,72,.36), transparent 34%), radial-gradient(circle at bottom right, rgba(248,113,113,.18), transparent 36%), linear-gradient(135deg, #2a0b12 0%, #450a17 100%)',
        ],
        'dourado_premium' => [
            'nome' => 'Dourado Premium',
            'descricao' => 'Preto e dourado com cara de painel profissional.',
            'page_bg' => '#17130a',
            'sidebar_bg' => 'rgba(23, 19, 10, 0.95)',
            'card_bg' => 'rgba(45, 36, 17, 0.86)',
            'text_color' => '#fffbeb',
            'text_muted' => '#fde68a',
            'accent_color' => '#d97706',
            'success_color' => '#65a30d',
            'danger_color' => '#dc2626',
            'warning_color' => '#facc15',
            'border_color' => 'rgba(253, 230, 138, 0.22)',
            'shadow_color' => 'rgba(0, 0, 0, 0.45)',
            'gradient' => 'radial-gradient(circle at 18% 18%, rgba(217,119,6,.34), transparent 34%), radial-gradient(circle at 82% 76%, rgba(250,204,21,.16), transparent 36%), linear-gradient(135deg, #11100b 0%, #29210f 100%)',
        ],
        'oceano' => [
            'nome' => 'Oceano',
            'descricao' => 'Azul petróleo com detalhes ciano.',
            'page_bg' => '#082f49',
            'sidebar_bg' => 'rgba(8, 47, 73, 0.94)',
            'card_bg' => 'rgba(14, 78, 107, 0.84)',
            'text_color' => '#ecfeff',
            'text_muted' => '#a5f3fc',
            'accent_color' => '#06b6d4',
            'success_color' => '#2dd4bf',
            'danger_color' => '#fb7185',
            'warning_color' => '#fbbf24',
            'border_color' => 'rgba(165, 243, 252, 0.22)',
            'shadow_color' => 'rgba(0, 42, 64, 0.42)',
            'gradient' => 'radial-gradient(circle at 25% 18%, rgba(6,182,212,.35), transparent 34%), radial-gradient(circle at 78% 82%, rgba(37,99,235,.20), transparent 38%), linear-gradient(135deg, #082f49 0%, #0f4961 100%)',
        ],
        'grafite' => [
            'nome' => 'Grafite',
            'descricao' => 'Cinza escuro minimalista.',
            'page_bg' => '#111315',
            'sidebar_bg' => 'rgba(17, 19, 21, 0.95)',
            'card_bg' => 'rgba(39, 43, 48, 0.86)',
            'text_color' => '#f4f4f5',
            'text_muted' => '#d4d4d8',
            'accent_color' => '#71717a',
            'success_color' => '#22c55e',
            'danger_color' => '#ef4444',
            'warning_color' => '#eab308',
            'border_color' => 'rgba(212, 212, 216, 0.18)',
            'shadow_color' => 'rgba(0, 0, 0, 0.38)',
            'gradient' => 'radial-gradient(circle at 20% 20%, rgba(113,113,122,.30), transparent 34%), radial-gradient(circle at 82% 78%, rgba(82,82,91,.20), transparent 38%), linear-gradient(135deg, #111315 0%, #27272a 100%)',
        ],
        'claro_elegante' => [
            'nome' => 'Claro Elegante',
            'descricao' => 'Tema claro, limpo e profissional.',
            'page_bg' => '#f5f7fb',
            'sidebar_bg' => 'rgba(255, 255, 255, 0.94)',
            'card_bg' => 'rgba(255, 255, 255, 0.90)',
            'text_color' => '#111827',
            'text_muted' => '#475569',
            'accent_color' => '#2563eb',
            'success_color' => '#16a34a',
            'danger_color' => '#dc2626',
            'warning_color' => '#d97706',
            'border_color' => 'rgba(15, 23, 42, 0.12)',
            'shadow_color' => 'rgba(15, 23, 42, 0.12)',
            'gradient' => 'radial-gradient(circle at 18% 18%, rgba(37,99,235,.16), transparent 34%), radial-gradient(circle at 82% 78%, rgba(14,165,233,.14), transparent 38%), linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%)',
        ],
        'magenta' => [
            'nome' => 'Magenta',
            'descricao' => 'Rosa/magenta vibrante para layout chamativo.',
            'page_bg' => '#2a0a25',
            'sidebar_bg' => 'rgba(42, 10, 37, 0.94)',
            'card_bg' => 'rgba(80, 20, 71, 0.84)',
            'text_color' => '#fdf2f8',
            'text_muted' => '#fbcfe8',
            'accent_color' => '#db2777',
            'success_color' => '#10b981',
            'danger_color' => '#f43f5e',
            'warning_color' => '#f59e0b',
            'border_color' => 'rgba(251, 207, 232, 0.22)',
            'shadow_color' => 'rgba(65, 6, 52, 0.42)',
            'gradient' => 'radial-gradient(circle at 20% 18%, rgba(219,39,119,.38), transparent 34%), radial-gradient(circle at 85% 78%, rgba(168,85,247,.22), transparent 38%), linear-gradient(135deg, #2a0a25 0%, #4a1545 100%)',
        ],
        'laranja_sunset' => [
            'nome' => 'Laranja Sunset',
            'descricao' => 'Laranja quente com visual de campanha.',
            'page_bg' => '#271007',
            'sidebar_bg' => 'rgba(39, 16, 7, 0.94)',
            'card_bg' => 'rgba(79, 37, 17, 0.84)',
            'text_color' => '#fff7ed',
            'text_muted' => '#fed7aa',
            'accent_color' => '#f97316',
            'success_color' => '#22c55e',
            'danger_color' => '#e11d48',
            'warning_color' => '#facc15',
            'border_color' => 'rgba(254, 215, 170, 0.22)',
            'shadow_color' => 'rgba(67, 20, 7, 0.42)',
            'gradient' => 'radial-gradient(circle at 16% 20%, rgba(249,115,22,.38), transparent 34%), radial-gradient(circle at 86% 78%, rgba(251,146,60,.18), transparent 38%), linear-gradient(135deg, #271007 0%, #4a1d0b 100%)',
        ],
        'ciano_dark' => [
            'nome' => 'Ciano Dark',
            'descricao' => 'Dark moderno com neon ciano.',
            'page_bg' => '#061b1d',
            'sidebar_bg' => 'rgba(6, 27, 29, 0.95)',
            'card_bg' => 'rgba(14, 48, 52, 0.86)',
            'text_color' => '#ecfeff',
            'text_muted' => '#bae6fd',
            'accent_color' => '#22d3ee',
            'success_color' => '#14b8a6',
            'danger_color' => '#fb7185',
            'warning_color' => '#facc15',
            'border_color' => 'rgba(186, 230, 253, 0.22)',
            'shadow_color' => 'rgba(0, 43, 48, 0.42)',
            'gradient' => 'radial-gradient(circle at 20% 18%, rgba(34,211,238,.34), transparent 34%), radial-gradient(circle at 82% 76%, rgba(45,212,191,.18), transparent 38%), linear-gradient(135deg, #061b1d 0%, #0e3034 100%)',
        ],
        'indigo_pro' => [
            'nome' => 'Índigo Pro',
            'descricao' => 'Azul índigo para visual corporativo.',
            'page_bg' => '#11133a',
            'sidebar_bg' => 'rgba(17, 19, 58, 0.95)',
            'card_bg' => 'rgba(35, 39, 100, 0.86)',
            'text_color' => '#eef2ff',
            'text_muted' => '#c7d2fe',
            'accent_color' => '#6366f1',
            'success_color' => '#22c55e',
            'danger_color' => '#f43f5e',
            'warning_color' => '#f59e0b',
            'border_color' => 'rgba(199, 210, 254, 0.22)',
            'shadow_color' => 'rgba(17, 24, 80, 0.42)',
            'gradient' => 'radial-gradient(circle at 20% 20%, rgba(99,102,241,.36), transparent 34%), radial-gradient(circle at 85% 80%, rgba(59,130,246,.18), transparent 38%), linear-gradient(135deg, #11133a 0%, #252766 100%)',
        ],
    ];
}

function temaPainelPadrao() {
    return 'azul_noite';
}

function normalizarTemaPainel($tema) {
    $tema = preg_replace('/[^a-z0-9_\-]/i', '', (string)$tema);
    $temas = painelTemasDisponiveis();
    return isset($temas[$tema]) ? $tema : temaPainelPadrao();
}

function ensureTemaPainelColumn(PDO $pdo) {
    try {
        $stmt = $pdo->query("SHOW COLUMNS FROM usuarios LIKE 'tema_painel'");
        if (!$stmt || !$stmt->fetch(PDO::FETCH_ASSOC)) {
            $pdo->exec("ALTER TABLE usuarios ADD COLUMN tema_painel VARCHAR(50) NOT NULL DEFAULT 'azul_noite' COMMENT 'Tema visual independente do painel' AFTER bloquear_credenciais");
        }
    } catch (Throwable $e) {
        error_log(date('Y-m-d H:i:s') . " - Erro ao garantir coluna tema_painel: " . $e->getMessage() . "\n", 3, __DIR__ . '/session_debug.log');
    }
}

function conectarTemaPainelPDO() {
    if (!defined('DB_HOST')) {
        require_once __DIR__ . '/db/configdb.php';
    }

    return new PDO(
        "mysql:host=" . DB_HOST . ";port=" . DB_PORT . ";dbname=" . DB_NAME . ";charset=utf8mb4",
        DB_USER,
        DB_PASS,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES => false,
        ]
    );
}

function obterTemaPainelUsuario(PDO $pdo, $user_id) {
    ensureTemaPainelColumn($pdo);
    $stmt = $pdo->prepare("SELECT tema_painel FROM usuarios WHERE id = ? LIMIT 1");
    $stmt->execute([(int)$user_id]);
    $tema = $stmt->fetchColumn();
    return normalizarTemaPainel($tema ?: temaPainelPadrao());
}

function salvarTemaPainelUsuario(PDO $pdo, $user_id, $tema) {
    ensureTemaPainelColumn($pdo);
    $tema = normalizarTemaPainel($tema);
    $stmt = $pdo->prepare("UPDATE usuarios SET tema_painel = ? WHERE id = ?");
    return $stmt->execute([$tema, (int)$user_id]);
}

function temaPainelAtualSeguro() {
    $tema = isset($_SESSION['tema_painel']) ? normalizarTemaPainel($_SESSION['tema_painel']) : temaPainelPadrao();

    if (isset($_SESSION['user_id'])) {
        try {
            $pdo = conectarTemaPainelPDO();
            $tema = obterTemaPainelUsuario($pdo, $_SESSION['user_id']);
            $_SESSION['tema_painel'] = $tema;
        } catch (Throwable $e) {
            error_log(date('Y-m-d H:i:s') . " - Erro ao carregar tema do painel: " . $e->getMessage() . "\n", 3, __DIR__ . '/session_debug.log');
        }
    }

    return normalizarTemaPainel($tema);
}

function cssVarsTemaPainel($tema) {
    $temas = painelTemasDisponiveis();
    $tema = normalizarTemaPainel($tema);
    $dados = $temas[$tema];

    return "--page-bg: {$dados['page_bg']};\n" .
           "--sidebar-bg: {$dados['sidebar_bg']};\n" .
           "--card-bg: {$dados['card_bg']};\n" .
           "--text-color: {$dados['text_color']};\n" .
           "--text-muted: {$dados['text_muted']};\n" .
           "--accent-color: {$dados['accent_color']};\n" .
           "--success-color: {$dados['success_color']};\n" .
           "--danger-color: {$dados['danger_color']};\n" .
           "--warning-color: {$dados['warning_color']};\n" .
           "--theme-border-color: {$dados['border_color']};\n" .
           "--box-shadow: 0 14px 38px {$dados['shadow_color']};\n" .
           "--theme-gradient: {$dados['gradient']};";
}
