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

// ==============================
// CONFIG API
// ==============================
$postArray = [
    "method" => "game_list",
    "agent_code" => "jarwok",
    "agent_token" => "858aec6aed15c0733cf9ea9fa9a589f3",
    "provider_code" => "SBTECH", // ganti provider lain kalau mau test
    "type" => "" // kosong = semua
];

$jsonData = json_encode($postArray);

// ==============================
// CURL REQUEST
// ==============================
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.ngaming.xyz');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    die("CURL ERROR: " . curl_error($ch));
}

curl_close($ch);

// decode
$result = json_decode($response, true);

?>

<!DOCTYPE html>
<html>
<head>
    <title>DEBUG GAME LIST API</title>
    <style>
        body {
            background: #111;
            color: #0f0;
            font-family: monospace;
            padding: 20px;
        }
        pre {
            background: #000;
            padding: 15px;
            border-radius: 10px;
            overflow: auto;
            max-height: 400px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        td, th {
            border: 1px solid #333;
            padding: 8px;
            text-align: left;
        }
        th {
            background: #222;
        }
    </style>
</head>
<body>

<h2>🚀 RAW RESPONSE API</h2>
<pre><?php print_r($result); ?></pre>

<?php
// ==============================
// AMBIL DATA GAME LIST
// ==============================
$games = $result['games'] 
    ?? $result['data'] 
    ?? $result['game_list'] 
    ?? [];
?>

<h2>📦 LIST GAME</h2>

<table>
    <tr>
        <th>No</th>
        <th>Game Code</th>
        <th>Game Name</th>
        <th>Game Type</th>
        <th>Provider</th>
    </tr>

<?php
if (!empty($games)) {

    $no = 1;

    foreach ($games as $game) {

        $code = $game['game_code'] 
            ?? $game['code'] 
            ?? '-';

        $name = $game['game_name'] 
            ?? $game['name'] 
            ?? '-';

        $type = $game['game_type'] 
            ?? $game['type'] 
            ?? $game['gameType'] 
            ?? '-';

        $prov = $game['provider_code'] 
            ?? $postArray['provider_code'];

        echo "<tr>
            <td>$no</td>
            <td>$code</td>
            <td>$name</td>
            <td style='color:yellow'>$type</td>
            <td>$prov</td>
        </tr>";

        $no++;
    }

} else {
    echo "<tr><td colspan='5'>❌ Tidak ada data</td></tr>";
}
?>

</table>

</body>
</html>