<?php
//error_reporting(0);
include('session.php');

$sql_1 = mysqli_query($conn,"SELECT * FROM `tb_ppplayer` WHERE status = 1 ORDER BY cuid DESC") or die(mysqli_error());
$no=0;
while($s1 = mysqli_fetch_array($sql_1)){
  $no++;
  $externalPlayerId = $s1['externalPlayerId'];
  $usersID = $s1['userID'];
  $provider = $s1['provider'];

  $getProvider = mysqli_query($conn,"SELECT * FROM `tb_tripayapi` WHERE provider = '$provider'") or die(mysqli_error($conn));
  $gp = mysqli_fetch_array($getProvider);
  $providerID = $gp['cuid'];
  $urlRequest = $gp['urlRequest'];
  $secureLogin = $gp['api_key'];
  $secretKey = $gp['secret_key'];

  $getTransaksi = mysqli_query($conn,"SELECT * FROM `tb_transaksi` WHERE userID = '$usersID' AND jenis = 5 AND note = 'Transfer to $provider' AND providerID = '$providerID' AND status = 1 ORDER BY cuid DESC LIMIT 1") or die(mysqli_error($conn));
  $gt = mysqli_fetch_array($getTransaksi);

  $getUser = mysqli_query($conn,"SELECT * FROM `tb_user` WHERE cuid = '$usersID'") or die(mysqli_error($conn));
  $gu = mysqli_fetch_array($getUser);

  // 🔹 API 88xgame: Info balance
  $agent_code  = "kingjp";
  $agent_token = "e3a31bed75bf563f606eb4adfe12c29a";
  $headerArray = ['Content-Type: application/json'];

  $postArray = [
      "agent_code"  => $agent_code,
      "agent_token" => $agent_token
  ];

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://api.88xgame.net/api/v2/info");
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postArray));
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  $res = curl_exec($ch);
  curl_close($ch);

  $api88 = json_decode($res, true);
  $userBalance  = $api88['user_balance'] ?? 0;
  $agentBalance = $api88['agent_balance'] ?? 0;
?>
<tr>
    <td class="text-center"><?php echo $no; ?></td>
    <td><?php echo $gu['user']; ?></td>
    <td><?php echo $s1['provider']; ?></td>
    <td class="text-right">Rp. <?php echo number_format($gt['total']); ?></td>
    <td class="text-right" id="chat-content">
        <?php
            // Tampilkan saldo dari API 88xgame
            echo "<div><b>API User:</b> Rp. ".number_format($userBalance)."</div>";
            echo "<div><b>API Server:</b> Rp. ".number_format($agentBalance)."</div>";

            // --- tetap jalankan saldo provider lama ---
            if($provider == 'PragmaticPlay'){
                // ... kode lama Pragmatic ...
            }
            else if($provider == 'Joker'){
                // ... kode lama Joker ...
            }
            else if($provider == 'Habanero'){
                // ... kode lama Habanero ...
            }
            else if($provider == 'PGSoft'){
                // ... kode lama PGSoft ...
            }
        ?>
    </td>
    <td class="text-center">
        <?php
            if($gt['total'] == $userBalance){
                echo '<span class="btn btn-info btn-sm">Draw</span>';
            }
            else if($gt['total'] > $userBalance){
                echo '<span class="btn btn-danger btn-sm">Rungkad</span>';
            }
            else {
                echo '<span class="btn btn-success btn-sm">Cuan</span>';
            }
        ?>
    </td>
    <td class="text-center">
        <a href="<?php echo $urlweb; ?>/function/end-<?php echo $s1['provider']; ?>.php?extplayer=<?php echo $usersID; ?>" class="btn btn-primary btn-sm" onclick="return confirm('Are you sure want End Session this User?');">
            Kick Member
        </a>
    </td>
</tr>
<?php } ?>
