<?php
include_once __DIR__ . "/function/connect.php";
include_once __DIR__ . "/main/API/functions.php";

$saldo = 0;

// ambil data integrasi
$q = mysqli_query($koneksi, "SELECT * FROM tb_integrasi LIMIT 1");
$d = mysqli_fetch_assoc($q);

if ($d) {

    $response = $SGX->agentbalance();

    if ($response) {

        $status = $response['status'] ?? '';
        $msg    = strtoupper($response['msg'] ?? '');

        if (
            $status == 1 ||
            strtoupper($status) == 'SUCCESS' ||
            strpos($msg, 'SUCCESS') !== false
        ) {
            // ✅ FIX DI SINI
            if (isset($response['agent']['balance'])) {
                $saldo = $response['agent']['balance'];
            }
        }
    }
}

// output ke dashboard
echo number_format($saldo);