<?php
/**
 * Test File for Geolocation IP Tracker API
 * Author: RSK World
 * Founder: Molla Samser
 * Designer & Tester: Rima Khatun
 * Website: https://rskworld.in
 * Contact: help@rskworld.in, +91 93305 39277
 * Year: 2026
 */

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API Test - Geolocation IP Tracker | RSK World</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 2rem 0;
        }
        .test-container {
            background: rgba(255,255,255,0.95);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            margin: 0 auto;
            max-width: 1200px;
        }
        .test-section {
            padding: 2rem;
            border-bottom: 1px solid #e9ecef;
        }
        .test-section:last-child {
            border-bottom: none;
        }
        .code-block {
            background: #2d3748;
            color: #e2e8f0;
            border-radius: 8px;
            padding: 1rem;
            font-family: 'Courier New', monospace;
            font-size: 0.9rem;
            overflow-x: auto;
        }
        .result-box {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            padding: 1rem;
            margin-top: 1rem;
            max-height: 400px;
            overflow-y: auto;
        }
        .btn-test {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            color: white;
            padding: 0.5rem 1.5rem;
            border-radius: 25px;
            margin: 0.25rem;
        }
        .btn-test:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="test-container">
            <div class="test-section text-center">
                <h1><i class="fas fa-vial me-2"></i>API Testing Dashboard</h1>
                <p class="lead">Test the Geolocation IP Tracker API endpoints</p>
                <p class="mb-0"><strong>RSK World</strong> | © 2026</p>
            </div>
            
            <div class="test-section">
                <h3><i class="fas fa-globe me-2"></i>Test Current IP</h3>
                <p>Get geolocation data for your current IP address</p>
                <button class="btn btn-test" onclick="testCurrentIP()">Test Current IP</button>
                <div class="code-block">GET /index.php</div>
                <div id="currentIPResult" class="result-box"></div>
            </div>
            
            <div class="test-section">
                <h3><i class="fas fa-search me-2"></i>Test Specific IP</h3>
                <p>Test geolocation for a specific IP address</p>
                <div class="row">
                    <div class="col-md-8">
                        <input type="text" id="testIP" class="form-control" placeholder="Enter IP address (e.g., 8.8.8.8)" value="8.8.8.8">
                    </div>
                    <div class="col-md-4">
                        <button class="btn btn-test" onclick="testSpecificIP()">Test IP</button>
                    </div>
                </div>
                <div class="code-block">GET /index.php?ip=8.8.8.8</div>
                <div id="specificIPResult" class="result-box"></div>
            </div>
            
            <div class="test-section">
                <h3><i class="fas fa-list me-2"></i>Test Bulk Lookup</h3>
                <p>Test multiple IPs at once</p>
                <div class="row">
                    <div class="col-md-8">
                        <textarea id="bulkIPs" class="form-control" rows="3" placeholder="Enter IPs (one per line)">8.8.8.8
1.1.1.1
208.67.222.222</textarea>
                    </div>
                    <div class="col-md-4">
                        <button class="btn btn-test" onclick="testBulk()">Test Bulk</button>
                    </div>
                </div>
                <div class="code-block">POST /index.php/bulk
{
    "ips": ["8.8.8.8", "1.1.1.1"]
}</div>
                <div id="bulkResult" class="result-box"></div>
            </div>
            
            <div class="test-section">
                <h3><i class="fas fa-bug me-2"></i>Test Private IPs</h3>
                <p>Test how the API handles private IP addresses</p>
                <div class="row">
                    <div class="col-md-3">
                        <button class="btn btn-test" onclick="testPrivateIP('127.0.0.1')">Loopback</button>
                    </div>
                    <div class="col-md-3">
                        <button class="btn btn-test" onclick="testPrivateIP('192.168.1.1')">Class C</button>
                    </div>
                    <div class="col-md-3">
                        <button class="btn btn-test" onclick="testPrivateIP('10.0.0.1')">Class A</button>
                    </div>
                    <div class="col-md-3">
                        <button class="btn btn-test" onclick="testPrivateIP('172.16.0.1')">Class B</button>
                    </div>
                </div>
                <div id="privateResult" class="result-box"></div>
            </div>
            
            <div class="test-section">
                <h3><i class="fas fa-info-circle me-2"></i>Server Information</h3>
                <div id="serverInfo" class="result-box"></div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function showResult(elementId, data, title) {
            const element = document.getElementById(elementId);
            element.innerHTML = `
                <h5>${title}</h5>
                <pre>${JSON.stringify(data, null, 2)}</pre>
            `;
        }
        
        async function testCurrentIP() {
            try {
                const response = await fetch('index.php');
                const data = await response.json();
                showResult('currentIPResult', data, 'Current IP Result');
            } catch (error) {
                showResult('currentIPResult', {error: error.message}, 'Error');
            }
        }
        
        async function testSpecificIP() {
            const ip = document.getElementById('testIP').value;
            if (!ip) {
                alert('Please enter an IP address');
                return;
            }
            
            try {
                const response = await fetch(`index.php?ip=${encodeURIComponent(ip)}`);
                const data = await response.json();
                showResult('specificIPResult', data, `IP ${ip} Result`);
            } catch (error) {
                showResult('specificIPResult', {error: error.message}, 'Error');
            }
        }
        
        async function testBulk() {
            const ipsText = document.getElementById('bulkIPs').value;
            const ips = ipsText.split('\n').filter(ip => ip.trim());
            
            if (ips.length === 0) {
                alert('Please enter at least one IP address');
                return;
            }
            
            try {
                const response = await fetch('index.php/bulk', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ips: ips})
                });
                const data = await response.json();
                showResult('bulkResult', data, 'Bulk Lookup Result');
            } catch (error) {
                showResult('bulkResult', {error: error.message}, 'Error');
            }
        }
        
        async function testPrivateIP(ip) {
            try {
                const response = await fetch(`index.php?ip=${encodeURIComponent(ip)}`);
                const data = await response.json();
                showResult('privateResult', data, `Private IP ${ip} Result`);
            } catch (error) {
                showResult('privateResult', {error: error.message}, 'Error');
            }
        }
        
        // Load server info on page load
        window.addEventListener('load', async function() {
            try {
                const response = await fetch('index.php');
                const data = await response.json();
                
                const serverInfo = {
                    current_time: new Date().toISOString(),
                    user_agent: navigator.userAgent,
                    api_endpoint: window.location.href,
                    detected_ip: data.debug?.detected_ip || 'Unknown',
                    server_vars: data.debug?.server_vars || {}
                };
                
                showResult('serverInfo', serverInfo, 'Server Information');
            } catch (error) {
                showResult('serverInfo', {error: error.message}, 'Error');
            }
        });
    </script>
</body>
</html>
