<?php
include 'includes/config.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $donacion_id = $_POST['donacion_id'];
    $codigo_transaccion = $_POST['codigo_transaccion'];
    
    // Obtener información de la donación CON IMAGEN
    $sql = "SELECT d.*, i.nombre as item_nombre, i.tipo as item_tipo, i.descripcion as item_descripcion, i.imagen as item_imagen
            FROM donaciones d 
            JOIN items i ON d.item_id = i.id 
            WHERE d.id = ? AND d.codigo_transaccion = ?";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$donacion_id, $codigo_transaccion]);
    $donacion = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$donacion) {
        header("Location: index.php");
        exit();
    }
} else {
    header("Location: index.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Confirmación de Transacción - ADRA</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        /* ========================================== */
        /* VARIABLES DE COLORES - PALETA OFICIAL ADRA */
        /* ========================================== */
        :root {
            --adra-red: #D50032;           /* CHRISTMAS RED */
            --adra-deep-red: #992A31;      /* CHRISTMAS DEEP RED */
            --adra-cream: #F8F0E4;         /* CREAM */
            --adra-gold: #C1A01E;          /* GOLD 20 */
            --adra-pale-blue: #E6F5FA;     /* PALE BLUE */
            --adra-white: #FFFFFF;         /* WHITE */
            --text-dark: #333333;
            --text-medium: #666666;
        }

        /* ========================================== */
        /* RESET Y CONFIGURACIÓN BASE */
        /* ========================================== */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html, body {
            height: 100%;
            overflow: hidden;
        }

        /* ========================================== */
        /* BODY - FONDO CON IMAGEN NAVIDEÑA */
        /* ========================================== */
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: url('fondo-navidad.jpeg') no-repeat center center fixed;
            background-size: cover;
            color: var(--text-dark);
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 8px;
        }

        /* ========================================== */
        /* CONTENEDOR PRINCIPAL */
        /* ========================================== */
        .confirmation-container {
            width: 100%;
            max-width: 400px;
            height: 100%;
            max-height: 98vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        /* ========================================== */
        /* TARJETA DE CONFIRMACIÓN */
        /* ========================================== */
        .confirmation-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.25);
            width: 100%;
            border: 2px solid var(--adra-deep-red);
            display: flex;
            flex-direction: column;
            height: auto;
            max-height: 98vh;
            overflow: hidden;
            backdrop-filter: blur(3px);
        }

        @supports not (backdrop-filter: blur(3px)) {
            .confirmation-card {
                background: rgba(255, 255, 255, 0.98);
            }
        }

        /* ========================================== */
        /* ENCABEZADO CON GRADIENTE ADRA */
        /* ========================================== */
        .confirmation-header {
            background: linear-gradient(135deg, 
                var(--adra-deep-red) 0%, 
                var(--adra-red) 50%, 
                var(--adra-gold) 100%
            );
            color: var(--adra-white);
            padding: 8px 12px;
            text-align: center;
            border-bottom: 2px solid var(--adra-deep-red);
            flex-shrink: 0;
        }

        .confirmation-header h1 {
            font-size: 0.95rem;
            font-weight: 800;
            margin-bottom: 0;
        }

        /* ========================================== */
        /* CUERPO DE LA CONFIRMACIÓN */
        /* ========================================== */
        .confirmation-body {
            padding: 10px;
            display: flex;
            flex-direction: column;
            overflow: hidden;
            flex: 1;
        }

        .content-wrapper {
            display: flex;
            flex-direction: column;
            flex: 1;
            overflow: hidden;
        }

        /* ========================================== */
        /* ICONO DE PREGUNTA */
        /* ========================================== */
        .icon-section {
            text-align: center;
            margin-bottom: 8px;
            flex-shrink: 0;
        }

        .confirmation-icon {
            font-size: 2.5rem;
            color: var(--adra-red);
            margin-bottom: 6px;
        }

        /* ========================================== */
        /* SECCIÓN DE PREGUNTA */
        /* ========================================== */
        .question-section {
            text-align: center;
            margin-bottom: 8px;
            flex-shrink: 0;
        }

        .question-section h2 {
            color: var(--adra-red);
            font-weight: 700;
            margin-bottom: 4px;
            font-size: 0.9rem;
            line-height: 1.2;
        }

        .question-section p {
            color: var(--text-medium);
            font-size: 0.7rem;
            margin-bottom: 0;
            line-height: 1.2;
        }

        /* ========================================== */
        /* INFORMACIÓN DEL BENEFICIARIO */
        /* ========================================== */
        .beneficiary-info {
            text-align: center;
            margin-bottom: 8px;
            padding-bottom: 6px;
            border-bottom: 1px solid var(--adra-cream);
            flex-shrink: 0;
        }

        /* ===== IMAGEN PROTAGONISTA GIGANTE ===== */
        .beneficiary-image {
            width: 130px;
            height: 130px;
            border-radius: 50%;
            overflow: hidden;
            border: 3px solid var(--adra-red);
            margin: 0 auto 6px auto;
            box-shadow: 0 4px 12px rgba(213, 0, 50, 0.2);
            background: linear-gradient(135deg, 
                var(--adra-cream), 
                var(--adra-pale-blue)
            );
        }

        .beneficiary-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .beneficiary-image i {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 3.5rem;
            color: var(--adra-red);
        }

        /* NOMBRE DEL ITEM */
        .beneficiary-details h3 {
            color: var(--adra-red);
            font-weight: 700;
            margin-bottom: 3px;
            font-size: 0.9rem;
            line-height: 1.1;
        }

        .beneficiary-details p {
            color: var(--text-medium);
            margin-bottom: 0;
            font-size: 0.7rem;
            line-height: 1.2;
            max-height: 2em;
            overflow: hidden;
        }

        /* ========================================== */
        /* DETALLES DE LA DONACIÓN */
        /* ========================================== */
        .donation-details {
            background: var(--adra-pale-blue);
            border-radius: 5px;
            padding: 8px;
            margin-bottom: 8px;
            border-left: 2px solid var(--adra-red);
            flex-shrink: 0;
        }

        .detail-item {
            display: flex;
            justify-content: space-between;
            margin-bottom: 4px;
            font-size: 0.75rem;
        }

        .detail-item:last-child {
            margin-bottom: 0;
        }

        .detail-label {
            font-weight: 600;
            color: var(--text-medium);
        }

        .detail-value {
            color: var(--text-dark);
            font-weight: 700;
        }

        /* ========================================== */
        /* BOTONES DE CONFIRMACIÓN */
        /* ========================================== */
        .buttons-section {
            display: flex;
            flex-direction: column;
            gap: 6px;
            margin-bottom: 6px;
            flex-shrink: 0;
        }

        /* Botón de confirmar (Rojo ADRA) */
        .confirm-btn {
            background: linear-gradient(135deg, 
                var(--adra-red) 0%, 
                var(--adra-deep-red) 100%
            );
            color: var(--adra-white);
            border: none;
            border-radius: 6px;
            padding: 10px;
            font-size: 0.8rem;
            font-weight: 700;
            width: 100%;
            transition: all 0.3s ease;
            box-shadow: 0 2px 6px rgba(213, 0, 50, 0.3);
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 4px;
            cursor: pointer;
            position: relative;
        }

        .confirm-btn:hover {
            background: linear-gradient(135deg, 
                var(--adra-deep-red) 0%, 
                var(--adra-red) 100%
            );
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(213, 0, 50, 0.4);
        }

        .confirm-btn:disabled {
            opacity: 0.7;
            cursor: not-allowed;
            transform: none;
        }

        .confirm-btn.processing {
            background: linear-gradient(135deg, 
                #6c757d 0%, 
                #495057 100%
            );
        }

        .confirm-btn .btn-text {
            transition: opacity 0.3s ease;
        }

        .confirm-btn .processing-text {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            opacity: 0;
            transition: opacity 0.3s ease;
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .confirm-btn.processing .btn-text {
            opacity: 0;
        }

        .confirm-btn.processing .processing-text {
            opacity: 1;
        }

        /* Botón de cancelar (Gris neutro) */
        .cancel-btn {
            background: transparent;
            color: var(--text-medium);
            border: 1px solid #ddd;
            border-radius: 6px;
            padding: 10px;
            font-size: 0.8rem;
            font-weight: 600;
            width: 100%;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 4px;
            cursor: pointer;
            position: relative;
        }

        .cancel-btn:hover {
            background: var(--adra-cream);
            border-color: var(--adra-gold);
            transform: translateY(-2px);
        }

        .cancel-btn:disabled {
            opacity: 0.7;
            cursor: not-allowed;
            transform: none;
        }

        .cancel-btn.processing {
            background: linear-gradient(135deg, 
                #6c757d 0%, 
                #495057 100%
            );
            color: var(--adra-white);
            border-color: transparent;
        }

        .cancel-btn .btn-text {
            transition: opacity 0.3s ease;
        }

        .cancel-btn .processing-text {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            opacity: 0;
            transition: opacity 0.3s ease;
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .cancel-btn.processing .btn-text {
            opacity: 0;
        }

        .cancel-btn.processing .processing-text {
            opacity: 1;
        }

        /* ========================================== */
        /* TEXTO DE AYUDA */
        /* ========================================== */
        .help-text {
            text-align: center;
            margin-top: 6px;
            flex-shrink: 0;
        }

        .help-text p {
            color: var(--text-medium);
            font-size: 0.65rem;
            margin-bottom: 0;
        }

        /* ========================================== */
        /* RESPONSIVE - TABLETS */
        /* ========================================== */
        @media (min-width: 768px) {
            body {
                padding: 15px;
            }

            .confirmation-card {
                max-width: 450px;
                border-radius: 12px;
            }

            .beneficiary-image {
                width: 160px;
                height: 160px;
            }

            .beneficiary-image i {
                font-size: 4.5rem;
            }
        }

        /* ========================================== */
        /* RESPONSIVE - MÓVILES PEQUEÑOS */
        /* ========================================== */
        @media (max-width: 360px) {
            .beneficiary-image {
                width: 110px;
                height: 110px;
            }

            .beneficiary-image i {
                font-size: 3rem;
            }
        }

        /* ========================================== */
        /* RESPONSIVE - PANTALLAS BAJAS */
        /* ========================================== */
        @media (max-height: 650px) {
            .confirmation-icon {
                font-size: 2rem;
                margin-bottom: 4px;
            }

            .beneficiary-image {
                width: 100px;
                height: 100px;
                margin-bottom: 4px;
            }

            .beneficiary-image i {
                font-size: 2.5rem;
            }
        }
    </style>
</head>
<body>
    <div class="confirmation-container">
        <div class="confirmation-card">
            <!-- ENCABEZADO -->
            <div class="confirmation-header">
                <h1>CONFIRMACIÓN DE TRANSACCIÓN</h1>
            </div>
            
            <!-- CUERPO -->
            <div class="confirmation-body">
                <div class="content-wrapper">
                    <!-- ICONO DE CONFIRMACIÓN -->
                    <div class="icon-section">
                        <i class="bi bi-question-circle confirmation-icon"></i>
                    </div>

                    <!-- PREGUNTA -->
                    <div class="question-section">
                        <h2>¿Se ha completado la transacción?</h2>
                        <p>Por favor confirma que ya realizaste la transferencia</p>
                    </div>

                    <!-- IMAGEN PROTAGONISTA GIGANTE -->
                    <div class="beneficiary-info">
                        <div class="beneficiary-image">
                            <?php if(!empty($donacion['item_imagen'])): ?>
                                <img src="<?php echo htmlspecialchars($donacion['item_imagen']); ?>" alt="<?php echo htmlspecialchars($donacion['item_nombre']); ?>">
                            <?php else: ?>
                                <i class="bi bi-gift"></i>
                            <?php endif; ?>
                        </div>
                        <div class="beneficiary-details">
                            <h3><?php echo htmlspecialchars($donacion['item_nombre']); ?></h3>
                            <p><?php echo htmlspecialchars($donacion['item_descripcion']); ?></p>
                        </div>
                    </div>

                    <!-- DETALLES DE LA DONACIÓN -->
                    <div class="donation-details">
                        <div class="detail-item">
                            <span class="detail-label">Monto:</span>
                            <span class="detail-value">S/ <?php echo number_format($donacion['monto'], 2); ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Código:</span>
                            <span class="detail-value"><?php echo $donacion['codigo_transaccion']; ?></span>
                        </div>
                    </div>

                    <!-- BOTONES DE CONFIRMACIÓN -->
                    <div class="buttons-section">
                        <form action="agradecimiento.php" method="POST" class="w-100" id="confirmForm">
                            <input type="hidden" name="donacion_id" value="<?php echo $donacion_id; ?>">
                            <input type="hidden" name="codigo_transaccion" value="<?php echo $codigo_transaccion; ?>">
                            <input type="hidden" name="confirmar" value="si">
                            <button type="submit" class="btn confirm-btn" id="confirmBtn">
                                <span class="btn-text">
                                    <i class="bi bi-check-circle-fill"></i> Sí, ya se confirmó
                                </span>
                                <span class="processing-text">
                                    <i class="bi bi-hourglass-split"></i> Procesando...
                                </span>
                            </button>
                        </form>
                        
                        <form action="agradecimiento.php" method="POST" class="w-100" id="cancelForm">
                            <input type="hidden" name="donacion_id" value="<?php echo $donacion_id; ?>">
                            <input type="hidden" name="codigo_transaccion" value="<?php echo $codigo_transaccion; ?>">
                            <input type="hidden" name="confirmar" value="no">
                            <button type="submit" class="btn cancel-btn" id="cancelBtn">
                                <span class="btn-text">
                                    <i class="bi bi-x-circle"></i> No, tengo problemas
                                </span>
                                <span class="processing-text">
                                    <i class="bi bi-hourglass-split"></i> Procesando...
                                </span>
                            </button>
                        </form>
                    </div>

                    <!-- TEXTO DE AYUDA -->
                    <div class="help-text">
                        <p>Si tienes problemas, selecciona "No" para volver atrás</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Crear elemento de audio para el sonido de click
            const clickSound = new Audio('sounds/click.mp3');
            
            const confirmForm = document.getElementById('confirmForm');
            const confirmBtn = document.getElementById('confirmBtn');
            const cancelForm = document.getElementById('cancelForm');
            const cancelBtn = document.getElementById('cancelBtn');
            
            let isProcessing = false;

            // Función para reproducir sonido de click
            function playClickSound() {
                clickSound.currentTime = 0;
                clickSound.play();
            }

            // Función para manejar el envío de formulario
            function handleFormSubmit(form, btn, e) {
                e.preventDefault();
                
                // Si ya está procesando, no hacer nada
                if (isProcessing) return;
                
                // Reproducir sonido de click
                playClickSound();
                
                // Deshabilitar botón y mostrar "Procesando..."
                isProcessing = true;
                btn.disabled = true;
                btn.classList.add('processing');
                
                // Esperar a que termine el sonido (500ms)
                setTimeout(() => {
                    // Enviar el formulario
                    form.submit();
                }, 500);
            }

            // Manejar el botón "Sí, ya pagué"
            confirmForm.addEventListener('submit', function(e) {
                handleFormSubmit(confirmForm, confirmBtn, e);
            });

            // Manejar el botón "No, tengo problemas"
            cancelForm.addEventListener('submit', function(e) {
                handleFormSubmit(cancelForm, cancelBtn, e);
            });

            // Prevenir zoom en iOS
            document.addEventListener('touchstart', function(event) {
                if (event.touches.length > 1) {
                    event.preventDefault();
                }
            }, { passive: false });
        });
    </script>
</body>
</html>