<?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";
    
    date_default_timezone_set("Asia/Kuala_Lumpur");
    $tarikh = date("Y-m-d H:i:s");
    
    $cwallet = sumWall("masuk", "cwallet") - sumWall("keluar", "cwallet");
    $cwallet_travel = sumWall("masuk", "cwallet_travel") - sumWall("keluar", "cwallet_travel");
    $cwallet_cash = sumWall("masuk", "cwallet_cash") - sumWall("keluar", "cwallet_cash");
    $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);






// Check kalau ada sumbit package DEL
        if (!empty(isset($_POST['pkg-del'])) ){
           
            //$tripID                = mysqli_real_escape_string($con, $_POST['pkg_id']);
        	//$sql_package           = "SELECT * FROM setting_trip_package WHERE trip_id='".$tripID."' ";
        	//$result_package        = mysqli_query($con, $sql_package);
        //	$row_package           = mysqli_fetch_array($result_package);
        	//$resultCheck_package   = mysqli_num_rows($result_package);
        		
        	if ($resultCheck_package == 1) {
        		   
        	    $pkgName            = $row_package['trip_name'];
        	    $filename           = $row_package['trip_img'];
            
                mysqli_query($con, "DELETE FROM setting_trip_package WHERE trip_id='".$tripID."' ");
                
               if (file_exists($filename)) {
                    unlink($filename);
                    $notis = "</br><div class='alert alert-info' role='alert'> * ".$pkgName." trip package was successfully remove !</div>";
                } else {
                    $notis = "</br><div class='alert alert-info' role='alert'> * ".$pkgName." trip package image was unsuccessful remove !</div>";
                }

        	} else {

                $pkgName            = "";
                $pkgPrice           = "";  
                $pkgLevel           = "";
                $pkgDesc            = "";
                $pkgImg             = "";
                $pkgButton          = '<button type="submit" name="pkg-add" class="btn btn-primary" form="package">Add</button>';
                $pkgTitle           = '<i class="fa fa-flag fa-1x "></i>';
         	}
         	
        }


?>
 <script>
    window.setTimeout(function() {
        $(".alert").fadeTo(500, 0).slideUp(500, function(){
            $(this).remove();
        });
    }, 2000);
</script>

        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2><i class="fa fa-gift"></i> Redeem Trip</h2>                        
                    </div>
                </div>
            <hr />
            
<?
/////////////////////////// USER VIEW /////////////////////////////////////////////////////////////

