<?php
	$rid=$_GET['rvid'];
	$result=mysqli_query($conn,"SELECT * FROM reservation AS rv INNER JOIN guest as g ON rv.g_id=g.g_id INNER JOIN room AS r 
	ON rv.r_id=r.r_id LEFT JOIN payment as py ON rv.rv_id=py.rv_id WHERE rv.rv_id=$rid") or dir(mysql_error());
	$row=mysqli_fetch_array($result);
?>

<div class="right_col" role="main">
	<div class="">
		<div class="page-title">
			<div class="title_left">
				<h3>Reservation Details</h3>
			</div>
		</div>
		<div class="row">
			<div class="col-md-12 col-sm-12 col-xs-12">
				<div class="x_panel">
					<div class="x_content">
						<table>
							<tr>
								<td class="text-right"><label class="control-label">Name</label></td>
								<td> : </td>
								<td><?php echo $row['g_full_name'];?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">No Room</label></td>
								<td> : </td>
								<td><?php echo $row['r_no'];?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Check In Date</label></td>
								<td> : </td>
								<td><?php echo $row['rv_ci_date'];?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Time</label></td>
								<td> : </td>
								<td><?php echo $row['rv_ci_time'];?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Night</label></td>
								<td> : </td>
								<td><?php echo $totalNights = $row['rv_night'];?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Check Out Date</label></td>
								<td> : </td>
								<td><?php echo $row['rv_co_date'];?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Time</label></td>
								<td> : </td>
								<td><?php echo $row['rv_co_time'];?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Amount</label></td>
								<td> : </td>
								<td>RM <?php echo $amount = $row['rv_amount'];?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Tax</label></td>
								<td> : </td>
								<td><?php echo $tax = $row['rv_tax'].' %';?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Total Amount</label></td>
								<td> : </td>
								<td>RM 	<?php
											$amount = $row['rv_amount'];
											$totalNights = $row['rv_night'];
											$tax = $row['rv_tax'];
											echo number_format($total=(($amount*$totalNights)*(1.0+($tax/100))),2); 
										?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Payment Collect</label></td>
								<td> : </td>
								<td>RM 	<?php
											$resultAM=mysqli_query($conn,"SELECT sum(py_amount) AS Total FROM payment WHERE rv_id=$rid") or die (mysql_error());
											$rowAm=mysqli_fetch_array($resultAM);
											$totalpay = $rowAm['Total'];
											echo number_format($totalpay,2); 
										?>
								</td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Balance</label></td>
								<td> : </td>
								<td>RM 	<?php
											$resultAM=mysqli_query($conn,"SELECT sum(py_amount) AS Total FROM payment WHERE rv_id=$rid") or die (mysql_error());
											$rowAm=mysqli_fetch_array($resultAM);
											$totalpay = $rowAm['Total'];
											$totalAmount=(($amount*$totalNights)*(1.0+($tax/100)));
											$balance = $totalAmount - $totalpay ;
											echo number_format($balance,2); 
										?>
								</td>
							</tr>
						</table>
					</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_content">
						<table>
							<tr>
								<td><h2>Payments</h2></td>
								<td width="30"></td>
								<td><a href="index.php?pagecontent=payment&rvid=<?php echo $rid;?>"><button type="button" class="btn btn-dark">Add Payment</button></a></td>
							</tr>
						</table>
						
						<table id="datatable" class="table table-striped table-bordered">
							<thead>
								<tr>
									<th>ID</th>
									<th>Date</th>
									<th>Amount</th>
									<th width="150">Payment method</th>
									<th>Note</th>
									<th>Action</th>
								</tr>
							</thead>
							<tbody>
								<?php
									$id=$_GET['rvid'];
									$result=mysqli_query($conn, "SELECT * FROM payment WHERE rv_id=$rid")
									or die (mysql_error());
									while($row = mysqli_fetch_array($result))
									{
								?>
								<tr>
									<td width=""><?php echo $row['py_no'];?></a></td>
									<td><?php echo $row['py_date'];?></td>
									<td width="">RM <?php echo $row['py_amount'];?></td>
									<td width=""><?php echo $row['py_method'];?></td>
									<td><?php echo $row['py_note'];?></td>
									<td>
									    <a href="index.php?pagecontent=reservationid&rvid=<?php echo $rid; ?>&delete=<?php echo $row['py_id'];?>"><button type="button" class="btn btn-danger">Delete</button>
									    <a href="index.php?pagecontent=reservationid&rvid=<?php echo $rid; ?>&valid=<?php echo $row['py_id'];?>"><button type="button" class="btn btn-success">Valid</button>
									</td>
								</tr>
								<?php
									}
								?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
        </div>
    </div>
</div>

<?php
	if(isset($_REQUEST['delete'])){
		
		$id=$_REQUEST['delete'];
		
		mysqli_query($conn,"DELETE FROM payment WHERE py_id=$id") or die (mysql_error());
		
		header('Location: index.php?pagecontent=reservationid&rvid='.$rid);  
	}
?>