<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TIM CASH - Paiement Carte Virtuelle</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            max-width: 500px;
            width: 100%;
            padding: 40px;
            text-align: center;
        }

        .icon {
            width: 80px;
            height: 80px;
            margin: 0 auto 20px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 40px;
        }

        .icon.success {
            background: #10b981;
            color: white;
        }

        .icon.pending {
            background: #f59e0b;
            color: white;
        }

        .icon.error {
            background: #ef4444;
            color: white;
        }

        h1 {
            color: #1f2937;
            margin-bottom: 10px;
            font-size: 28px;
        }

        .status {
            font-size: 18px;
            color: #6b7280;
            margin-bottom: 30px;
        }

        .details {
            background: #f9fafb;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 30px;
            text-align: left;
        }

        .detail-row {
            display: flex;
            justify-content: space-between;
            padding: 10px 0;
            border-bottom: 1px solid #e5e7eb;
        }

        .detail-row:last-child {
            border-bottom: none;
        }

        .detail-label {
            color: #6b7280;
            font-weight: 500;
        }

        .detail-value {
            color: #1f2937;
            font-weight: 600;
        }

        .card-preview {
            margin: 20px 0;
            padding: 20px;
            border-radius: 15px;
            color: white;
            min-height: 150px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }

        .card-preview.gold {
            background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
        }

        .card-preview.amber {
            background: linear-gradient(135deg, #84cc16 0%, #eab308 100%);
        }

        .card-preview.green {
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
        }

        .card-preview.infinite {
            background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
        }

        .card-logo {
            font-size: 24px;
            font-weight: bold;
            text-align: left;
        }

        .card-number {
            font-size: 20px;
            letter-spacing: 2px;
            margin: 20px 0;
        }

        .card-info {
            display: flex;
            justify-content: space-between;
            font-size: 14px;
        }

        .btn {
            display: inline-block;
            padding: 15px 40px;
            border-radius: 10px;
            text-decoration: none;
            font-weight: 600;
            font-size: 16px;
            transition: all 0.3s;
            border: none;
            cursor: pointer;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4);
        }

        .btn-secondary {
            background: #f3f4f6;
            color: #1f2937;
            margin-left: 10px;
        }

        .btn-secondary:hover {
            background: #e5e7eb;
        }

        .spinner {
            border: 4px solid #f3f4f6;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 20px auto;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .loading-text {
            color: #6b7280;
            margin-top: 10px;
        }

        .footer {
            margin-top: 30px;
            color: #9ca3af;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="loading" style="display: none;">
            <div class="spinner"></div>
            <p class="loading-text">Vérification du paiement...</p>
        </div>

        <div id="content">
            <!-- Le contenu sera injecté par JavaScript -->
        </div>
    </div>

    <script>
        // Récupérer les paramètres de l'URL
        const urlParams = new URLSearchParams(window.location.search);
        const transactionId = urlParams.get('transaction_id');
        const purchaseId = urlParams.get('purchase_id');

        async function checkPaymentStatus() {
            if (!purchaseId) {
                showError('ID de transaction manquant');
                return;
            }

            document.getElementById('loading').style.display = 'block';
            document.getElementById('content').style.display = 'none';

            try {
                const response = await fetch(`/api/virtual-cards/purchase/${purchaseId}`);
                const data = await response.json();

                document.getElementById('loading').style.display = 'none';
                document.getElementById('content').style.display = 'block';

                if (data.status === 'card_created') {
                    showSuccess(data);
                } else if (data.status === 'payment_completed') {
                    showProcessing(data);
                } else if (data.status === 'payment_initiated' || data.status === 'pending') {
                    showPending(data);
                } else if (data.status === 'failed') {
                    showError(data.failure_reason || 'Le paiement a échoué');
                } else {
                    showPending(data);
                }
            } catch (error) {
                document.getElementById('loading').style.display = 'none';
                document.getElementById('content').style.display = 'block';
                showError('Erreur lors de la vérification du paiement');
            }
        }

        function showSuccess(data) {
            const colorNames = {
                'gold': 'Or',
                'amber': 'Ambre',
                'green': 'Vert',
                'infinite': 'Infinite'
            };

            document.getElementById('content').innerHTML = `
                <div class="icon success">✓</div>
                <h1>Paiement Réussi !</h1>
                <p class="status">Votre carte virtuelle a été créée avec succès</p>

                <div class="card-preview ${data.card_color}">
                    <div class="card-logo">TIM CASH</div>
                    <div class="card-number">•••• •••• •••• ••••</div>
                    <div class="card-info">
                        <span>${colorNames[data.card_color] || data.card_color.toUpperCase()}</span>
                        <span>VIRTUELLE</span>
                    </div>
                </div>

                <div class="details">
                    <div class="detail-row">
                        <span class="detail-label">Couleur</span>
                        <span class="detail-value">${colorNames[data.card_color] || data.card_color}</span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Montant</span>
                        <span class="detail-value">${data.total_amount} FCFA</span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Transaction</span>
                        <span class="detail-value">${data.payment_transaction_id}</span>
                    </div>
                </div>

                <button class="btn btn-primary" onclick="returnToApp()">Retour à l'application</button>

                <div class="footer">
                    <p>Merci d'avoir choisi TIM CASH</p>
                </div>
            `;
        }

        function showProcessing(data) {
            document.getElementById('content').innerHTML = `
                <div class="icon pending">⏳</div>
                <h1>Paiement en cours</h1>
                <p class="status">Votre paiement est en cours de traitement</p>

                <div class="details">
                    <div class="detail-row">
                        <span class="detail-label">Montant</span>
                        <span class="detail-value">${data.total_amount} FCFA</span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Transaction</span>
                        <span class="detail-value">${data.payment_transaction_id}</span>
                    </div>
                </div>

                <p style="color: #6b7280; margin: 20px 0;">
                    Votre carte virtuelle sera créée automatiquement une fois le paiement confirmé.
                </p>

                <button class="btn btn-primary" onclick="checkPaymentStatus()">Vérifier à nouveau</button>
                <button class="btn btn-secondary" onclick="returnToApp()">Retour</button>
            `;
        }

        function showPending(data) {
            document.getElementById('content').innerHTML = `
                <div class="icon pending">⏳</div>
                <h1>Paiement en attente</h1>
                <p class="status">Votre paiement est en attente de confirmation</p>

                <div class="details">
                    <div class="detail-row">
                        <span class="detail-label">Montant</span>
                        <span class="detail-value">${data.total_amount} FCFA</span>
                    </div>
                    <div class="detail-row">
                        <span class="detail-label">Transaction</span>
                        <span class="detail-value">${data.payment_transaction_id}</span>
                    </div>
                </div>

                <p style="color: #6b7280; margin: 20px 0;">
                    Veuillez patienter pendant que nous vérifions votre paiement.
                </p>

                <button class="btn btn-primary" onclick="checkPaymentStatus()">Vérifier à nouveau</button>
                <button class="btn btn-secondary" onclick="returnToApp()">Retour</button>
            `;
        }

        function showError(message) {
            document.getElementById('content').innerHTML = `
                <div class="icon error">✕</div>
                <h1>Paiement échoué</h1>
                <p class="status">${message}</p>

                <p style="color: #6b7280; margin: 20px 0;">
                    Veuillez réessayer ou contacter le support si le problème persiste.
                </p>

                <button class="btn btn-primary" onclick="returnToApp()">Retour à l'application</button>

                <div class="footer">
                    <p>Besoin d'aide ? Contactez notre support</p>
                </div>
            `;
        }

        function returnToApp() {
            // Pour l'application mobile, utiliser un deep link
            window.location.href = 'timcash://payment/return?purchase_id=' + purchaseId;
            
            // Fallback après 2 secondes si le deep link ne fonctionne pas
            setTimeout(() => {
                window.close();
            }, 2000);
        }

        // Vérifier le statut au chargement de la page
        window.onload = checkPaymentStatus;

        // Rafraîchir automatiquement toutes les 5 secondes si en attente
        setInterval(() => {
            const content = document.getElementById('content').innerHTML;
            if (content.includes('en attente') || content.includes('en cours')) {
                checkPaymentStatus();
            }
        }, 5000);
    </script>
</body>
</html>

