<?php
    require_once('session.php');
    if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
    header("HTTP/1.1 403 Forbidden");
    echo "Access forbidden. Use HTTPS.";
    exit();
    }
    $id = $_GET['cuid'];
    $sql_1 = mysqli_query($conn,"SELECT * FROM `tb_transaksi` WHERE cuid = '$id'") or die(mysqli_error());
    $s1 = mysqli_fetch_array($sql_1);
    $usersID = $s1['userID'];
    $amounts = $s1['total'];

    $getUser = mysqli_query($conn,"SELECT * FROM `tb_user` WHERE user = '$usersID'") or die(mysqli_error());
    $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 = mysqli_query($conn,"UPDATE `tb_transaksi` SET `status` = 1 WHERE `cuid` = '$id'") or die(mysqli_error($conn));
        $updateBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `pending` = pending - '$amounts', `payout` = payout + '$amounts' WHERE `userID` = '$usersID'") or die(mysqli_error($conn));
        $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());
        $cr = mysqli_num_rows($cekRef);
        if($cr == 0){
            $cek_usernya = mysqli_query($conn, "SELECT * FROM tb_user WHERE cuid = '$usersID'");
            $data_usernya = mysqli_fetch_array($cek_usernya);
            $username = $data_usernya['extplayer'];
            $insert_transaksi = 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());
            
            }
    }
    header('location:../withdraw/');
    
?>