<?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['delBonus-submit'])) {
        
            $delBonus_uname     = mysqli_real_escape_string($con, $_POST['delBonus-uname']);
            $delBonus_amount    = mysqli_real_escape_string($con, $_POST['delBonus-amount']);
            $delBonus_remark    = mysqli_real_escape_string($con, $_POST['delBonus-remark']);
    
    	    //Check if inputs are empty
        	if (empty($delBonus_uname) || empty($delBonus_amount)) {
        		$notis = "Your imput is empty !";
        		
        	} else {
    
        		$sql_ahli           = "SELECT * FROM ahli WHERE username='".$delBonus_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>".$delBonus_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, keluar, userid)
                                    VALUES ('".$tarikh."', 'Bonus Deducted by admin : ".$delBonus_remark." ', '".$delBonus_uname."', '".$delBonus_amount."', '".$id."')");
                        $notis = "</br><div class='alert alert-info' role='alert'> * RM".$delBonus_amount." was successfully deduct from ".$addBonus_uname." bonus !</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">
                                Delete 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="delBonus-uname" required />
                                            </div>
                                            <div class="form-group">
                                                <label>Amount</label>
                                                <input class="form-control" type="number" name="delBonus-amount" required />
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                                <div class="form-group">
                                                    <label>Remark</label>
                                                    <textarea class="form-control" name="delBonus-remark" rows="5"></textarea>
                                                </div>
                                        </div>
                                        <div class="col-md-12">
                                            <button type="submit" name="delBonus-submit" class="btn btn-danger">Delete</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  
                                </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";
}
?>
