<?php
// ===============================
// SETTING BONUS REFERRAL PER CODE
// ===============================
 $ref_config = [
    
];

// default referral
 $default_rate = 40;
 $old_member_rate = 30;

if (isset($_GET['id'])) {

    $id = $_GET['id'];
    $tanggal = date('Y-m-d H:i:s');

    $query = mysqli_query($koneksi, "SELECT * FROM tb_transaksi WHERE id = '$id'");
    $data  = mysqli_fetch_array($query);

    if (!$data) {
        exit('Transaksi tidak ditemukan');
    }

    $id_user = $data['id_user'];
    $total   = $data['total'];

    // ===============================
    // SETTING BONUS REFERRAL
    // ===============================
    $cek_reff = mysqli_query($koneksi, "SELECT * FROM tb_refferal WHERE id_user = '$id_user' LIMIT 1");

    if (mysqli_num_rows($cek_reff) > 0) {
        $liat = mysqli_fetch_array($cek_reff);
        $reff = $liat['user_refferal'];

        $cek_transaksi = mysqli_query($koneksi, "SELECT id FROM tb_transaksi WHERE id_user = '$id_user' AND status = 'Sukses' AND transaksi = 'Top Up' LIMIT 1");

        if (mysqli_num_rows($cek_transaksi) > 0) {
            $rate_ref = $old_member_rate;
        } else {
            $rate_ref = $default_rate;
        }

        if (isset($ref_config[$reff])) {
            $rate_ref = $ref_config[$reff];
        }

        $bonus_ref = $total * ($rate_ref / 100);

        // Saldo Referral
        $cek_saldo_reff = mysqli_query($koneksi, "SELECT id FROM tb_saldo WHERE id_user = '$reff'");
        if (mysqli_num_rows($cek_saldo_reff) > 0) {
            mysqli_query($koneksi, "UPDATE tb_saldo SET active = active + $bonus_ref WHERE id_user = '$reff'");
        } else {
            mysqli_query($koneksi, "INSERT INTO tb_saldo (id_user, active, pending, transfer, payout, wagering) VALUES ('$reff', $bonus_ref, 0, 0, 0, 0)");
        }

        mysqli_query($koneksi, "INSERT INTO tb_transaksi (id, gambar, tanggal, transaksi, total, dari_bank, metode, bonus, keterangan, status, id_user) VALUES (NULL, '', '$tanggal', 'Bonus Refferal', '$bonus_ref', '', 'Bonus Refferal', '', 'Bonus', 'Sukses', '$reff')");
        mysqli_query($koneksi, "UPDATE tb_refferal SET bonus = bonus + $bonus_ref WHERE id_user = '$id_user'");
    }

    // ===============================
    // TAMBAH SALDO USER DEPOSIT
    // ===============================
    $cek_saldo_user = mysqli_query($koneksi, "SELECT id FROM tb_saldo WHERE id_user = '$id_user'");
    if (mysqli_num_rows($cek_saldo_user) > 0) {
        mysqli_query($koneksi, "UPDATE tb_saldo SET active = active + $total WHERE id_user = '$id_user'");
    } else {
        mysqli_query($koneksi, "INSERT INTO tb_saldo (id_user, active, pending, transfer, payout, wagering) VALUES ('$id_user', $total, 0, 0, 0, 0)");
    }

    // Update status transaksi deposit
    $query2 = mysqli_query($koneksi, "UPDATE tb_transaksi SET status = 'Sukses' WHERE id = '$id' AND id_user = '$id_user'");

    if ($query2) {
?>
        <script type="text/javascript">
            alert('Konfirmasi Deposit Berhasil');
            window.location = "?page=permintaan_deposit";
        </script>
<?php
    } else {
?>
        <script type="text/javascript">
            alert('Konfirmasi Deposit Gagal');
            window.location = "?page=permintaan_deposit";
        </script>
<?php
    }
}
?>