<?php
// pages/step3.php

// Inicializa tracking da página
$pageType = 'step3';
$pageId = isset($_GET['id']) ? intval($_GET['id']) : null;
$slug = isset($_GET['slug']) ? sanitizeInput($_GET['slug']) : null;

require_once BASE_PATH . '/includes/config-tracking.php';

// Busca a página no banco de dados
try {
    if ($pageId) {
        // Busca por ID
        $db->query("SELECT * FROM pages WHERE id = :id AND type = :type AND published = 1");
        $db->bind(':id', $pageId);
        $db->bind(':type', $pageType);
    } else if ($slug) {
        // Busca por slug
        $db->query("SELECT * FROM pages WHERE slug = :slug AND type = :type AND published = 1");
        $db->bind(':slug', $slug);
        $db->bind(':type', $pageType);
    } else {
        // Busca a primeira página ativa do tipo step3
        $db->query("SELECT * FROM pages WHERE type = :type AND published = 1 ORDER BY sequence ASC LIMIT 1");
        $db->bind(':type', $pageType);
    }
    
    $page = $db->single();
    
    if (!$page) {
        // Redireciona para página 404 ou exibe mensagem
        die("Página não encontrada");
    }
    
    // Registra visualização da página
    $db->query("INSERT INTO page_views (page_id, visitor_ip, user_agent) 
               VALUES (:page_id, :visitor_ip, :user_agent)");
    $db->bind(':page_id', $page['id']);
    $db->bind(':visitor_ip', $_SERVER['REMOTE_ADDR']);
    $db->bind(':user_agent', $_SERVER['HTTP_USER_AGENT'] ?? '');
    $db->execute();
    
    // Define próxima página no fluxo
    $nextPage = getNextPageInFlow($pageType);
    $nextPageUrl = getPageUrl($nextPage);
    
    // Define página anterior
    $prevPageUrl = getPageUrl('step2');
    
} catch (Exception $e) {
    // Log do erro
    logError("Erro ao carregar página step3: " . $e->getMessage());
    die("Ocorreu um erro ao carregar a página. Por favor, tente novamente mais tarde.");
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($page['title']); ?> - <?php echo SITE_NAME; ?></title>
    
    <!-- Meta Tags -->
    <meta name="description" content="<?php echo htmlspecialchars($page['meta_description']); ?>">
    <meta property="og:title" content="<?php echo htmlspecialchars($page['title']); ?>">
    <meta property="og:description" content="<?php echo htmlspecialchars($page['meta_description']); ?>">
    <?php if (!empty($page['featured_image'])): ?>
    <meta property="og:image" content="<?php echo SITE_URL . htmlspecialchars($page['featured_image']); ?>">
    <?php endif; ?>
    
     <!-- Código de Rastreamento -->
    <?php if (function_exists('addTrackingHeaderCodes')) addTrackingHeaderCodes(); ?>
    
    <!-- Favicon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <style>
        :root {
            --primary-color: #4F46E5;
            --secondary-color: #7C3AED;
            --accent-color: #EC4899;
            --dark-color: #1F2937;
            --light-color: #F8FAFC;
            --text-color: #1F2937;
            --border-color: #E5E7EB;
            --success-color: #10B981;
            --gradient: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
        }
        
        body {
            font-family: 'Poppins', sans-serif;
            line-height: 1.6;
            color: var(--text-color);
            background-color: var(--light-color);
        }
        
        .step-indicator {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .step {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #e0e0e0;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            position: relative;
            z-index: 2;
        }
        
        .step-active {
            background: var(--gradient);
            color: white;
        }
        
        .step-complete {
            background: var(--success-color);
            color: white;
        }
        
        .step-line {
            flex-grow: 1;
            height: 3px;
            background: #e0e0e0;
            margin: 20px 10px 0 10px;
            max-width: 100px;
        }
        
        .step-line-active {
            background: var(--gradient);
        }
        
        .page-header {
            background: var(--gradient);
            color: white;
            padding: 3rem 0;
        }
        
        .page-title {
            font-size: 2.2rem;
            font-weight: 700;
            margin-bottom: 1rem;
        }
        
        .page-subtitle {
            font-size: 1.1rem;
            opacity: 0.9;
        }
        
        .main-content {
            background: white;
            padding: 3rem 0;
            border-radius: 1rem;
            margin-top: -2rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .content-block {
            max-width: 800px;
            margin: 0 auto;
            padding: 0 1.5rem;
        }
        
        .content-block img {
            max-width: 100%;
            height: auto;
            border-radius: 0.5rem;
            margin: 1.5rem 0;
        }
        
        .advanced-tip {
            background: rgba(16, 185, 129, 0.05);
            border: 1px solid rgba(16, 185, 129, 0.2);
            border-radius: 0.5rem;
            padding: 1.5rem;
            margin: 2rem 0;
        }
        
        .advanced-tip h4 {
            color: var(--success-color);
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
        }
        
        .advanced-tip h4 i {
            margin-right: 0.5rem;
        }
        
        .resources-box {
            background: rgba(79, 70, 229, 0.05);
            border: 1px solid rgba(79, 70, 229, 0.2);
            border-radius: 0.5rem;
            padding: 1.5rem;
            margin: 2rem 0;
        }
        
        .resources-box h4 {
            color: var(--primary-color);
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
        }
        
        .resources-box h4 i {
            margin-right: 0.5rem;
        }
        
        .resources-box ul {
            list-style-type: none;
            padding-left: 0;
        }
        
        .resources-box ul li {
            padding: 0.5rem 0;
            border-bottom: 1px solid rgba(79, 70, 229, 0.1);
        }
        
        .resources-box ul li:last-child {
            border-bottom: none;
        }
        
        .resources-box ul li a {
            color: var(--primary-color);
            text-decoration: none;
            display: flex;
            align-items: center;
        }
        
        .resources-box ul li a i {
            margin-right: 0.5rem;
        }
        
        .resources-box ul li a:hover {
            color: var(--secondary-color);
        }
        
        .cta-button {
    display: inline-block;
    background: linear-gradient(90deg, #facc15, #fbbf24); /* degradado amarillo */
    color: #000; /* texto negro para buen contraste */
    font-weight: 600;
    padding: 1rem 2rem;
    border-radius: 0.5rem;
    text-decoration: none;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(250, 204, 21, 0.4);
}

.cta-button:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 25px rgba(250, 204, 21, 0.5);
    color: #000;
}
/* Estilos para tornar as etapas clicáveis */
.step-indicator .step {
    text-decoration: none;
    cursor: pointer;
    transition: all 0.3s ease;
}

.step-indicator .step:hover {
    transform: translateY(-3px);
    box-shadow: 0 5px 15px rgba(79, 70, 229, 0.3);
}

/* Efeito de pulsação para a etapa ativa */
@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(79, 70, 229, 0.4); }
    70% { box-shadow: 0 0 0 10px rgba(79, 70, 229, 0); }
    100% { box-shadow: 0 0 0 0 rgba(79, 70, 229, 0); }
}

.step-indicator .step-active {
    animation: pulse 2s infinite;
}

/* Estilo para etapas futuras */
.step-indicator .step:not(.step-active):not(.step-complete) {
    opacity: 0.7;
}

.step-indicator .step:not(.step-active):not(.step-complete):hover {
    opacity: 1;
}     
  /* Estilos para o botão CTA do WhatsApp */
.whatsapp-cta-container {
    text-align: center;
    margin: 30px 0;
    padding: 25px;
    background-color: #f0fff0;
    border-radius: 12px;
    border: 1px solid #c3e6c3;
}

.whatsapp-title {
    color: #333;
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 20px;
    text-align: center;
}

.whatsapp-cta-button {
    display: inline-block;
    background-color: #25D366;
    color: white !important;
    font-weight: 600;
    font-size: 16px;
    padding: 15px 25px;
    border-radius: 8px;
    text-decoration: none !important;
    transition: all 0.3s ease;
    box-shadow: 0 4px 10px rgba(37, 211, 102, 0.3);
    border: none;
}

.whatsapp-cta-button:hover {
    background-color: #20bf5b;
    transform: translateY(-2px);
    box-shadow: 0 6px 15px rgba(37, 211, 102, 0.4);
}

.whatsapp-cta-button i {
    margin-right: 10px;
    font-size: 20px;
}    

        .navigation-buttons {
            display: flex;
            justify-content: space-between;
            margin-top: 3rem;
        }
        
        .prev-button {
            background: white;
            color: var(--primary-color);
            border: 1px solid var(--primary-color);
            padding: 0.75rem 1.5rem;
            border-radius: 0.5rem;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .prev-button:hover {
            background: rgba(79, 70, 229, 0.05);
            color: var(--primary-color);
        }
        
        .progress-bar-container {
            width: 100%;
            background-color: #e0e0e0;
            border-radius: 1rem;
            margin: 2rem 0;
            height: 10px;
            overflow: hidden;
        }
        
        .progress-bar {
            height: 100%;
            background: var(--gradient);
            width: 75%; /* 75% na terceira etapa */
            border-radius: 1rem;
        }
        
        .footer {
            background: var(--dark-color);
            color: white;
            padding: 2rem 0;
            text-align: center;
            margin-top: 3rem;
        }
        
        .footer a {
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
        }
        
        .footer a:hover {
            color: white;
        }
        
        @media (max-width: 768px) {
            .page-title {
                font-size: 1.8rem;
            }
            
            .page-subtitle {
                font-size: 1rem;
            }
            
            .navigation-buttons {
                flex-direction: column;
                gap: 1rem;
            }
            
            .prev-button, .cta-button {
                text-align: center;
            }
        }
    </style>
</head>
<body>
    <header class="page-header">
        <div class="container">
            <div class="text-center">
                <h1 class="page-title"><?php echo htmlspecialchars($page['title']); ?></h1>
                <p class="page-subtitle"><?php echo htmlspecialchars($page['excerpt']); ?></p>
                 <a href="<?php echo $page['cta_link'] ?: $nextPageUrl; ?>" class="cta-button" onclick="registrarRedirecionamento('proximo', <?php echo $page['id']; ?>)">
                    <?php echo htmlspecialchars($page['cta_text'] ?: 'Acessar Agora'); ?>
                    <i class="fas fa-arrow-right ms-2"></i>
                </a>
            </div>
        </div>
    </header>

     <div class="container">
    <div class="step-indicator">
        <!-- Etapa 1 -->
        <a href="<?php echo getPageUrl('step1'); ?>" class="step <?php echo ($pageType == 'step1') ? 'step-active' : (($pageType == 'step2' || $pageType == 'step3' || $pageType == 'registration') ? 'step-complete' : ''); ?>">
            <?php if($pageType == 'step1'): ?>
                1
            <?php elseif($pageType == 'step2' || $pageType == 'step3' || $pageType == 'registration'): ?>
                <i class="fas fa-check"></i>
            <?php else: ?>
                1
            <?php endif; ?>
        </a>
        
        <div class="step-line <?php echo ($pageType == 'step2' || $pageType == 'step3' || $pageType == 'registration') ? 'step-line-active' : ''; ?>"></div>
        
        <!-- Etapa 2 -->
        <a href="<?php echo getPageUrl('step2'); ?>" class="step <?php echo ($pageType == 'step2') ? 'step-active' : (($pageType == 'step3' || $pageType == 'registration') ? 'step-complete' : ''); ?>">
            <?php if($pageType == 'step2'): ?>
                2
            <?php elseif($pageType == 'step3' || $pageType == 'registration'): ?>
                <i class="fas fa-check"></i>
            <?php else: ?>
                2
            <?php endif; ?>
        </a>
        
        <div class="step-line <?php echo ($pageType == 'step3' || $pageType == 'registration') ? 'step-line-active' : ''; ?>"></div>
        
        <!-- Etapa 3 -->
        <a href="<?php echo getPageUrl('step3'); ?>" class="step <?php echo ($pageType == 'step3') ? 'step-active' : (($pageType == 'registration') ? 'step-complete' : ''); ?>">
            <?php if($pageType == 'step3'): ?>
                3
            <?php elseif($pageType == 'registration'): ?>
                <i class="fas fa-check"></i>
            <?php else: ?>
                3
            <?php endif; ?>
        </a>
        
        <div class="step-line <?php echo ($pageType == 'registration') ? 'step-line-active' : ''; ?>"></div>
        
        <!-- Etapa 4 (Registro) -->
        <a href="<?php echo getPageUrl('registration'); ?>" class="step <?php echo ($pageType == 'registration') ? 'step-active' : ''; ?>">
            4
        </a>
    </div>

        
        <div class="progress-bar-container">
            <div class="progress-bar"></div>
        </div>
        
        <div class="main-content">
            <center> Content1</center><br>
            <div class="content-block">
                <?php echo $page['content']; ?>
                
                <div class="advanced-tip">
    <h4><i class="fas fa-star"></i> Consejos Avanzados</h4>
    <p>Para quienes desean profundizar aún más en el tema:</p>
    <ul>
        <li>Estrategias avanzadas que pocos conocen</li>
        <li>Cómo aplicar estas técnicas en diferentes contextos</li>
        <li>Cómo evitar errores comunes que muchos cometen</li>
    </ul>
</div>

                
               
                <div class="navigation-buttons">
                    <a href="<?php echo $prevPageUrl; ?>" class="prev-button" onclick="registrarRedirecionamento('anterior', <?php echo $page['id']; ?>)">
                        <i class="fas fa-arrow-left me-2"></i> Etapa Anterior
                    </a>
                    <a href="<?php echo $page['cta_link'] ?: $nextPageUrl; ?>" class="cta-button" onclick="registrarRedirecionamento('proximo', <?php echo $page['id']; ?>)">
                        <?php echo htmlspecialchars($page['cta_text'] ?: 'Siguiente Etapa'); ?>
                        <i class="fas fa-arrow-right ms-2"></i>
                    </a>
                </div>
            </div>
        </div>
    </div>

    <footer class="footer">
    <div class="container">
        <p>&copy; <?php echo date('Y'); ?> <?php echo SITE_NAME; ?> - Todos los derechos reservados.</p>
        <p>
            <a href="/terminos-de-uso.php">Términos de Uso</a> | 
            <a href="/politica-de-privacidad.php">Política de Privacidad</a>
        </p>
    </div>
</footer>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>
    // Função para registrar redirecionamentos
    function registrarRedirecionamento(tipo, pageId) {
        fetch('/includes/track.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: 'action=redirect&tipo=' + tipo + '&page_id=' + pageId
        })
        .then(response => response.json())
        .then(data => console.log('Tracking successful:', data))
        .catch(error => console.error('Tracking error:', error));
    }
    // Adicione este JavaScript para rastrear cliques nas etapas
document.addEventListener('DOMContentLoaded', function() {
    const stepLinks = document.querySelectorAll('.step-indicator .step');
    
    stepLinks.forEach(function(link) {
        link.addEventListener('click', function(e) {
            // Obter número da etapa a partir do texto do link ou atributo
            const stepText = this.innerText.trim();
            const currentStep = '<?php echo $pageType; ?>';
            
            // Registrar navegação na etapa
            fetch('/includes/track.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: 'action=step_navigation&from=' + currentStep + '&to=' + this.getAttribute('href') + '&page_id=' + <?php echo $page['id']; ?>
            })
            .then(response => response.json())
            .catch(error => console.error('Tracking error:', error));
        });
    });
});
    </script>
</body>
</html>