<?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";

    $nilaipin = reKod("nilaipin", "komisen", "id", 1);

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

            $kuantiti = 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");

            $amaun = $nilaipin * $kuantiti; // jualan pin

            // potong bonus
            mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, keluar, userid)
            VALUES ('".$tarikh."', 'Buy ".$kuantiti." PIN from Bonus', '".$_SESSION['username']."', ".$amaun.", ".$_SESSION['aidi'].")");

            // janaan pin
            for ($x = 1; $x <= $kuantiti; $x++) {
                mysqli_query($con, "INSERT INTO bestpin (no, userid, datesold, ip) VALUES ('".randomPin()."', ".$_SESSION['aidi'].", '".$tarikh."', '".$_SERVER['REMOTE_ADDR']."')");
                if ($x == $kuantiti) {
                    $upd = true;
                }
            }

            if ($upd) {
                echo "You bought ".$kuantiti." PIN !";
            }


        }















    } 

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


    $limit = 100;


    if ($cwallet <= $limit) {
        $butang = "button";
        $dis = "disabled";
    } else {
        $butang = "submit";
        $dis = "";
    }
    
?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>Buy PIN (from bonus)</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-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">
                            Buy PIN
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-md-6">
<?
 //else {
if (!isset($_POST[md5("wthdrw".$_SESSION['aidi'])])) {

    $available = floor($cwallet / $nilaipin);


?>
                                    <form role="form" method="post">
                                        <div class="form-group">
                                            <label>Bonus Balance (PIN Available)</label>
                                            <input class="form-control" value="<?= "RM ".$cwallet." (".$available." PIN available) " ?>" readonly />
                                        </div>
                                        <div class="form-group">
                                            <label>Purchase PIN (Qty)</label>
                                            <input class="form-control" type="number" min="1" step="1" max="<?= $available ?>" name="aa" required />
                                        </div> 
                                        <div class="form-group">
                                            <label>Security</label>
                                            <div class="g-recaptcha" data-sitekey="6Lftf5UUAAAAABHmoqDkm7yTm7iRbQB1lmS5GXE-"></div>
                                        </div>                                   

                                        <button type="<?= $butang ?>" name="<?= md5("buypin".$_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";
}
?>
