<?php
require_once('session.php');

$id = $_GET['cuid'];
$sql_1 = mysqli_query($conn,"SELECT * FROM `tb_transaksi` WHERE cuid = '$id'") or die(mysqli_error($conn));
$s1 = mysqli_fetch_array($sql_1);

$usersID = $s1['userID'];
$amounts = $s1['total'];
$trxID   = $s1['kd_transaksi'];

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

$komisie = ($amounts * 20)/100;
$komisi  = round($komisie);

$kode_unik = substr(str_shuffle(1234567890),0,3);
$kd_transaksi = date('Ymds').$kode_unik;
$created_date = date('Y-m-d H:i:s');

if($s1['status'] == 0){

    // update saldo
    $getTO = mysqli_query($conn,"SELECT * FROM `tb_turnover` WHERE trxID = '$trxID'") or die(mysqli_error($conn));
    $goo = mysqli_num_rows($getTO);

    if($goo == 0){
        $totalBalance = $amounts;
        mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$totalBalance' WHERE `userID` = '$usersID'") or die(mysqli_error($conn));
    } else {
        $go = mysqli_fetch_array($getTO);
        $bonus = $go['bonus'];
        $totalBalance = $amounts + $bonus;
        mysqli_query($conn,"UPDATE `tb_turnover` SET `status` = 1 WHERE `trxID` = '$trxID'") or die(mysqli_error($conn));
        mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active + '$totalBalance' WHERE `userID` = '$usersID'") or die(mysqli_error($conn));
    }

    // bonus referral
    $cekRef = mysqli_query($conn,"SELECT * FROM `tb_transaksi` WHERE userID = '$uplineID' AND jenis = 3 AND pay_from = '$usersID' AND status = 1") or die(mysqli_error($conn));
    $cr = mysqli_num_rows($cekRef);

    if($cr == 0){
        mysqli_query($conn,"INSERT INTO `tb_transaksi` 
            (`kd_transaksi`, `date`, `transaksi`, `total`, `saldo`, `note`, `gameid`, `providerID`, `jenis`, `metode`, `pay_from`, `userID`, `status`) 
            VALUES 
            ('$kd_transaksi','$created_date','Refferal','$komisi',0,'Bonus Refferal','','0','3','0','$usersID','$uplineID',1)"
        ) or die(mysqli_error($conn));

        mysqli_query($conn,"UPDATE `tb_balance` SET `active` = active - '$komisi' WHERE `userID` = '$uplineID'") or die(mysqli_error($conn));
    }

    mysqli_query($conn,"UPDATE `tb_transaksi` SET `status` = 1 WHERE `cuid` = '$id'") or die(mysqli_error($conn));

    // ambil data user
    $query = "SELECT * FROM tb_user WHERE cuid='$usersID' LIMIT 1";
    $result = mysqli_query($conn, $query);
    $data = mysqli_fetch_assoc($result);

    $ext = $data['extplayer'];

    // 🔹 API baru user_deposit
    $postArray = [
        "agent_code"  => "terate22",
        "agent_token" => "770cf7d8671955a7b1caa8761756f6af",
        "user_code"   => $ext,
        "amount"      => (int)$amounts
    ];

    $jsonData = json_encode($postArray);
    $headerArray = ['Content-Type: application/json'];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.88xgame.net/api/v2/user_deposit');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

    $res = curl_exec($ch);

    if (curl_errno($ch)) {
        die("cURL Error: " . curl_error($ch));
    }
    curl_close($ch);

    $decode = json_decode($res, true);

    if (!isset($decode['status']) || $decode['status'] != 1) {
        echo "<pre>❌ Deposit API Gagal:\n";
        print_r($decode);
        echo "</pre>";
        exit();
    }
}

header('location:../request_depo/');
?>
