<?php
session_start();
require '../config.php';
require '../lib/session_user.php';
$cek_CoinDepo = $conn->query("SELECT * FROM penukaran_coin WHERE tipe = 'Deposit'");
$data_CoinDepo = $cek_CoinDepo->fetch_assoc();
if (isset($_POST['tukar'])) {
    require '../lib/session_login.php';
    $coin = $conn->real_escape_string(trim(filter($_POST['coin'])));

    $cek_coin = $conn->query("SELECT * FROM referral_kode WHERE username = '$sess_username' AND coin = '$coin'");
    $data_coin = $cek_coin->fetch_assoc();
    $cek_coin_rows = mysqli_num_rows($cek_coin);

    $post_balance = $data_coin['coin']*$data_CoinDepo['nominal'];
    $post_voc = $data_coin['voucher'];
    
    $cek_wa = $conn->query("SELECT * FROM bot_whatsapp WHERE status = 'Aktif'");
        $data_wa_bot = $cek_wa->fetch_assoc();
        
    if (!$coin) {
        $_SESSION['hasil'] = array('alert' => 'danger', 'judul' => 'Permintaan Gagal', 'pesan' => 'Tidak Ada Coin Untuk Di Tukar');
    } else if ($cek_coin_rows == 0) {
        $_SESSION['hasil'] = array('alert' => 'danger', 'judul' => 'Permintaan Gagal', 'pesan' => 'Coin Tidak Valid.');
    } else if ($coin < $data_CoinDepo['min']) {
        $_SESSION['hasil'] = array('alert' => 'danger', 'judul' => 'Permintaan Gagal', 'pesan' => 'Jumlah Minimal Penukaran Adalah ' . $data_CoinDepo['min'].' COIN ');
    } else {
        
        $insert_depo = $conn->query("INSERT INTO history_saldo VALUES ('', '$sess_username', 'Penukaran Coin', '$post_balance', 'Penambahan Saldo Dengan Menukarkan Coin $coin', '$date', '$time')");
        $insert_depo = $conn->query("UPDATE users set saldo = saldo + $post_balance WHERE username = '$sess_username'");
        $insert_depo = $conn->query("UPDATE referral_kode set coin = coin - $coin WHERE username = '$sess_username'");

        if ($insert_depo == TRUE) {
            $target = $data_user['nomer'];
                $token_whatsapp =  $data_wa_bot['token_wa'];
                $waktu_order = (tanggal_indo($date));
                $webstes = $config['web']['url'];
                $curl = curl_init();
                curl_setopt_array($curl, array(
                    CURLOPT_URL => 'https://api.fonnte.com/send',
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => '',
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 0,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => 'POST',
                    CURLOPT_POSTFIELDS => array(
                        'target' => $target,
                        'message' => "PENUKARAN COIN BERHASIL\n\nAnda baru saja melakukan penukaran coin sebesar : Rp $post_balance\nWaktu : $waktu_order - $time WIB\n\nby : $webstes",
                    ),
                    CURLOPT_HTTPHEADER => array(
                        "Authorization: $token_whatsapp " //change TOKEN to your actual token
                    ),
                ));
    
                $response = curl_exec($curl);
                curl_close($curl);
                
            $_SESSION['hasil'] = array('alert' => 'success', 'judul' => 'Redeem Voucher Berhasil', 'pesan' => 'Saldo Anda Telah Kami Tambah Dengan Nominal Rp ' . $post_balance);
            exit(header("location: " . $config['web']['url']));
        } else {
            $_SESSION['hasil'] = array('alert' => 'danger', 'judul' => 'Upss', 'pesan' => 'Terjadi Kesalahan Silahkan Hubungi Admin');
        }
    }
    header("Location: " . $_SERVER['REQUEST_URI'] . "");
        exit();
}
require '../lib/header.php';
?>
<div class="content-wrapper">
    <?php
    if (isset($_SESSION['hasil'])) {
    ?>
        <div class="alert alert-<?php echo $_SESSION['hasil']['alert'] ?>">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <b><?php echo $_SESSION['hasil']['judul'] ?></b> <?php echo $_SESSION['hasil']['pesan'] ?>
        </div>
    <?php
        unset($_SESSION['hasil']);
    }
    ?>
    <div class="row">
        <div class="col-md-6 grid-margin">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title"><i class="mdi mdi-coin text-warning"></i> <?php echo $data_kodeReferral['coin']; ?></h4>
                    <form class="forms-sample" method="POST">
                        <input type="hidden" name="csrf_token" value="<?php echo $config['csrf_token'] ?>">
                        <div class="form-group">
                            <label for="coin">Coin Saya</label>
                            <input type="text" name="coin" id="coin" class="form-control" style="background: #000;" value="<?php echo $data_kodeReferral['coin']; ?>" readonly>
                        </div>
                        <button type="submit" name="tukar" class="btn btn-info float-right">Tukar Coin</button>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-md-6 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">- PENUKARAN COIN</h4>
                    <ul>
                        <li>Coin Dapat Di Tukar Menjadi Saldo Sesuai Syarat Dan Ketentuan.</li>

                        <li>Silahkan Klik Tukar Coin Untuk Mengkonvert Jumlah Coin Menjadi Saldo</li>
                        <li>Minimal Penukaran Coin Adalah <i class="mdi mdi-coin text-warning"></i> <?php echo $data_CoinDepo['min']; ?> Coin</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

</div>
<?php
require '../lib/footer.php';
?>