<?php
session_start();

// Obtener datos del error
$errorMessage = $_SESSION['form_errors'] ?? 'Ocurrió un error al procesar tu pedido.';
$formData = $_SESSION['form_data'] ?? [];

// Limpiar datos de sesión
unset($_SESSION['form_errors']);
unset($_SESSION['form_data']);
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Error en el Pedido</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        :root {
            --dominant-color: #B92117;
            --dominant-text: #FFFFFF;
            --yellow: #D4AC0D;
            --orange: #D35400;
            --white: #FFFFFF;
            --light-gray: #F5F5F5;
            --dark-gray: #333333;
            --error-red: #dc3545;
        }
        
        body {
            font-family: 'Poppins', sans-serif;
            background-color: var(--light-gray);
            color: var(--dark-gray);
            margin: 0;
            padding: 0;
        }
        
        .error-container {
            max-width: 600px;
            margin: 50px auto;
            padding: 30px;
            background: var(--white);
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            text-align: center;
        }
        
        .error-animation {
            font-size: 80px;
            color: var(--error-red);
            margin-bottom: 20px;
            animation: shake 0.5s infinite alternate;
        }
        
        @keyframes shake {
            from { transform: translateX(-5px); }
            to { transform: translateX(5px); }
        }
        
        h1 {
            color: var(--error-red);
            margin-bottom: 20px;
        }
        
        .error-message {
            background: #f8d7da;
            border-left: 4px solid var(--error-red);
            padding: 15px;
            margin: 20px 0;
            text-align: left;
            border-radius: 4px;
        }
        
        .error-details {
            background: var(--light-gray);
            padding: 15px;
            border-radius: 8px;
            margin-top: 20px;
            text-align: left;
            font-family: monospace;
            font-size: 12px;
            max-height: 200px;
            overflow-y: auto;
            display: none; /* Oculto por defecto */
        }
        
        .show-details {
            color: var(--dominant-color);
            text-decoration: none;
            font-size: 14px;
            cursor: pointer;
            display: inline-block;
            margin-top: 15px;
        }
        
        .show-details:hover {
            text-decoration: underline;
        }
        
        .action-buttons {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 30px;
        }
        
        .btn {
            padding: 12px 25px;
            border-radius: 30px;
            font-weight: 600;
            cursor: pointer;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s;
        }
        
        .btn-primary {
            background-color: var(--dominant-color);
            color: var(--white);
            border: none;
        }
        
        .btn-primary:hover {
            background-color: var(--orange);
        }
        
        .btn i {
            margin-right: 8px;
        }
        
        @media (max-width: 768px) {
            .error-container {
                margin: 20px;
                padding: 20px;
            }
            
            .action-buttons {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="error-container">
        <div class="error-animation">
            <i class="fas fa-exclamation-triangle"></i>
        </div>
        
        <h1>Error al Procesar el Pedido</h1>
        <p><?= htmlspecialchars($errorMessage) ?></p>
        
        <div class="action-buttons">
            <a href="carrito.php" class="btn btn-primary">
                <i class="fas fa-arrow-left"></i> Volver al Carrito
            </a>
            
            <?php if(!empty($formData)): ?>
            <button id="retryBtn" class="btn btn-secondary">
                <i class="fas fa-redo"></i> Reintentar
            </button>
            <script>
                document.getElementById('retryBtn').addEventListener('click', function() {
                    // Aquí puedes implementar lógica para rellenar el formulario automáticamente
                    alert('Volviendo al carrito con tus datos...');
                    window.location.href = 'carrito.php';
                });
            </script>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>