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

// Ambil data dari POS
$metode = htmlspecialchars(strip_tags(trim($_POST['metode'] ?? '')), ENT_QUOTES, 'UTF-8');
$nominal = preg_replace('/[^A-Za-z0-9\-]/', '', $_POST['nominal']);
$usersID = htmlspecialchars(strip_tags(trim($_POST['userID'] ?? '')), ENT_QUOTES, 'UTF-8');

// Ambil data bank
$getBank = mysqli_query($conn, "SELECT * FROM `tb_bank` WHERE userID = '$usersID'") or die(mysqli_error($conn));
$gb = mysqli_fetch_array($getBank);
$catatan = $gb['akun'] . ' ' . $gb['no_rek'] . ' a/n ' . $gb['pemilik'];

// Ambil saldo aktif
$cekBalance = mysqli_query($conn, "SELECT * FROM `tb_balance` WHERE userID = '$usersID'") or die(mysqli_error($conn));
$cb = mysqli_fetch_array($cekBalance);
$saldoAktif = $cb['active'];



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

// Ambil data user eksternal
$cek_usernya = mysqli_query($conn, "SELECT * FROM tb_user WHERE cuid = '$usersID'") or die(mysqli_error($conn));
$data_usernya = mysqli_fetch_array($cek_usernya);
$username = $data_usernya['extplayer'];
$amount = intval($nominal);

// Cek minimal withdraw
if($nominal < 20000){
        header('Location:../withdraw/?notif=2');
        exit();
    }
    else {
        if($saldoAktif < $nominal){
            header('Location:../withdraw/?notif=4');
            exit();
        }
        else {
    // Cek transaksi pending
    $cekTransaksi = mysqli_query($conn, "SELECT * FROM `tb_transaksi` WHERE jenis = 2 AND userID = '$usersID' AND status = 0") or die(mysqli_error($conn));
    if (mysqli_num_rows($cekTransaksi) > 0) {
        header('location:../bank/withdraw?notif=3');
        exit();
    } else {
        // Kurangi saldo melalui API eksternal
        $sql_255 = mysqli_query($conn, "SELECT * FROM `tb_apigames` WHERE cuid = 1") or die(mysqli_error($conn));
        $s255 = mysqli_fetch_array($sql_255);

        //$withdrawBalanceResponse = $NG->withdraw_member($username, $amount);
       
        $message = "Ada permintaan withdraw\n\n";
        $message .= "Nominal : Rp" . number_format($nominal) . "\n\n";
        $message .= "Id pengirim : " . $u['user'];
        $api = new BotTelegram();
        $sendertele = $api->sendToTelegram($message);

        // Insert transaksi ke database
        $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','Penarikan Dana','$nominal',0, '$catatan', '', '0','2','$metode','0','$usersID',0)") or die(mysqli_error($conn));
        $newSaldo = $saldoAktif - $nominal;
		$updateBalance = mysqli_query($conn,"UPDATE `tb_balance` SET `active` = '$newSaldo' WHERE `userID` = '$usersID'") or die(mysqli_error($conn));
        header('location:../bank/withdraw?notif=1');
        exit();
    }
}
}