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

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

    
    $result = mysqli_query($con, "SELECT * FROM ahli WHERE id='".$_SESSION['aidi']."'");
    $rows = mysqli_fetch_array($result);        
    
?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>Bonus</h2>                          
                    </div>
                </div>
            <hr />
<?
if ($_SESSION['username'] <> "admin") {
        if (!in_array($_SESSION['role'], ['admin', 'finance', 'cashier'])) {
?>
            <div class="row">

                        <div class="col-md-6 col-sm-12 col-xs-12">
                            <div class="panel panel-back noti-box" style="box-shadow:0 4px 8px rgba(0,0,0,0.1); border-radius:8px;">
                                    <span class="icon-box bg-color-green set-icon">
                                        <i class="fa fa-usd"></i>
                                    </span>
                                <div class="text-box" >
                                    <p class="main-text"><?= number_format($cwallet,2) ?> MYR</p>
                                    <p class="text-muted">Total Bonus</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6 col-sm-12 col-xs-12">
                            <div class="panel panel-back noti-box" style="box-shadow:0 4px 8px rgba(0,0,0,0.1); border-radius:8px;">
                                <span class="icon-box bg-primary set-icon">
                                    <i class="fa fa-solid fa-hourglass-start"></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(0,2) ?></p>
                                <p class="text-muted">Pending Bonus</p>
                            </div>
                            </div>
                        </div>
            


            </div>
                 <!-- /. ROW  -->
                <hr />
<? 
        }
} ?>
            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                             Bonus History
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Date</th>
                                            <th>Description</th>
                                            <th>Ref</th>
                                            <th>Debit</th>
                                            <th>Credit</th>
                                        </tr>
                                    </thead>
                                    <tbody>
<?
$c = 1;  

 ?> 
<?php
    // CASH BONUS  
    if (in_array($_SESSION['role'], ['finance', 'admin', 'cashier'])) {
        $sql = "SELECT tarikh, keterangan, rujukan, keluar, masuk FROM cwallet ORDER BY tarikh DESC";
    }   else    {
        $sql = "SELECT tarikh, keterangan, rujukan, keluar, masuk FROM cwallet WHERE userid ='".$_SESSION['aidi']."' ORDER BY tarikh DESC";
    }
        $result = mysqli_query($con, $sql);

        while ($row = mysqli_fetch_array($result)) {
?>
                                        <tr >
                                            <td><?= $c++ ?>
                                            <td><?= $row[0] ?></td>
                                            <td><?= $row[1] ?></td>
                                            <td><code><?= $row[2] ?></code></td>
                                            <td class="right"><?= $row[3]?></td>
                                            <td class="right"><?= $row[4] ?></td>
                                        </tr>
 <?     } 
 ?> 

 
 
                                    </tbody>
                                </table>
                            </div>
                            
                        </div>
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>          
    </div>
             <!-- /. PAGE INNER  -->
            </div>
<?
    include "_footer2.php";
}
?>
