<?php
error_reporting(E_ALL);
require_once('session.php');
// Memuat class HGI sesuai referensi topup.php
require_once('../../config/class.hgi.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'];
$jumlah = $s1['total'];

// Membersihkan format angka
$amount_str = str_replace('.', '', $jumlah);
$amounts = (float) $amount_str;

$trxID = $s1['kd_transaksi'];

// Ambil data user
$getUser = mysqli_query($conn, "SELECT * FROM `tb_user` WHERE cuid = '$usersID'") or die(mysqli_error($conn));
$gu = mysqli_fetch_array($getUser);
$usernya = $gu['username'];

$uplineID = $gu['referral_user_id'];
$komisie = 0;
$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');

// Inisialisasi API HGI
$hgi = new HypergateAPI();

if ($s1['status'] == 0) {
    $getTO = mysqli_query($conn, "SELECT * FROM `tb_turnover` WHERE trxID = '$trxID'") or die(mysqli_error($conn));
    $goo = mysqli_num_rows($getTO);

    if ($goo === 0) {
        // --- DEPOSIT TANPA BONUS ---
        $totalBalance = $amounts;
        
        // Eksekusi API HGI
        $res = $hgi->deposit($usernya, (int)$totalBalance);
        
        if ($res['status'] == 1) {
            // Update Database Lokal jika API Berhasil
            mysqli_query($conn, "UPDATE `tb_balance` SET `active` = `active` + '$totalBalance' WHERE `userID` = '$usersID'") or die(mysqli_error($conn));
        } else {
            die("Error HGI Deposit: " . $res['msg']);
        }
        
    } else {
        // --- DEPOSIT DENGAN BONUS ---
        $go = mysqli_fetch_array($getTO);
        $bonus = $go['bonus'];
        $totalBalance = floatval($amounts) + floatval($bonus);
        
        // Eksekusi API HGI
        $res = $hgi->deposit($usernya, (int)$totalBalance);
        
        if ($res['status'] == 1) {
            // Update Status TO dan Balance Lokal
            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));
            mysqli_query($conn, "UPDATE `tb_transaksi` SET `status` = 1 WHERE `userID` = '$usersID' AND `jenis` = '7' AND `kd_transaksi` = '$trxID'");
        } else {
            die("Error HGI Deposit: " . $res['msg']);
        }
    }
    
    sleep(1);

    // Proses Referral / Upline
    $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', 'Referral', '$komisi', 0, 'Bonus Referral', '', '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));
    }
    
    // Update status transaksi utama menjadi sukses
    mysqli_query($conn, "UPDATE `tb_transaksi` SET `status` = 1 WHERE `userID` = '$usersID' AND `jenis` = 1 AND `cuid` = '$id'");
    
    sleep(1);
    header('location:../request_depo/?notif=1');
    exit();
} else {
    header('location:../request_depo/');
    exit();
}
?>