<?php 



	if(!$_SESSION['admin']){exit;}



	date_default_timezone_set('Asia/Kuala_Lumpur');

	

	if(isset($_GET['rvno'])){

		$getDate = getDetailByMysqliWhere('reservation','rv_no',$_GET['rvno'])['rv_ci_date'];

	}else{

		if(isset($_GET['date'])){

			$getDate = $_GET['date'];

		}else{

			header("Location: ?pagecontent=reservations&date=".date("Y-m-d"));

		}

	}



	if(!isset($_GET['rvno']) && !isset($_GET['date'])){exit;}

?>



<!-- page content -->

<div class="right_col" role="main">

	<div class="">

	<div class="page-title">

		<div class="title_left">

		<h3>Reservation <small>Some examples to get you started</small></h3>

		</div>



		<div class="title_right">

		<div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">

			<div class="input-group">

			<input type="text" class="form-control" placeholder="Search for...">

			<span class="input-group-btn">

				<button class="btn btn-default" type="button">Go!</button>

			</span>

			</div>

		</div>

		</div>

	</div>



	<div class="clearfix"></div>



	<div class="row">



		<div class="col-md-12 col-sm-12 col-xs-12">

			<div class="x_panel">

				<div class="x_title">

				<h2>Today's List<small>Users</small></h2>

				<ul class="nav navbar-right panel_toolbox">

					<li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>

					</li>

					<li class="dropdown">

					<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>

					<ul class="dropdown-menu" role="menu">

						<li><a href="#">Settings 1</a>

						</li>

						<li><a href="#">Settings 2</a>

						</li>

					</ul>

					</li>

					<li><a class="close-link"><i class="fa fa-close"></i></a>

					</li>

				</ul>

				<div class="clearfix"></div>

				</div>

				<div class="x_content">

				<p class="text-muted font-13 m-b-30">

					Responsive is an extension for DataTables that resolves that problem by optimising the table's layout for different screen sizes through the dynamic insertion and removal of columns from the table.

				</p>

				

				<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">

					<thead>

					<tr>

						<th>Reservation</th>

						<th>Platform</th>

						<th>Guest</th>

						<th>Room</th>

						<th>Check In/Out</th>

						<th>Night</th>

						<th>CO Time</th>

						<th>Total</th>

						<th>Deposit</th>

						<th>Clear</th>

						<th>Status</th>

						<th>Action</th>

					</tr>

					</thead>

					<tbody>

						<?php

									

							if(isset($_REQUEST['date'])){

								$sql = "SELECT * FROM reservation WHERE '$getDate' BETWEEN rv_ci_date AND DATE_SUB(rv_co_date,INTERVAL 1 DAY) ORDER BY rv_ci_date DESC";

							}

						

							if(isset($_REQUEST['rvno'])){ $getReservationNo = $_GET['rvno'];

								$sql = "SELECT * FROM reservation WHERE rv_no='$getReservationNo'";

							}

						

							$result = mysqli_query($conn,$sql);

							

							while($row = mysqli_fetch_array($result))

							{

								// if($row['rv_status'] >= 2 && filterRoomAdmin($row['r_id'],$_SESSION['admin'][0])==true){

								//     continue;

								// }

						?>

						<tr style="<?php if($row['rv_status'] == 0 && $row['rv_ci_date'] == $getDate){echo 'background-color:#FFD5CC; font-weight: bold;'; }elseif($row['rv_status'] > 1 && $row['r_id']){echo 'background-color:#D6FFD1; font-weight: bold;'; }elseif($row['rv_status'] > 1 && !$row['r_id']){echo 'background-color:#FFFAB3; font-weight: bold;';} ?>">

							<td><a onClick="navigator.clipboard.writeText('<?php echo $row['rv_no']; ?>')"><?php echo $row['rv_no']; ?></a></td>

							

							<td><?php 

								echo platformTitle($row['rv_platform']); echo "<br>";

								echo reservationByOwnerIdToTitle($row['rv_byowner']);

								?></td>

							

							<td><a href="?pagecontent=reservation_communication&rv=<?php echo $row['rv_id']; ?>">

								<?php if($row['rv_guest'])

								{echo $row['rv_guest'];}elseif(empty($row['rv_guest']) && !empty($row['rv_guest_name'])){

									echo $row['rv_guest_name'];

									?> <span style="font-weight: bold; color: red;">(filled by guest)</span><?php

									}else{ ?><span style="color:red;";><?php echo $row['rv_apartment']; ?></span><?php

									} ?></a>

							</td>

							<td><a href="?pagecontent=report_profit_room&room=<?php echo $row['r_id']; ?>"><?php if($row['r_id']){echo getDetailFromTable('properties',getDetailFromTable('rooms',$row['r_id'])['p_id'])['pty_code']." ".getDetailFromTable('rooms',$row['r_id'])[1];} ?></a></td>

							<td><?php echo $row['rv_ci_date']." / ".$row['rv_co_date'];?></td>

							<td><?php echo $row['rv_night'];?></td>

							<td><?php echo date_format(date_create($row['rv_co_time']),"h:ia");?></td>

							<td style="<?php if($row['rv_total'] <= 0){echo 'background-color:#FFD5CC; font-weight: bold;';} ?>">

								<a href="?pagecontent=reservation_view&id=<?php echo $row['rv_id']; ?>"><?php if($row['rv_total']=="0.00"){echo "<span style='font-weight:bold; color:red;'>RM ".$row['rv_total']."</span>";}else{echo "RM ".$row['rv_total'];} ?></a>

								

							</td>

							<td>

								<a href="?pagecontent=reservation_view&id=<?php echo $row['rv_id']; ?>">

									<?php if(getDetailByMysqliWhere('deposit','rv_id',$row['rv_id'])){ ?>

										<span style="font-weight:bold; color:blue;"><?php echo "RM ".getDetailByMysqliWhere('deposit','rv_id',$row['rv_id'])['dp_amount']; ?></span>

									<?php }else{ ?>

										<span style="font-weight:bold; color:red;">NO RECORD</span>

									<?php } ?>

								</a>

							</td>

							<td>

								<a href="?pagecontent=reservation_view&id=<?php echo $row['rv_id']; ?>">

									<?php 

										$totalPaid = totalPaid($row['rv_id'])['totalPaid']; 

										if(!$totalPaid){$totalPaid = 0.00;} 

										if($totalPaid < $row['rv_total']){

											echo "<span style='font-weight:bold; color:red;'>RM ".$totalPaid."</span>";}else{

												echo "<span style='font-weight:bold; color:blue;'>CLEARED</span>";}?>

												</a>

							</td>

							<td>

								<?php if($row['rv_ci_date'] == $getDate){ ?>

								<a href="?pagecontent=reservation_status&id=<?php echo $row['rv_id'];?>">

								<?php

								switch($row['rv_status'])

									{

										case "0": echo '<span style="color:red;font-weight:bold;">Pending</span>';

										break;

										case "1": echo '<span style="color:red;font-weight:bold;">Cancelled</span>';

										break;

										case "2": echo '<span style="color:blue;font-weight:bold;">Confirmed</span>';

										break;

										case "3": echo '<span style="color:blue;font-weight:bold;">Arriving</span>';

										break;

										case "4": echo '<span style="color:blue;font-weight:bold;">Arrived</span>';

										break;

										case "5": echo '<span style="color:blue;font-weight:bold;">Checked In</span>';

										break;

										case "6": echo '<span style="color:blue;font-weight:bold;">Checked Out</span>';

										break;

										case "7": echo '<span style="color:black;font-weight:bold;">Completed</span>';

										break;

										default: echo '<span style="color:red;font-weight:bold;">Unknown</span>';

									}

								?></a>

								<?php }else{ ?>

									<span style="color:black;font-weight:bold;">Staying</span>

								<?php } ?>

							</td>

							<td>

								<a href="?pagecontent=reservation_update&id=<?php echo $row['rv_id']; ?>&date=<?php echo $getDate; ?>"><button class="btn btn-xs btn-primary">Update</button></a>

								

								<a href="index.php?pagecontent=reservation_new&name=<?php echo $row['rv_guest']; ?>&contact=<?php echo $row['rv_contact']; ?>&email=<?php echo $row['rv_email']; ?>&room=<?php echo $row['r_id']; ?>&date=<?php echo $row['rv_co_date']; ?>&price=<?php echo $row['rv_total']; ?>"><button class="btn btn-xs btn-warning">Extend</button></a>

							</td>

						</tr>

						<?php } ?>

					</tbody>

				</table>

				

				

				</div>

			</div>

		</div>

	</div>

	</div>

</div>

<!-- /page content -->



<script>

	$(document).ready(function() {

		$('#datatable-responsive').DataTable({

			"scrollY": "300px", // Set the desired height for your table

			"paging": true,     // Enable pagination

			// Other DataTables options...

		});

	});

</script>



<?php

	function reservationByOwnerIdToTitle($id){

		switch($id){

			case 0 : $title = ""; break;

			case 1 : $title = "By Owner"; break;

		}

		

		return $title;

	}

?>