if ($_SESSION['username'] <> "admin") {
?>              
                <!-- /////////////////////////////////////////////////////////////  BALANCE TRIP REWARD  ////////////////////////  -->
                <div class="row">
                    <div class="col-md-6 col-sm-12 col-xs-12">
                        <div class="panel panel-back noti-box">
                            <span class="icon-box bg-color-red set-icon">
                                <i class="fa fa-flag" style="margin-top:15px"></i>
                            </span>
                        <div class="text-box small" >
                            <p class="main-text"><?= number_format($cwallet_travel,2) ?> MYR</p>
                            <p class="text-muted">Balance Trips Reward</p>
                        </div>
                        </div>
                    </div>
                </div>
                 <!-- /. ROW  -->
                
         

			<div class="row">
                <div class="col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Redeem Trip History
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-borderless table-hover" id="dataTables-example">
                                                    <thead>
                                                        <tr>
                                                            <th><h6>#                   </h6></th>
                                                            <th><h6>Date                </h6></th>
                                                            <th><h6>Package             </h6></th>
                                                            <th><h6>Ticket Qty / PAX    </h6></th>
                                                            <th><h6>Amount              </h6></th>
                                                            <th><h6>Status              </h6></th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                <?
                    $c = 1;
                    $sql = "SELECT id, username, nama, kp, status FROM ahli WHERE id NOT IN (1,2,3,4,5)";
                    $result = mysqli_query($con, $sql);
                
//                    while ($row = mysqli_fetch_array($result)) {
                ?>
<!--                                                       <tr class="even small">
                                                            <td><?= $c++ ?>
                                                            <td><?= $tarikh; ?></td>
                                                            <td><?= $row[2] ?></td>
                                                            <td><?= $row[3] ?></td>
                                                            <td><?= $row[3] ?></td>
                                                            <td><a href="#" class="btn btn-success btn-xs">Active</a></td>
                                                            <td><a href="edit.php?id=<?= $row[0] ?>" class="btn btn-primary btn-xs"><i class="fa fa-edit "></i> Edit</a></td>
                                                        </tr>       
                                                       
                 <?// } ?>        
-->
                                                    </tbody>
                                                </table>
                            </div>
                        </div>
                    </div>
                </div>

			</div>            
            
            <?= $notis ?>

            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                             <i class="fa fa-flag fa-1x "></i> Trip Package Listing  
                        </div>
                        <div class="panel-body">
                         

<?
$c = 1;    
    $sql        = "SELECT * FROM setting_trip_package ORDER BY trip_level, trip_price ASC";
    $result     = mysqli_query($con, $sql);
    $tripBal    = number_format($cwallet_travel,2);
    //$tripBal    = 1000;
    while ($row = mysqli_fetch_array($result)) {
        
        if ($tripBal >= $row['trip_price']) {
            $img   = '<img src="'.$row['trip_img'].'" class="responsive" style="width:100%; height:200px; "> ';
            $gift   = '<button data-toggle="modal" data-target="#mdl'.$row['trip_id'].'" type="button" class="btn btn-circle btn-xl btn-outline-light btn-success"  form="pkg'.$row['trip_id'].'">   <i class="fa fa-gift fa-2x btn" title="Redeem Now"    ></i></button>';    
        }   else    {
            $img   = '<img src="'.$row['trip_img'].'" class="responsive" style="width:100%; height:200px; filter: grayscale(100%); -webkit-filter: grayscale(100%); "> ';
            $gift   = '';    
        }
?>
                                                
                                                <div class="container col-md-12 border border-info" style="margin-bottom:20px;">
                                                  <form id="pkg<?= $row['trip_id']; ?>" method="post" action="">  
                                                  <?php echo $img; ?>
                                                  <input class="form-control" type="hidden" name="pkg_id" value="<?= $row['trip_id']; ?>" />
                                                    <div class="level ">
                                                        <div class="col-md-12 border-1" style="margin-left:5px;">
                                                            <h5 style="margin-top:5px;"><i class="fa fa-flag fa-flip-horizontal"></i> <?= ucwords($row['trip_level']); ?> </h5>
                                                            <h1 style="margin-top:-5px;">RM <?= $row['trip_price']; ?> <span class="small" style="color:#FFF;">/ pax</span></h1> 
                                                        </div>
                                                    </div>
                                                    <div class="content " >
                                                        <div class="col-md-9 border-1" style="margin-left:5px; margin-top:-15px; display:inline-block;">
                                                            <div >
                                                                <h3><?= ucwords($row['trip_name']); ?></h3>  
                                                                <h6 class="small" style="font-size: 14px; margin-top:-10px;"><span class="small"><?= ucwords($row['trip_desc']); ?> </span></h6> 
                                                            </div>
                                                        </div>
                                                        <div class="col-md-3" style="position: absolute; top: 50%; left: 95%; transform: translate(-50%, -50%);">
                                                            <?= $gift; ?>
                                                        </div>                                                    
                                                    </div>
                                                  
<!-- /////////////////   Modal Trips   ///////////////////////////////////////////////////////////////////////////////////////// -->  
                                                    
                                                    <div class="modal fade" id="mdl<?= $row['trip_id']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                                                      <div class="modal-dialog modal-dialog-centered" role="document" style="color: #434e65;
	width: 525px;">
                                                        <div class="modal-content" style="padding: 20px; padding-bottom: 150px;
                                                                                            	font-size: 16px;
                                                                                            	border-radius: 5px;
                                                                                            	border: none;">
                                                          <div class="modal-header justify-content-center" style="background: #47c9a2;
                                                                                            border-bottom: none;   
                                                                                            position: relative;
                                                                                            text-align: center;
                                                                                            margin: -20px -20px 0;
                                                                                            border-radius: 5px 5px 0 0;
                                                                                            padding: 35px;">
                                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                                  <span aria-hidden="true">&times;</span>
                                                                </button>
                                                                <div style="color: #fff;		
                                                                        	width:100px;
                                                                        	height: 100%;
                                                                        	display: inline-block;
                                                                        	border-radius: 50%;
                                                                        	z-index: 9;
                                                                        	border: 5px solid #fff;
                                                                        	text-align: center;">
                                            					    <h1><i class="fa fa-gift fa-2x btn" ></i></h1>
                                            				    </div>
                                                          </div>
                                                			<div class="modal-body" >
                                                			    <h1 style="font-size: 10px; font-style: italic; " class=" text-success">Destination :</h1>
                                                				<h1 style="font-size: 20px; margin-top: -8px; margin-bottom: -10px;"><?= ucwords($row['trip_name']); ?></h1>	
                                                				<h6 style="font-size: 10px;"><?= ucwords($row['trip_desc']); ?></h6>
                                                				
                                                				    <div class="col-md-3 border border-success" style="display:inline-block;">
                                                				        <h1 style="font-size: 10px; font-style: italic; " class=" text-success">Trip Reward Bal :</h1>
                                                				        <h1 style="font-size: 20px; margin-top: -8px; "><span class="small">RM </span><?= number_format($cwallet_travel,2) ?></h1>
                                                				    </div>
                                                				    <div class="col-md-3 border border-success" style="display:inline-block;">
                                                				        <h1 style="font-size: 10px; font-style: italic; " class=" text-success">Trip Price / pax:</h1>
                                                				        <h1 style="font-size: 20px; margin-top: -8px; "><span class="small">RM </span><?= $row['trip_price']; ?></h1>
                                                				        <input id="TPrice"  type="hidden"   value="<?= $row['trip_price']; ?>" />
                                                				    </div>
                                                				    <div class="col-md-3 border border-success" style="display:inline-block;">
                                                				        <h1 style="font-size: 10px; font-style: italic; " class=" text-success">Ticket Qty / Pax :</h1>
                                                				        <div style="font-size: 20px; margin-top: -8px; ">
                                                				            <select id="TQty" onchange="MyTQty" class="small" style="width:100%;" required>
<?php                                                                           //TripBal    = number_format($cwallet_travel,2);
                                                                                $TicQty     = $tripBal / $row['trip_price'];
                                                                                $TicketQty  = floor($TicQty);
                                                                                $t = 0;
                                                                                while ($TicketQty >= 1 )  {
                                                                                    $t++;
?>                                                            
                                                                                    <option value="<?= $t; ?>"><?= $t; ?></option>    
<?php                                                                               $TicketQty = $TicketQty - 1;
                                                                                }
                                                                                
?>
                                                				            </select>
                                                				        </div>
                                                				    </div>
                                                				    <div class="col-md-3 border border-success" style="display:inline-block;">
                                                				        <h1 style="font-size: 10px; font-style: italic; " class=" text-success">Total RM :</h1>
                                                				        <h1 style="font-size: 20px; margin-top: -8px; ">
                                                				            <div id="TTotal">
                                                				                <span class="small text-success">RM </span><span class="text-danger"> <?= $row['trip_price']; ?></span>
                                                				                <input type="hidden"   value="<?= $row['trip_price']; ?>" />
                                                				            </div>
                                                				        </h1>
                                                				    </div>
                                                			</div>	
                                                			
                                                          
                                                                    <div class="modal-footer col-md-12 text-center" >
                                                                        
                                                    				    <h6 style="margin-top: -8px; " class=" text-success">* If you are agree to redeem above trip package, please click YES button.</h6>
                                                            			<button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
                                                                        <button type="button" class="btn btn-success">Yes</button>
                                                                    </div>
                                                        
                                                        </div>
                                                      </div>
                                                    </div>
                                                  
                                                  </form>
                                                </div>
                                                
                                         

 <? } ?>
 

 

                           
                            
                        </div>
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>

<?php   
}   else    {
    
/////////////////////////// ADMIN VIEW /////////////////////////////////////////////////////////////    
?>

			<div class="row">
                <div class="col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Request List <span class="badge text-danger"> 1</span>
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-borderless table-hover" id="dataTables-example">
                                                    <thead>
                                                        <tr>
                                                            <th><h6>#           </h6></th>
                                                            <th><h6>Date        </h6></th>
                                                            <th><h6>Package     </h6></th>
                                                            <th><h6>Pax         </h6></th>
                                                            <th><h6>Amount      </h6></th>
                                                            <th><h6>Status      </h6></th>
                                                            <th><h6>Action      </h6></th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                <?
                    $c = 1;
                    $sql = "SELECT id, username, nama, kp, status FROM ahli WHERE id NOT IN (1,2,3,4,5)";
                    $result = mysqli_query($con, $sql);
                
//                    while ($row = mysqli_fetch_array($result)) {
                ?>
<!--                                                       <tr class="even small">
                                                            <td><?= $c++ ?>
                                                            <td><?= $tarikh; ?></td>
                                                            <td><?= $row[2] ?></td>
                                                            <td><?= $row[3] ?></td>
                                                            <td><?= $row[3] ?></td>
                                                            <td><a href="#" class="btn btn-success btn-xs">Active</a></td>
                                                            <td><a href="edit.php?id=<?= $row[0] ?>" class="btn btn-primary btn-xs"><i class="fa fa-edit "></i> Edit</a></td>
                                                        </tr>       
                                                       
                 <?// } ?>        
-->
                                                    </tbody>
                                                </table>
                            </div>
                        </div>
                    </div>
                </div>
			</div>
			
			
			<div class="row">
                <div class="col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Redeem History
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-borderless table-hover" id="dataTables-example">
                                                    <thead>
                                                        <tr>
                                                            <th><h6>#           </h6></th>
                                                            <th><h6>Date        </h6></th>
                                                            <th><h6>Package     </h6></th>
                                                            <th><h6>Pax         </h6></th>
                                                            <th><h6>Amount      </h6></th>
                                                            <th><h6>Status      </h6></th>
                                                            <th><h6>Action      </h6></th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                <?
                    $c = 1;
                    $sql = "SELECT id, username, nama, kp, status FROM ahli WHERE id NOT IN (1,2,3,4,5)";
                    $result = mysqli_query($con, $sql);
                
//                    while ($row = mysqli_fetch_array($result)) {
                ?>
<!--                                                       <tr class="even small">
                                                            <td><?= $c++ ?>
                                                            <td><?= $tarikh; ?></td>
                                                            <td><?= $row[2] ?></td>
                                                            <td><?= $row[3] ?></td>
                                                            <td><?= $row[3] ?></td>
                                                            <td><a href="#" class="btn btn-success btn-xs">Active</a></td>
                                                            <td><a href="edit.php?id=<?= $row[0] ?>" class="btn btn-primary btn-xs"><i class="fa fa-edit "></i> Edit</a></td>
                                                        </tr>       
                                                       
                 <?// } ?>        
-->
                                                    </tbody>
                                                </table>
                            </div>
                        </div>
                    </div>
                </div>
			</div>


<?php
    }
?> 



            </div>
        </div>
<?
    include "_footer.php";
}
?>
