<?php
require_once 'config.php';
require_once 'core/autoload.php';
require_once 'core/app/model/EventoData.php';

$id_evento = isset($_GET['id_evento']) ? intval($_GET['id_evento']) : 0;
$evento = $id_evento > 0 ? EventoData::getById($id_evento) : null;

$nombre_evento = $evento ? $evento->nombre_evento : 'Evento';
$fechaTxt = $evento ? strftime('%d de %B de %Y', strtotime($evento->fecha_evento)) : '';
$horaInicio = $evento ? substr($evento->hora_inicio, 0, 5) : '';
$horaFin = $evento ? substr($evento->hora_fin, 0, 5) : '';
$lugar = $evento ? $evento->lugar : '';
$enlace_meet = $evento ? $evento->enlace_meet : '';
$tiene_meet = !empty($enlace_meet);
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Confirmación - <?php echo htmlspecialchars($nombre_evento); ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css">
    <style>
        :root { --primary: #667eea; --primary-dark: #764ba2; --success: #28a745; }
        * { box-sizing: border-box; }
        body {
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            font-family: 'Segoe UI', system-ui, sans-serif;
            margin: 0;
        }
        .confirmation-container {
            background: white;
            border-radius: 16px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
            padding: 40px;
            max-width: 600px;
            width: 100%;
            text-align: center;
        }
        .success-icon {
            font-size: 4rem;
            color: var(--success);
            margin-bottom: 20px;
            animation: bounce 0.6s ease-in-out;
        }
        @keyframes bounce {
            0%, 100% { transform: translateY(0); }
            50% { transform: translateY(-10px); }
        }
        .confirmation-title {
            color: #222;
            font-weight: 700;
            margin-bottom: 10px;
            font-size: 1.8rem;
        }
        .confirmation-subtitle {
            color: #666;
            margin-bottom: 30px;
            font-size: 1rem;
        }
        .data-summary {
            background-color: #f8f9ff;
            border-left: 4px solid var(--primary);
            border-radius: 8px;
            padding: 20px;
            margin: 30px 0;
            text-align: left;
        }
        .data-item {
            display: flex;
            justify-content: space-between;
            padding: 10px 0;
            border-bottom: 1px solid #e8e8e8;
        }
        .data-item:last-child { border-bottom: none; }
        .data-label { font-weight: 600; color: var(--primary); min-width: 140px; }
        .data-value { color: #333; text-align: right; flex-grow: 1; }
        .info-box {
            background-color: #d1fae5;
            border: 1px solid #6ee7b7;
            border-radius: 8px;
            padding: 15px;
            margin: 20px 0;
            color: #065f46;
            font-size: 0.95rem;
        }
        .info-box .icon { margin-right: 8px; }
        .event-details {
            background: linear-gradient(135deg, #e8f4fd 0%, #d6ecff 100%);
            border: 1px solid #a8d4f7;
            border-radius: 12px;
            padding: 20px;
            margin: 20px 0;
            text-align: left;
        }
        .event-details h4 {
            color: #003d66;
            font-weight: 700;
            margin-bottom: 12px;
            font-size: 1.1rem;
        }
        .event-details .detail-item {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 8px 0;
            color: #004a7c;
        }
        .event-details .detail-icon { font-size: 1.2rem; }
        .buttons-group {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 30px;
            flex-wrap: wrap;
        }
        .btn-custom {
            padding: 12px 24px;
            font-size: 0.95rem;
            font-weight: 600;
            border-radius: 8px;
            border: none;
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
            text-decoration: none;
            display: inline-block;
        }
        .btn-custom:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
        }
        .btn-primary { background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%); color: white; }
        .btn-primary:hover { color: white; }
        .btn-secondary { background-color: #f0f0f0; color: #333; }
        .btn-secondary:hover { background-color: #e0e0e0; color: #333; }
        .next-steps {
            margin-top: 30px;
            padding-top: 25px;
            border-top: 2px solid #eee;
            text-align: left;
        }
        .next-steps h4 { color: var(--primary); font-weight: 600; margin-bottom: 15px; }
        .next-steps ul { color: #666; list-style: none; padding: 0; }
        .next-steps li { padding: 8px 0; padding-left: 25px; position: relative; }
        .next-steps li:before { content: '✓'; position: absolute; left: 0; color: var(--success); font-weight: bold; }
        @media (max-width: 576px) {
            .confirmation-container { padding: 25px 20px; }
            .buttons-group { flex-direction: column; }
        }
    </style>
</head>
<body>
    <div class="confirmation-container">
        <div class="success-icon">✅</div>

        <div class="confirmation-title">¡Registro Completado!</div>
        <div class="confirmation-subtitle">Tus datos fueron guardados exitosamente</div>

        <div id="dataSummary" class="data-summary">
        </div>

        <?php if ($evento): ?>
        <div class="event-details">
            <h4>📋 Detalles del Evento</h4>
            <div class="detail-item">
                <span class="detail-icon">📅</span>
                <span><strong>Fecha:</strong> <?php echo $fechaTxt; ?></span>
            </div>
            <div class="detail-item">
                <span class="detail-icon">⏰</span>
                <span><strong>Horario:</strong> <?php echo $horaInicio . ' - ' . $horaFin; ?></span>
            </div>
            <?php if ($lugar): ?>
            <div class="detail-item">
                <span class="detail-icon">📍</span>
                <span><strong>Lugar:</strong> <?php echo htmlspecialchars($lugar); ?></span>
            </div>
            <?php endif; ?>
            <?php if ($tiene_meet): ?>
            <div class="detail-item">
                <span class="detail-icon">🔗</span>
                <a href="<?php echo htmlspecialchars($enlace_meet); ?>" target="_blank">Unirse a la reunión virtual</a>
            </div>
            <?php endif; ?>
        </div>
        <?php endif; ?>

        <div class="info-box">
            <span class="icon">📧</span>
            Se envió una confirmación a tu correo electrónico.
        </div>

        <div class="next-steps">
            <h4>Próximos Pasos:</h4>
            <ul>
                <li>Nos pondremos en contacto contigo pronto</li>
                <li>Revisa tu correo para más información</li>
                <?php if ($lugar): ?>
                <li>Preséntate en <?php echo htmlspecialchars($lugar); ?> el día del evento</li>
                <?php endif; ?>
            </ul>
        </div>

        <div class="buttons-group">
            <?php if ($id_evento > 0): ?>
            <a href="form_virtual.php?id_evento=<?php echo $id_evento; ?>" class="btn-custom btn-secondary">
                📝 Ver Formulario
            </a>
            <?php endif; ?>
            <a href="index.php" class="btn-custom btn-primary">
                🏠 Ir al Inicio
            </a>
        </div>
    </div>

    <script>
        const datosFormulario = JSON.parse(localStorage.getItem('registro_evento_<?php echo $id_evento; ?>') || 'null');

        if (datosFormulario) {
            const dataSummary = document.getElementById('dataSummary');
            let html = '';
            if (datosFormulario.nombre) {
                html += '<div class="data-item"><span class="data-label">Nombre</span><span class="data-value">' + datosFormulario.nombre + '</span></div>';
            }
            if (datosFormulario.correo) {
                html += '<div class="data-item"><span class="data-label">Correo</span><span class="data-value">' + datosFormulario.correo + '</span></div>';
            }
            if (datosFormulario.empresa) {
                html += '<div class="data-item"><span class="data-label">Empresa</span><span class="data-value">' + datosFormulario.empresa + '</span></div>';
            }
            dataSummary.innerHTML = html || '<div class="data-item"><span class="data-value">Registro completado</span></div>';
        } else {
            document.getElementById('dataSummary').innerHTML = '<div class="data-item"><span class="data-value">Tus datos fueron guardados correctamente</span></div>';
        }

        setTimeout(() => {
            localStorage.removeItem('registro_evento_<?php echo $id_evento; ?>');
        }, 5000);
    </script>
</body>
</html>
