<?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 {
    require "conn.php";
    include "_header.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";

    $mwallet_transaction    = MsumWall("keluar", "mwallet");
    $mwallet                = MsumWall("masuk", "mwallet") - sumWall("keluar", "mwallet");
    $cwallet                = sumWall("masuk", "cwallet") - sumWall("keluar", "cwallet");
    $cwallet_carwash        = sumWall("masuk", "cwallet_carwash") - sumWall("keluar", "cwallet_carwash");
    $cwallet_travel         = sumWall("masuk", "cwallet_travel") - sumWall("keluar", "cwallet_travel");                 // wax & polish
    $cwallet_cash           = sumWall("masuk", "cwallet_cash") - sumWall("keluar", "cwallet_cash");                     // repair
    $cwallet_accessories    = sumWall("masuk", "cwallet_accessories") - sumWall("keluar", "cwallet_accessories");
    $cwallet_insurance      = sumWall("masuk", "cwallet_insurance") - sumWall("keluar", "cwallet_insurance");
    $limit = 100;

    if ($cwallet >= $limit) {
        $link = "request_withdraw.php";
    } else {
        $link = "#";
    }

    $result = mysqli_query($con, "SELECT SUM(amaun) FROM withdraw WHERE userid='".$_SESSION['aidi']."'");
    $withdraw = mysqli_fetch_array($result);

                                if (isset($_POST['btn_tx_trn_insurance_cash'])) {
                                    
		                            date_default_timezone_set("Asia/Kuala_Lumpur");
		                            $tarikh                 = date("Y-m-d H:i:s");                                    
                                    $uid                    = $_SESSION['aidi'];
                                    $unama                  = strtoupper($_SESSION['namapenuh']);
                                    $tx_insurancecash       = $_POST['tx_insurance_cash'];
                                    $surcharge_data         = number_format((4 * $tx_insurancecash)/100,2); 
                                    $tx_data                = number_format($tx_insurancecash - $surcharge_data,2); 
                                    $tx_keterangan          = "Transfer Insurance Bonus to CASH Bonus *Surcharge: RM".$surcharge_data; 
                                    $cash_keterangan        = "Received RM".$surcharge_data." surcharge Insurance Bonus from ".$_SESSION['namapenuh'];  
                                    // duit keluar insurance bonus - members
                                    mysqli_query($con, "INSERT INTO cwallet_insurance (tarikh, keterangan, rujukan, keluar, userid)
                                    VALUES ('".$tarikh."', '".$tx_keterangan."', '".$unama."', '".$tx_insurancecash."', '".$uid."')");
                                    // duit masuk cash bonus - members
                                    mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
                                    VALUES ('".$tarikh."', '".$tx_keterangan."', '".$unama."', ".$tx_data.", ".$uid.")");
                                    // duit masuk cash bonus - admin
                                    mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
                                    VALUES ('".$tarikh."', '".$cash_keterangan."', '".$unama."', ".$surcharge_data.", '1')");
                                  
                                    
                          }


?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>Dashboard</h2>
                        <h5>Welcome <?= $_SESSION['namapenuh'] ?> , Love to see you back. </h5>
                    </div>
                </div>
                 <!-- /. ROW  -->

                 <!-- /. ROW  -->
                <hr />
                
<?
//if ($_SESSION['username'] <> "admin")  {

    if ($_SESSION['role'] <> "merchant") {
   
?>

                <div class="row">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <h4>Available Balance</h4>
                    </div>
                    <div class="col-md-6 col-sm-12 col-xs-12">
                        <div class="panel panel-back noti-box" >
                            <span class="icon-box bg-primary set-icon" >
                                <i class="fa fa-usd" ></i>
                            </span>
                            <div class="text-box small" >
                                <p class="main-text"><span class="small" style="font-size:15px; padding-right:10px;">RM </span><?= number_format($cwallet,2) ?></p>
                                <p class="text-muted">Cash Bonus</p>
                            </div>
                        </div>
                    </div>


                    <div class="col-md-6 col-sm-12 col-xs-12">
                        <div class="panel panel-back noti-box">
                            <span class="icon-box bg-primary set-icon">
                                <i class="fa fa-tint"></i>
                            </span>
                        <div class="text-box small" >
                            <p class="main-text"><span class="small" style="font-size:15px; padding-right:10px;">RM </span><?= number_format($cwallet_carwash,2) ?></p>
                            <p class="text-muted">Car Wash Bonus</p>
                        </div>
                        </div>
                    </div>
                
                    <div class="col-md-6 col-sm-12 col-xs-12">
                        <div class="panel panel-back noti-box">
                            <span class="icon-box bg-primary set-icon">
                                <i class="fa fa-star-half-stroke"></i>
                            </span>
                        <div class="text-box small" >
                            <p class="main-text"><span class="small" style="font-size:15px; padding-right:10px;">RM </span><?= number_format($cwallet_travel,2) ?></p>
                            <p class="text-muted">Car Polish & Wax Bonus</p>
                        </div>
                        </div>
                    </div>
                    
                    <div class="col-md-6 col-sm-12 col-xs-12">
                        <div class="panel panel-back noti-box">
                            <span class="icon-box bg-primary set-icon">
                                <i class="fa fa-wrench"></i>
                            </span>
                        <div class="text-box small" >
                            <p class="main-text"><span class="small" style="font-size:15px; padding-right:10px;">RM </span><?= number_format($cwallet_cash,2) ?></p>
                            <p class="text-muted">Car Repairing Bonus</p>
                        </div>
                        </div>
                    </div>
                    
                    <div class="col-md-6 col-sm-12 col-xs-12">
                        <div class="panel panel-back noti-box">
                            <span class="icon-box bg-primary set-icon">
                                <i class="fa fa-magic"></i>
                            </span>
                        <div class="text-box small" >
                            <p class="main-text"><span class="small" style="font-size:15px; padding-right:10px;">RM </span><?= number_format($cwallet_accessories,2) ?> </p>
                            <p class="text-muted">Car Accessories Bonus</p>
                        </div>
                        </div>
                    </div>
                    
                    <div class="col-md-6 col-sm-12 col-xs-12">
         
                            <div class="panel panel-back noti-box">
                            <span class="icon-box bg-primary set-icon">
                                <i class="fa fa-shield"></i>
                            </span>
                        <div class="text-box small" >
                            <div data-toggle="modal" data-target="#exampleModalCenter" style="cursor: pointer; position:relative;    top:0;    right:0;    float:right; color:blue;">Transfer to Cash Bonus</div>
                            <p class="main-text"><span class="small" style="font-size:15px; padding-right:10px;">RM </span><?= number_format($cwallet_insurance,2) ?></p>
                            <p class="text-muted">Car Insurance Bonus</p>
                        </div>
                        </div>
                            <!-- Modal -->
                            <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                              <div class="modal-dialog modal-dialog-centered" role="document">
                                <div class="modal-content">
                                    <form method="post" >
                                      <div class="modal-header bg-primary">
                                          <button style="position:relative;    top:0;    right:0;    float:right;" type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;  </button>
                                        <h5 class="modal-title" id="exampleModalLongTitle">Transfer Available Insurance Bonus</h5>
                                        
                                      </div>
                                      <div class="modal-body">
                                        Confirm transfer <span class="small" style="font-size:15px; color:red;">RM <?= number_format($cwallet_insurance,2) ?></span> to Cash Bonus ?  
                                        <input type="hidden" value="<?= number_format($cwallet_insurance,2) ?>" name="tx_insurance_cash" />
                                      </div>
                                      <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
                                        <button type="submit" class="btn btn-primary" name="btn_tx_trn_insurance_cash">Yes</button>
                                      </div>
                                    </form>
                                </div>
                              </div>
                            </div>                       
                    </div>

            </div>
                 <!-- /. ROW  -->
                <hr />
<? 
    }   else    {
?>
                <div class="container">
                    <div class="row">
                        <div class="col-md-6 col-sm-12 col-xs-12 bg-warningk">
                            <div class="panel panel-back noti-box" >
                                <span class="icon-box bg-primary set-icon" >
                                    <i class="fa fa-usd" ></i>
                                </span>
                                <div class="text-box small" >
                                    <p class="main-text"><span class="small" style="font-size:15px; padding-right:10px;">RM </span><?= number_format($mwallet,2) ?></p>
                                    <p class="text-muted">Available Balance</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6 col-sm-12 col-xs-12 bg-warningk">
                            <div class="panel panel-back noti-box" >
                                <span class="icon-box bg-primary set-icon" >
                                    <i class="fa fa-money-bill-transfer" ></i>
                                </span>
                                <div class="text-box small" >
                                    <p class="main-text"><span class="small" style="font-size:15px; padding-right:10px;">RM </span><?= number_format($mwallet_transaction,2) ?></p>
                                    <p class="text-muted">Total Withdrawal</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
<?php
    }
// } 



date_default_timezone_set("Asia/Kuala_Lumpur");

// kiraan 60 hari utk ahli lembab lagi tak produktif
$dwnln = mysqli_num_rows(mysqli_query($con, "SELECT id FROM ahli WHERE penaja = '".$_SESSION['aidi']."'"));

if ($dwnln  == 0) {

  $sql = "SELECT DATE_ADD(tarikh, INTERVAL 12 MONTH) FROM `ahli` WHERE id = ".$_SESSION['aidi'];
  $expired = mysqli_fetch_array(mysqli_query($con, $sql))[0];

  $tarikh = new DateTime($expired);
  $lani = new DateTime();

  $beza = $tarikh->diff($lani)->format("%a days, %h hours, %i minutes");

/*
$datetime1 = new DateTime();
$datetime2 = new DateTime($expired);
$interval = $datetime1->diff($datetime2);
$beza = $interval->format('%a days');
*/
?>
                <div class="row">
                    <div class="col-md-4">
                    <div class="panel panel-primary text-center no-boder bg-color-red">
                        <div class="panel-body">
                            <i class="fa fa-power-off fa-5x"></i>
                            <h4><?= $beza ?></h4>
                        </div>
                        <div class="panel-footer back-footer-red">
                            Account Expired : <?= date("d-m-Y", strtotime($expired)) ?>

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