<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/../main/API/functions.php';

// Ambil IP server
$server_ip = $_SERVER['SERVER_ADDR'] ?? gethostbyname(gethostname());
$client_ip = $_SERVER['REMOTE_ADDR'] ?? 'UNKNOWN';

// Test koneksi API
$test_api = $SGX->agentbalance();

?>
<!DOCTYPE html>
<html>
<head>
    <title>Test Koneksi API</title>
    <style>
        body {
            font-family: Arial;
            background: #111;
            color: #fff;
            padding: 20px;
        }
        .box {
            background: #1e1e1e;
            padding: 20px;
            margin-bottom: 20px;
            border-radius: 10px;
        }
        .success { color: #00ff99; }
        .error { color: #ff4d4d; }
    </style>
</head>
<body>

<h2>🔍 TEST KONEKSI API</h2>

<div class="box">
    <h3>🌐 INFO IP</h3>
    <p><b>IP Server (Whitelist):</b> <?= $server_ip; ?></p>
    <p><b>IP Client (Kamu):</b> <?= $client_ip; ?></p>
</div>

<div class="box">
    <h3>🔑 DATA INTEGRASI</h3>
    <p><b>Agent Code:</b> <?= $SGX->agen; ?></p>
    <p><b>Token:</b> <?= $SGX->token; ?></p>
</div>

<div class="box">
    <h3>📡 HASIL CEK API</h3>

    <?php
    if (!$test_api) {
        echo "<p class='error'>❌ Tidak ada respon dari API (kemungkinan diblokir / IP belum whitelist)</p>";
    } else {
        $status = $test_api['status'] ?? '';
$msg    = strtoupper($test_api['msg'] ?? '');

if ($status == 1 || $msg == 'SUCCESS') {
    echo "<p class='success'>✅ API TERHUBUNG</p>";
} else {
    echo "<p class='error'>❌ API GAGAL</p>";
}

        echo "<p><b>Message:</b> $msg</p>";

        echo "<h4>📦 RESPONSE API:</h4>";
        echo "<pre>";
        print_r($test_api);
        echo "</pre>";
    }
    ?>
</div>

</body>
</html>