<?php
session_start();
$key = md5(date("Y-m-d").":".$_SERVER['REMOTE_ADDR']);
if (isset($_SESSION['aW1iZXN0cmEuY29tLm15']) <> $key) {
    header("location:".$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME']); // cara baru !
} else {
    include "_header4.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";
    require "conn.php";

    if (isset($_POST[md5("wthdrw".$_SESSION['aidi'])])) {

            $amaun = mysqli_real_escape_string($con, $_POST['aa']);
            $secret = "6Lftf5UUAAAAAKfr-KvHBd-oSDd0J_JmFciRkKBt";
            $response = $_POST["g-recaptcha-response"];
            $verify = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
            $captcha_success = json_decode($verify);

        if ($captcha_success->success==true) {

            date_default_timezone_set("Asia/Kuala_Lumpur");
            $tarikh = date("Y-m-d H:i:s");

            mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, keluar, userid)
            VALUES ('".$tarikh."', 'Manual Withdrawal', '".$_SESSION['username']."', ".$amaun.", ".$_SESSION['aidi'].")");

            mysqli_query($con, "INSERT INTO withdraw (userid, amaun, tarikh)
            VALUES ('".$_SESSION['aidi']."', ".$amaun.", '".$tarikh."')");

            $cwallet = sumWall("masuk", "cwallet") - sumWall("keluar", "cwallet");
            //$nota = noTis("info", "Done", "Withdrawal accepted.");

        }

    } 

    $cwallet = sumWall("masuk", "cwallet") - sumWall("keluar", "cwallet");


    $limit = 100;


    if ($cwallet <= $limit) {
        $butang = "button";
        $dis = "disabled";
    } else {
        $butang = "submit";
        $dis = "";
    }

    $result = mysqli_query($con, "SELECT SUM(amaun) FROM withdraw WHERE userid='".$_SESSION['aidi']."'");
    $withdraw = mysqli_fetch_array($result);
    
?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>Withdrawal</h2>
                    </div>
                </div>
            <hr />
<?
if ($_SESSION['username'] <> "admin") {
?>
                <div class="row">

                    <div class="col-md-6 col-sm-6 col-xs-6">
                        <div class="panel panel-back noti-box">
                            <span class="icon-box bg-color-red set-icon">
                                <i class="fa fa-money"></i>
                            </span>
                        <div class="text-box" >
                            <p class="main-text"><?= number_format($withdraw[0],2) ?> MYR</p>
                            <p class="text-muted">Total Withdrawal</p>
                        </div>
                        </div>
                    </div>

                    <div class="col-md-6 col-sm-6 col-xs-6">
                        <div class="panel panel-back noti-box">
                            <span class="icon-box bg-color-green set-icon">
                                <i class="fa fa-usd"></i>
                            </span>
                        <div class="text-box" >
                            <p class="main-text"><?= $cwallet ?> MYR</p>
                            <p class="text-muted">Bonus Balance</p>
                        </div>
                        </div>
                    </div>

            </div>
                 <!-- /. ROW  -->
                <hr />

                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Withdraw
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-md-6">
<?
 //else {
if (!isset($_POST[md5("wthdrw".$_SESSION['aidi'])])) {
?>
                                    <form role="form" method="post">
                                        <div class="form-group">
                                            <label>Bonus Balance</label>
                                            <input class="form-control" value="<?= $cwallet ?>" readonly />
                                        </div>
                                        <div class="form-group">
                                            <label>Amount</label>
                                            <input class="form-control" type="number" name="aa" />
                                        </div> 
                                        <div class="form-group">
                                            <label>Security</label>
                                            <div class="g-recaptcha" data-sitekey="6Lftf5UUAAAAABHmoqDkm7yTm7iRbQB1lmS5GXE-"></div>
                                        </div>                                   

                                        <button type="<?= $butang ?>" name="<?= md5("wthdrw".$_SESSION['aidi']) ?>" class="btn btn-primary"<?= $dis ?>>Submit</button>

                                        </form>
<? } else {

    if ($captcha_success->success==false) {

        noTis("warning", "Warning!", "Browser refresh/reload detected.");

    } else if ($captcha_success->success==true) {

        noTis("info", "Done", "Withdrawal accepted.");
    }
    
}
?>
                                </div>
                               
                            </div>
                        </div>
                    </div>                
<? } ?>

         
    </div>
             <!-- /. PAGE INNER  -->
            </div>
<?
    include "_footer2.php";
}
?>
