<?php
	require_once('session.php');
	require_once('../../api/nexus.php');

	$usersID = $_POST['userID'];
	$amounts = $_POST['nominal'];
	$nominal = $amounts;
	$jenis = $_POST['jenis'];

	$today = date('Y-m-d');
	$sql_3 = mysqli_query($conn,"SELECT * FROM `tb_transaksi` ORDER BY cuid DESC LIMIT 1") or die(mysqli_error($conn));
	$s33 = mysqli_num_rows($sql_3);
	if($s33 == 0){
		$unikID = 0;
	}
	else {
		$s3 = mysqli_fetch_array($sql_3);
		$unikID = $s3['cuid'];
	}
	$no_invoice = 'INV/'.date('y').'/'.date('m').'/'.date('s').$unikID;
	$unik = date('Hs');
	$kode_unik = substr(str_shuffle(1234567890),0,3);
	$orderid = $kode_unik.date('dis');
	$created_date = date('Y-m-d H:i:s');

	$getBalance = mysqli_query($conn,"SELECT * FROM `tb_balance` WHERE userID = '$usersID'") or die(mysqli_error());
	$gb = mysqli_fetch_array($getBalance);
	$saldoAktif = $gb['active'];
	$cek_usernya = mysqli_query($conn, "SELECT * FROM tb_user WHERE cuid = '$usersID'");
	$data_usernya = mysqli_fetch_array($cek_usernya);
	$extplayer = $data_usernya['extplayer'];

	if($jenis == 0){

		$sql_255 = mysqli_query($conn, "SELECT * FROM `tb_apigames` WHERE cuid = 1") or die(mysqli_error($conn));
        $s255 = mysqli_fetch_array($sql_255);
                    
        

		$getBalance = mysqli_query($conn, "SELECT * FROM `tb_balance` WHERE userID = '$usersID'") or die(mysqli_error());
		$gb = mysqli_fetch_array($getBalance);
		$saldoAktif = $gb['active'];

		// Update saldo pengguna
		$newSaldo = $saldoAktif + $amounts;
		$updateBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = '$newSaldo' WHERE `userID` = '$usersID'") or die(mysqli_error($conn));
	//echo $response;
		header('location:../balance/?notif=1');
	}
	else if($jenis == 2){
		$newSaldo = $saldoAktif + $amounts;
		$updateBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = '$newSaldo' WHERE `userID` = '$usersID'") or die(mysqli_error($conn));
		$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 ('$no_invoice','$created_date','Bonus Saldo','$amounts',0,'Bonus','', '1', '1','1','0','$usersID',1)") or die(mysqli_error());
		$update_balaces = mysqli_query($conn,"UPDATE `tb_seo` SET coin = coin - '$amounts' WHERE cuid = '1'") or die(mysqli_error());
		header('location:../balance/?notif=1');
	}
	else {

		$sql_255 = mysqli_query($conn, "SELECT * FROM `tb_apigames` WHERE cuid = 1") or die(mysqli_error($conn));
        $s255 = mysqli_fetch_array($sql_255);
                    
        

		$getBalance = mysqli_query($conn, "SELECT * FROM `tb_balance` WHERE userID = '$usersID'") or die(mysqli_error());
		$gb = mysqli_fetch_array($getBalance);
		$saldoAktif = $gb['active'];

		// Update saldo pengguna
		$newSaldo = $saldoAktif - $amounts;
		$updateBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = '$newSaldo' WHERE `userID` = '$usersID'") or die(mysqli_error($conn));
	//echo $response;
		header('location:../balance/?notif=1');
	}

	?>