<?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);
?>
<tr>
    <td class="text-center" style="vertical-align: middle; white-space: normal;"><?php echo $no; ?></td>
    <td style="vertical-align: middle; white-space: normal;"><?php echo $gu['user']; ?></td>
    <td style="vertical-align: middle; white-space: normal;"><?php echo $s1['provider']; ?></td>
    <td class="text-right" style="vertical-align: middle; white-space: normal;">Rp. <?php echo number_format($gt['total']); ?></td>
    <td class="text-right" id="chat-content" style="vertical-align: middle; white-space: normal;">
        <?php
            if($provider == 'PragmaticPlay'){
                $params = 'externalPlayerId='.$externalPlayerId.'&secureLogin='.$secureLogin.$secretKey;
                $hashNeed = md5($params);
                $curl = curl_init();
                                                    
                curl_setopt_array($curl, array(
                    CURLOPT_URL => $urlRequest.'/balance/current',
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                        CURLOPT_TIMEOUT => 0,
                        CURLOPT_FOLLOWLOCATION => true,
                        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                        CURLOPT_CUSTOMREQUEST => "POST",
                        CURLOPT_POSTFIELDS => "secureLogin=".$secureLogin."&externalPlayerId=".$externalPlayerId."&hash=".$hashNeed,
                        CURLOPT_HTTPHEADER => array(
                            "Content-Type: application/x-www-form-urlencoded",
                            "Cache-Control: no-cache"
                        ),
                ));
                                                                       
                $response = curl_exec($curl);
                //echo $response;
                curl_close($curl);
                $hasil = json_decode($response, true);
                $newSaldo = $hasil['balance'];
                echo 'Rp. '.number_format($newSaldo);
            }
            else if($provider == 'Joker'){
                $timestamp = time();
                                        
                $fields = ['Method' => 'GC',
                          'Username' => $userid,
                          'Timestamp' => $timestamp
                         ];
                ksort($fields);
                $signature = urlencode(base64_encode(hash_hmac("sha1", urldecode(http_build_query($fields,'', '&')), $secretKey, TRUE)));
                $curl = curl_init();
                curl_setopt_array($curl, array(
                    CURLOPT_URL => $urlRequest.'?appid='.$secureLogin.'&signature='.$signature,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 0,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "POST",
                    CURLOPT_POSTFIELDS => '{"Method":"GC","Username":"'.$userid.'","Timestamp":"'.$timestamp.'"}',
                    CURLOPT_HTTPHEADER => array(
                        "Content-Type: application/json",
                        "Cache-Control: no-cache"
                    ),
                ));
                                                    
                $response = curl_exec($curl);
                //echo $response;
                curl_close($curl);
                $hasil = json_decode($response, true);
                $newSaldo = $hasil['Credit'];
                echo 'Rp. '.number_format($newSaldo);
                
            }
            else if($provider == 'Habanero'){
                $curl = curl_init();
                    
                curl_setopt_array($curl, array(
                  CURLOPT_URL => $urlRequest.'QueryPlayer',
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_ENCODING => "",
                  CURLOPT_MAXREDIRS => 10,
                  CURLOPT_TIMEOUT => 0,
                  CURLOPT_FOLLOWLOCATION => true,
                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                  CURLOPT_CUSTOMREQUEST => "POST",
                  CURLOPT_POSTFIELDS => '{
                      "BrandId": "'.$secretKey.'", 
                      "APIKey": "'.$secureLogin.'", 
                      "Username": "'.$userid.'",
                      "password":"Testaja123"
                  }',
                  CURLOPT_HTTPHEADER => array(
                      "Content-Type: application/x-www-form-urlencoded",
                      "Cache-Control: no-cache"
                  ),
                ));
                                          
                $response = curl_exec($curl);
                //echo $response;
                curl_close($curl);
                $hasil = json_decode($response, true);
                $newSaldo = $hasil['RealBalance'];
                echo 'Rp. '.number_format($newSaldo);
            }*/
        ?>
    </td>
    <td class="text-center" style="vertical-align: middle; white-space: normal;">
        <?php
            if($gt['total'] == $newSaldo){
                echo '
                    <span class="btn btn-info btn-sm">Draw</span>
                ';
            }
            else if($gt['total'] > $newSaldo){
                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" style="vertical-align: middle; white-space: normal;">
        <a href="<?php echo $urlweb; ?>/function/end-session.php?cuid=<?php echo $s1['externalPlayerId']; ?>" class="btn btn-primary btn-sm" onclick="return confirm('Are you sure want End Session this User?');">
            Kick Member
        </a>
    </td>
</tr>
<?php } ?>