<?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 "_header.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";
    require "conn.php"; 
    
    date_default_timezone_set("Asia/Kuala_Lumpur");
    $tarikh = date("Y-m-d H:i:s");
    

        // Check kalau ada sumbit bonus manual
        if (isset($_POST['addBonus-submit'])) {
        
            $addBonus_uname     = mysqli_real_escape_string($con, $_POST['addBonus-uname']);
            $addBonus_amount    = mysqli_real_escape_string($con, $_POST['addBonus-amount']);  
            $addBonus_remark    = mysqli_real_escape_string($con, $_POST['addBonus-remark']);
    
    	    //Check if inputs are empty
        	if (empty($addBonus_uname) || empty($addBonus_amount)) {
        		$notis = "Your imput is empty !";
        		
        	} else {
    
        		$sql_ahli           = "SELECT * FROM ahli WHERE username='".$addBonus_uname."'";
        		$result_ahli        = mysqli_query($con, $sql_ahli);
        		$resultCheck_ahli   = mysqli_num_rows($result_ahli);
        		
        		if ($resultCheck_ahli < 1) {
        		    
        	        $notis = "</br><div class='alert alert-info' role='alert'> * Username <b>".$addBonus_uname."</b> not in record !</div>";
        	        
        		} else {
    
        			if ($row = mysqli_fetch_assoc($result_ahli)) {
    
        				$id     = $row['id']; 
                        $test   = mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
                                    VALUES ('".$tarikh."', 'Bonus Added by admin : ".$addBonus_remark." ', '".$addBonus_uname."', '".$addBonus_amount."', '".$id."')");
                        $notis = "</br><div class='alert alert-info' role='alert'> * RM".$addBonus_amount." was successfully added for ".$addBonus_uname." !</div>";
        				}
                
        			}
        	}
        }
    
?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>Bonus</h2>                        
                    </div>
                </div>
            <hr />

			<div class="row">
                <div class="col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Add Bonus
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <form role="form" method="post" action="">
                                <div class="col-md-12">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Username</label>
                                            <input class="form-control" name="addBonus-uname" required />
                                        </div>
                                        <div class="form-group">
                                            <label>Amount</label>
                                            <input class="form-control" type="number" name="addBonus-amount" required />
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Remark</label>
                                            <textarea class="form-control" name="addBonus-remark" rows="5"></textarea>
                                        </div>
                                    </div>
                                    <div class="col-md-12">
                                    <button type="submit" name="addBonus-submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </div>
                                </form>
                            </div>
                            <?= $notis ?>
                        </div>
                    </div>
                </div>

			</div>

            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                             Bonus History  <?= $test; ?>
                        </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;    
    $sql = "SELECT tarikh, keterangan, rujukan, keluar, masuk FROM cwallet ORDER BY tarikh DESC";
    $result = mysqli_query($con, $sql);

    while ($row = mysqli_fetch_array($result)) {
?>
                                        <tr class="even">
                                            <td><?= $c++ ?>
                                            <td><?= $row[0] ?></td>
                                            <td><?= $row[1] ?></td>
                                            <td><?= $row[2] ?></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>
        </div>
<?
    include "_footer.php";
}
?>
