<?php $_SESSION['redirect'] = $_SERVER['REQUEST_URI'];
	$getReservationId = isset($_GET['id']) ? $_GET['id'] : "";
	$getReservationPaymentId = isset($_GET['rvpid']) ? $_GET['rvpid'] : 0;
	$getMethod = isset($_GET['method']) ? $_GET['method'] : $getMethod = 1;
	$getDate = isset($_GET['date']) ?  $_GET['date'] : date("Y-m-d");
	$getAmount = isset($_GET['amount']) ? $_GET['amount'] : "0.00";
	$getNote = isset($_GET['note']) ? $_GET['note'] : "";
	$getStatus = isset($_GET['status']) ? $_GET['status'] : 0;
	
	$reservationDetail = isset($getReservationId) ? getDetailByTable('reservation',$getReservationId) : "";
	$roomDetail = isset($reservationDetail['r_id']) ? getDetailByTable('rooms',$reservationDetail['r_id']) : "";
	$propertyDetail = isset($roomDetail['p_id']) ? getDetailByTable('properties',$roomDetail['p_id']) : "";
	$depositDetail = isset($getReservationId) ? getDetailByMysqliWhere('deposit','rv_id',$getReservationId) : "";
	
	$adminDetail = $_SESSION['admin'];
?>

<style>
	.panel-hidden {
		max-height: 0;
		overflow: hidden;
		transition: max-height 0.3s ease-out;
	}

	.panel-hidden.panel-visible {
		max-height: 600px; /* Adjust as needed */
	}
</style>

<div class="right_col" role="main">
	<div class="">
		<div class="page-title">
			<div class="title_left">
				<h3>Reservation Details (RV No: <?php echo $reservationDetail['rv_no'];?> ) <a href="?pagecontent=reservation_update&id=<?php echo $getReservationId; ?>&redirect=?pagecontent=reservation_view&id=<?php echo $getReservationId; ?>"><button class="btn btn-primary">Update</button></a></h3>
			</div>
		</div>
		<div class="clearfix"></div>
		<div class="row">
			
			<div class="col-md-4 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 $reservationDetail['rv_guest'];?> <a href="?pagecontent=reservation_communication&rv=<?php echo $getReservationId; ?>"><button class="btn btn-xs btn-primary">View</button></a></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">RV No</label></td>
								<td> : </td>
								<td><?php echo $reservationDetail['rv_no'];?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">No Room</label></td>
								<td> : </td>
								<td><?php echo isset($reservationDetail['r_id']) ? $roomDetail['r_no'] : "NO RECORD";?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Property</label></td>
								<td> : </td>
								<td><?php echo isset($reservationDetail['r_id']) ? $propertyDetail['p_title'] : "NO RECORD";?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Check In Date</label></td>
								<td> : </td>
								<td><a href="?pagecontent=reservations&date=<?php echo getDetailByTable('reservation',$getReservationId)['rv_ci_date']; ?>"><?php echo getDetailByTable('reservation',$getReservationId)['rv_ci_date']; ?></a></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Time</label></td>
								<td> : </td>
								<td><?php echo getDetailByTable('reservation',$getReservationId)['rv_ci_time']; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Night</label></td>
								<td> : </td>
								<td><?php echo getDetailByTable('reservation',$getReservationId)['rv_night']; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Check Out Date</label></td>
								<td> : </td>
								<td><?php echo getDetailByTable('reservation',$getReservationId)['rv_co_date']; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Time</label></td>
								<td> : </td>
								<td><?php echo getDetailByTable('reservation',$getReservationId)['rv_co_time']; ?></td>
							</tr>
						</table>
					</div>
				</div>
			</div>
			
			<div class="col-md-4 col-sm-12 col-xs-12">
				<div class="x_panel">
					<div class="x_content">
						<table>
							<tr>
								<td class="text-right"><label class="control-label">Total</label></td>
								<td> : </td>
								<td><?php $amount = getDetailByTable('reservation',$getReservationId)['rv_total']; echo "<span style='color:black; font-weight:bold;'>RM ".$amount."</span>"; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Received</label></td>
								<td> : </td>
								<td><?php $totalPaid = totalPaid($getReservationId)['totalPaid']; if($totalPaid){echo "<span style='color:blue; font-weight:bold;'>RM ".$totalPaid."</span>";;}else{echo "<span style='color:red; font-weight:bold;'>NO RECORD</span>";} ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Balance</label></td>
								<td> : </td>
								<td><?php $balance = $amount - $totalPaid; echo "<span style='color:red; font-weight:bold;'>RM ".$balance."</span>"; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Tax</label></td>
								<td> : </td>
								<td><?php echo isset($row['rv_tax']) ? $row['rv_tax'].' %' : ''; ?></td>
							</tr>
							<tr id="tr-deposit">
								<td class="text-right"><label class="control-label">Deposit</label></td>
								<td> : </td>
								<td>
									<?php if(empty($depositDetail['dp_amount'])){ ?>
										<button class="btn_add_deposit btn btn-xs btn-primary">Add Deposit</button>
									<?php }else{ ?>
										<?php echo "RM ".$depositDetail['dp_amount']; ?>
										<a href="?pagecontent=reservation_view&id=<?php echo $getReservationId; ?>&updateDeposit=<?php echo $depositDetail['dp_id']; ?>" class="btn btn-xs btn-primary">Update</a>
									<?php } ?>

                                    <?php echo !isset(getDetailByMysqliWhere('deposit','rv_id',$getReservationId)['a_id']) ? " - upload by Guest via SCI System" : ""; ?>
								</td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Receiver Account</label></td>
								<td> : </td>
								<td><?php echo isset($depositDetail['dp_receiver_acc']) ? $depositDetail['dp_receiver_acc'] : "NO RECORD"; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Receiver Bank</label></td>
								<td> : </td>
								<td><?php echo isset($depositDetail['dp_receiver_bank']) ? $depositDetail['dp_receiver_bank'] : "NO RECORD"; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Receiver Holder</label></td>
								<td> : </td>
								<td><?php echo isset($depositDetail['dp_receiver_holder']) ? $depositDetail['dp_receiver_holder'] : "NO RECORD"; ?></td>
							</tr>
						</table>
					</div>
				</div>
			</div>
        </div>
		
        <div id="panel-payment" class="row panel-hidden">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Add Payment Detail</h2>
						<div class="clearfix"></div>
					</div>

					<div class="x_content">
						<form class="form-horizontal form-label-left" method="post">
							<input type="text" name="rv_id" value="<?php echo $getReservationId; ?>" hidden>
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Date
								</label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="night" class="form-control col-md-3 col-xs-12" name="date" type="date" value="<?php echo $getDate; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="reservationbooking">Method
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<select class="form-control" name="method">
										<option value="0" <?php echo select(0, $getMethod); ?>>Unknown Method</option>
										<option value="1" <?php echo select(1, $getMethod); ?>>Bank In/Cash Deposit (CIMB)</option>
										<option value="2" <?php echo select(2, $getMethod); ?>>Cash</option>
									</select>
								</div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Amount 
								</label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="night" class="form-control col-md-3 col-xs-12" name="amount" type="currency" value="<?php echo $getAmount; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Note</span>
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<textarea id="message" class="form-control" name="note"></textarea>
								</div>
							</div>

							<div class="ln_solid"></div>

							<div class="form-group">
								<div class="col-md-6 col-md-offset-3">
									<button id="send" type="submit" class="btn btn-success" name="addBtn">Add</button>
								</div>
							</div>

						</form>
									
									
					</div>
                </div>
            </div>
        </div>		
		
        <div id="panel-deposit" class="row panel-hidden">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Add Deposit Detail</h2>
						<div class="clearfix"></div>
					</div>

					<div class="x_content">
						<form id="depositForm" class="form-horizontal form-label-left" method="post">
							<input type="text" name="rv_id" value="<?php echo $getReservationId; ?>" hidden>
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Amount </label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="amount" class="form-control col-md-3 col-xs-12" name="amount" type="currency" value="<?php echo $getAmount; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="reservationbooking">Method </label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<select id="method" class="form-control" name="method">
										<option value="0" <?php echo select(0, $getMethod); ?>>Unknown Method</option>
										<option value="1" <?php echo select(1, $getMethod); ?>>Bank In/Cash Deposit (CIMB)</option>
										<option value="2" <?php echo select(2, $getMethod); ?>>Cash</option>
									</select>
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Receive Date </label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="date" class="form-control col-md-3 col-xs-12" name="date" type="date" value="<?php echo $getDate; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Note</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<textarea id="note" class="form-control" name="note"></textarea>
								</div>
							</div>

							<div class="ln_solid"></div>

							<div class="form-group">
								<div class="col-md-6 col-md-offset-3">
									<button id="depositSubmitBtn" type="button" class="btn btn-success" name="depositSubmitBtn">Submit</button>
								</div>
							</div>
						</form>

						<?php
                            //input from dzvntwk.com/sci/v2/dp.php
                            if(isset($_REQUEST['inputDeposit']))
							{
                                $getReservationId = $_GET['id'];
								$postDate = date('Y-m-d');
								$postAmount = $_REQUEST['inputDeposit'];
								$postMethod = 1;
								$postNote = "";
								
								//admin id
								$adminId = $_SESSION['admin']['a_id'];
								
								mysqli_query($conn, "INSERT INTO deposit (rv_id,dp_received_at,dp_amount,dp_method,dp_note,a_id) VALUES ($getReservationId,'$postDate','$postAmount',$postMethod,'$postNote',$adminId)");
								
                                //RECORD LOG
                                $logTitle = ucfirst($adminDetail['a_username'])." add deposit details via Telegram.";
								// logs($logTitle,NULL,$postDepositId);
                                mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");
								
								header("Location: ?pagecontent=reservation_view&id=$getReservationId");
							}
						?>	
									
					</div>
                </div>
            </div>
        </div>
		
		<?php if(isset($_GET['updateDeposit'])){
			$getDepositId = isset($_GET['updateDeposit']) ? $_GET['updateDeposit'] : "";
			$depositDetail = getDetailByTable('deposit',$getDepositId);

			$getRefundReceiverAccount = isset($depositDetail['dp_receiver_acc']) ? $depositDetail['dp_receiver_acc'] : "";
			$getRefundReceiverBank = isset($depositDetail['dp_receiver_bank']) ? $depositDetail['dp_receiver_bank'] : "";
			$getRefundReceiverHolder = isset($depositDetail['dp_receiver_holder']) ? $depositDetail['dp_receiver_holder'] : "";
			
			?>
        <div class="row">
            <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Update Deposit Detail</h2>
						<div class="clearfix"></div>
					</div>

					<div class="x_content">
						<form class="form-horizontal form-label-left" method="post">
							<input type="hidden" name="depositId" value="<?php echo $_GET['updateDeposit']; ?>">
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Amount 
								</label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="night" class="form-control col-md-3 col-xs-12" name="amount" type="currency" value="<?php echo isset($depositDetail['dp_amount']) ? $depositDetail['dp_amount'] : ""; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="reservationbooking">Method
								</label>
								<div class="col-md-6 col-sm-6 col-xs-12">
									<select class="form-control" name="method">
										<option value="0" <?php echo select(0, isset($depositDetail['dp_method']) ? $depositDetail['dp_method'] : 0); ?>>Unknown Method</option>
										<option value="1" <?php echo select(0, isset($depositDetail['dp_method']) ? $depositDetail['dp_method'] : 0); ?>>Bank In/Cash Deposit (CIMB)</option>
										<option value="2" <?php echo select(0, isset($depositDetail['dp_method']) ? $depositDetail['dp_method'] : 0); ?>>Cash</option>
									</select>
								</div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Receive Date 
								</label>
								<div class="col-md-6 col-sm-6 col-xs-12">
									<input id="night" class="form-control col-md-3 col-xs-12" name="date" type="date" value="<?php echo $depositDetail['dp_received_at']; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Note</span>
								</label>
								<div class="col-md-6 col-sm-6 col-xs-12">
									<textarea id="message" class="form-control" name="note"><?php echo isset($depositDetail['dp_note']) ? $depositDetail['dp_note'] : ""; ?></textarea>
								</div>
							</div>

							<div class="ln_solid"></div>

							<div class="form-group">
								<div class="col-md-6 col-md-offset-3">
									<button id="send" type="submit" class="btn btn-success" name="updateDepositBtn">Update</button>
									<a href="?pagecontent=reservation_view&id=<?php echo $getReservationId; ?>&removeDeposit=y" class="btn btn-danger">Remove</a>
								</div>
							</div>

						</form>
									
					</div>
                </div>
            </div>

			
			<div class="col-md-6 col-sm-12 col-xs-12">
				<div class="x_panel">
					<div class="x_title">
						<h2>Receiver Detail</h2>
						<div class="clearfix"></div>
					</div>

					<div class="x_content">
						<form class="form-horizontal form-label-left" method="post">
							<input name="deposit" type="hidden" value="<?php echo $getDepositId; ?>">
							<input name="reservation" type="hidden" value="<?php echo $getReservationId; ?>">
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Account
								</label>
								<div class="col-md-6 col-sm-6 col-xs-12">
									<input class="form-control col-md-3 col-xs-12" type="text" name="receiverAccount" value="<?php echo $getRefundReceiverAccount; ?>">
								</div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Bank 
								</label>
								<div class="col-md-6 col-sm-6 col-xs-12">
									<input class="form-control col-md-3 col-xs-12" type="text" name="receiverBank" value="<?php echo $getRefundReceiverBank; ?>">
								</div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Holder Name 
								</label>
								<div class="col-md-6 col-sm-6 col-xs-12">
									<input class="form-control col-md-3 col-xs-12" type="text" name="receiverHolder" value="<?php echo $getRefundReceiverHolder; ?>">
								</div>
							</div>

							<div class="ln_solid"></div>

							<div class="form-group">
								<div class="col-md-6 col-md-offset-3">
									<button id="send" type="submit" class="btn btn-primary" name="receiverBtn">Update</button>
									<a href="?pagecontent=deposit&clearReceiver=<?php echo $getDepositId; ?>" class="btn btn-danger">Clear</a>
								</div>
							</div>

						</form>
									
									
					</div>
				</div>
    		</div>


        </div>
		<?php } ?>

		<?php
			if(isset($_POST['updateDepositBtn'])){
				$postDepositId = $_POST['depositId'];
				$postDate = $_POST['date'];
				$postAmount = $_POST['amount'];
				$postMethod = $_POST['method'];
				$postNote = $_POST['note'];
				
				//admin id
				$adminId = $_SESSION['admin']['a_id'];
				
				mysqli_query($conn, "UPDATE deposit SET dp_received_at='$postDate', dp_amount='$postAmount', dp_method=$postMethod, dp_note='$postNote', a_id=$adminId WHERE dp_id=$postDepositId");
				
				//RECORD LOG
				$logTitle = ucfirst($adminDetail['a_username'])." update deposit RM ".$postAmount;
				// logs($logTitle,NULL,$postDepositId);
				mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");

				header("Location: ?pagecontent=reservation_view&id=$getReservationId");
			}

			if(isset($_REQUEST['removeDeposit'])){
				$getReservationId = $_GET['id'];
				
				mysqli_query($conn, "DELETE FROM deposit WHERE rv_id=$getReservationId");

				//RECORD LOG
				// $logTitle = ucfirst($adminDetail['a_username'])." remove deposit";
				// logs($logTitle,NULL,$getDepositId);
				// mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$getDepositId,'$ipAddress',$adminId)");
				
				header("Location: ?pagecontent=reservation_view&id=$getReservationId");
			}
		?>
		
		<?php if(isset($_GET['update'])){ ?>
        <div class="row" id="updatePayment">
            <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Update Payment</h2>
						<div class="clearfix"></div>
					</div>

					<div class="x_content">
						<form class="form-horizontal form-label-left" method="post">
							<input type="hidden" name="reservationId" value="<?php echo $getReservationId; ?>">
							<input type="hidden" name="reservationPaymentId" value="<?php echo $getReservationPaymentId; ?>">
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Date
								</label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="night" class="form-control col-md-3 col-xs-12" name="date" type="date" value="<?php echo $getDate; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="reservationbooking">Method
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<select class="form-control" name="method">
										<option value="0" <?php echo select(0, $getMethod); ?>>Unknown Method</option>
										<option value="1" <?php echo select(1, $getMethod); ?>>Bank In/Cash Deposit (CIMB)</option>
										<option value="2" <?php echo select(2, $getMethod); ?>>Cash</option>
									</select>
								</div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Amount 
								</label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="night" class="form-control col-md-3 col-xs-12" name="amount" type="currency" value="<?php echo $getAmount; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Note</span>
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<textarea id="message" class="form-control" name="note"><?php echo $getNote; ?></textarea>
								</div>
							</div>

							<div class="ln_solid"></div>

							<div class="form-group">
								<div class="col-md-6 col-md-offset-3">
									<button id="send" type="submit" class="btn btn-primary" name="updateBtn">Update</button>
									<button id="send" type="submit" class="btn btn-danger" name="removeBtn">Remove</button>
								</div>
							</div>

						</form>
									
									
					</div>
                </div>
            </div>

            <div class="col-md-6 col-sm-6 col-xs-12">
				<div class="x_panel">
					<div class="x_content">
						<img src="uploads/payments/<?php echo getDetailByTable('reservation_payment',$getReservationPaymentId)['rvp_filename']; ?>" width="100%">
					</div>
					
					<div class="x_content">
						
						<form class="form-horizontal form-label-left" method="post" enctype="multipart/form-data">
							<input type="hidden" name="reservationId" value="<?php echo $getReservationId; ?>">
							<input type="hidden" name="reservationPaymentId" value="<?php echo $getReservationPaymentId; ?>">
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Upload 
								</label>
								<div class="col-md-9 col-sm-9 col-xs-12">
									<input id="night" class="form-control col-md-3 col-xs-12" name="fileToUpload" type="file" >
								</div>
							</div>

							<div class="ln_solid"></div>

							<div class="form-group">
								<div class="col-md-6 col-md-offset-3">
									<button id="send" type="submit" class="btn btn-success" name="uploadBtn">Upload</button>

									<button type="submit" class="btn btn-danger" name="removePhoto" <?php if(!getDetailByTable('reservation_payment',$getReservationPaymentId)['rvp_filename']){echo "disabled";} ?>>Remove</button> | 

                                    <?php if(getDetailByTable('reservation_payment',$getReservationPaymentId)['rvp_verified_by']){ ?>
                                    <button type="submit" class="btn btn-warning" name="unverifiy" <?php if(!getDetailByTable('reservation_payment',$getReservationPaymentId)['rvp_filename']){echo "disabled";} ?>>Unverify</button>
                                    <?php }else{ ?>
                                    <button type="submit" class="btn btn-primary" name="verify" <?php if(!getDetailByTable('reservation_payment',$getReservationPaymentId)['rvp_filename']){echo "disabled";} ?>>Verify</button>
                                    <?php } ?>
								</div>
							</div>
						</form>

                        

					</div>
				</div>
			</div> 
		
		
        </div>
		<?php } ?>

        <?php
                            //UPLOAD RECEIPT
                            if(isset($_POST['uploadBtn'])){
                                
                                if(getDetailByTable("reservation_payment",$getReservationPaymentId)['rvp_filename']){
                                    $filename = getDetailByTable("reservation_payment",$getReservationPaymentId)['rvp_filename'];
                                    unlink("uploads/payments/".$filename);
                                }
                                    
                                //image resize
                                if(is_array($_FILES)) {

                                    $uploadedFile = $_FILES['fileToUpload']['tmp_name']; 
                                    $sourceProperties = getimagesize($uploadedFile);
                                    $imageType = $sourceProperties[2];

                                    switch ($imageType) {

                                        case IMAGETYPE_PNG:
                                            $imageSrc = imagecreatefrompng($uploadedFile); 
                                            imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
                                            break;           

                                        case IMAGETYPE_JPEG:
                                            $imageSrc = imagecreatefromjpeg($uploadedFile); 
                                            imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
                                            break;

                                        case IMAGETYPE_GIF:
                                            $imageSrc = imagecreatefromgif($uploadedFile); 
                                            imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1]);
                                            break;

                                        default:
                                            echo "Invalid Image type.";
                                            exit;
                                            break;
                                    }
                                }

                                //upload receipt
                                $errors = array();
                                $file_name = $_FILES['fileToUpload']['name'];
                                $file_size =$_FILES['fileToUpload']['size'];
                                $file_tmp = $_FILES['fileToUpload']['tmp_name'];
                                $file_type = $_FILES['fileToUpload']['type'];

                                $file_ext = strtolower(end(explode('.',$_FILES['fileToUpload']['name'])));

                                $extensions = array("jpeg","jpg","png");

                                if(in_array($file_ext,$extensions)=== false){
                                    $errors[] = "extension not allowed, please choose a JPEG or PNG file.";
                                }

                                if($file_size > 2097152){
                                    $errors[] = 'File size must be excately 2 MB';
                                }

                                    $file_name = $getReservationPaymentId."_".time().".".$file_ext;
                                    $filePath = "uploads/payments/".$file_name;

                                if(empty($errors) == true){

                                    if(move_uploaded_file($file_tmp,$filePath)){
                                        $adminId = $_SESSION['admin']['a_id'];

                                        mysqli_query($conn, "UPDATE reservation_payment SET rvp_filename='$file_name', rvp_uploaded_by=$adminId WHERE rvp_id=$getReservationPaymentId");

                                        $adminName = isset($adminDetail['a_username']) ? $adminDetail['a_username'] : $_SESSION['staff']['s_nickname'];
                                        $bookingNo = getDetailByTable("reservation",$getReservationId)['rv_no'];
                                        $guest = getDetailByTable("reservation",$getReservationId)['rv_guest'];
                                        $date = getDetailByTable("reservation",$getReservationId)['rv_ci_date'];
                                        // $date = getDetailByTable("reservations",$getReservationId)['rv_ci_date'];

                                        sendMessage2(-1001209156913,$adminName." upload payment receipt for

Booking No : ".$bookingNo."
Guest : ".$guest."
Check In : ".$date."
");
                                        sendPhoto(-1001209156913,"https://admin.homestay.dzvisual.my/production/".$filePath);
                                        
                                        paymentLog($getReservationId,$adminName." add payment receipt photo for ".$date);
										// logs("Admin ".$adminName." uploaded payment receipt.",NULL,NULL,$getReservationId);
                                        // paymentLog($getReservationId,"test");
                                    }
                                }
                                
                                //$postMonthYear = date("F / Y",strtotime(getDetailByTable("utilities",$getUtilityId)['u_date']));
                                //utilityLog(0,$getRoomId,$getUtilityTypeId,$adminDetail['a_username']." add photo for ".$postMonthYear);
                                
                                if($_SESSION['redirect']){ $redirectUrl = $_SESSION['redirect'];
                                    header("Location: $redirectUrl");
                                }
                            }

                            function imageResize($imageSrc,$imageWidth,$imageHeight) {

                                $newImageWidth =200;
                                $newImageHeight =200;

                                $newImageLayer=imagecreatetruecolor($newImageWidth,$newImageHeight);
                                imagecopyresampled($newImageLayer,$imageSrc,0,0,0,0,$newImageWidth,$newImageHeight,$imageWidth,$imageHeight);

                                return $newImageLayer;
                            }

                            //REMOVE RECEIPT
                            if(isset($_POST['removePhoto'])){
                                
                                if(getDetailByTable("reservation_payment",$getReservationPaymentId)['rvp_filename']){
                                    $expensesFilename = getDetailByTable("reservation_payment",$getReservationPaymentId)['rvp_filename'];
                                    unlink("uploads/payments/".$expensesFilename);
                                    
                                    if(mysqli_query($conn, "UPDATE reservation_payment SET rvp_filename='',rvp_verified_by='',rvp_uploaded_by='' WHERE rvp_id=$getReservationPaymentId")){
                                        
                                        $adminName = $adminDetail['a_username'];
                                        $bookingNo = getDetailByTable("reservation",$getReservationId)['rv_no'];
                                        $guest = getDetailByTable("reservation",$getReservationId)['rv_guest'];
                                        $date = getDetailByTable("reservation",$getReservationId)['rv_ci_date'];

                                        sendMessage2(-1001209156913,$adminName." removed payment's receipt photo for

Booking No : ".$bookingNo."
Guest : ".$guest."
Check In : ".$date."
");
                                    }
                                }
                                
                                paymentLog($getReservationId,$adminName." remove payment receipt photo for ".$date);
								// logs("Admin ".$adminName." removed payment receipt.",NULL,NULL,$getReservationId);
                                
                                if($_SESSION['redirect']){ $redirectUrl = $_SESSION['redirect'];
                                    header("Location: $redirectUrl");
                                }
                            }

                            //VERIFY RECEIPT
                            if(isset($_POST['verify'])){
                                $adminId = $_SESSION['admin']['a_id'];

                                if(mysqli_query($conn, "UPDATE reservation_payment SET rvp_verified_by=$adminId WHERE rvp_id=$getReservationPaymentId")){
                                    
                                    $adminName = $adminDetail['a_username'];
                                    $bookingNo = getDetailByTable("reservation",$getReservationId)['rv_no'];
                                    $guest = getDetailByTable("reservation",$getReservationId)['rv_guest'];
                                    $date = getDetailByTable("reservation",$getReservationId)['rv_ci_date'];

                                    sendMessage2(-1001209156913,$adminName." verify payments receipt photo for

Booking No : ".$bookingNo."
Guest : ".$guest."
Check In : ".$date."
");
                                }
                                    
                                paymentLog($getReservationId,$adminName." verify payment receipt photo for ".$date);
								// logs("Admin ".$adminName." verify payment receipt.",NULL,NULL,$getReservationId);
                                
                                if($_SESSION['redirect']){ $redirectUrl = $_SESSION['redirect'];
                                    header("Location: $redirectUrl");
                                }
                            }

                            //VERIFY RECEIPT
                            if(isset($_POST['unverifiy'])){
                                // $adminId = $_SESSION['admin']['a_id'];

                                if(mysqli_query($conn, "UPDATE reservation_payment SET rvp_verified_by='' WHERE rvp_id=$getReservationPaymentId")){
                                    
                                    $adminName = $adminDetail['a_username'];
                                    $bookingNo = getDetailByTable("reservation",$getReservationId)['rv_no'];
                                    $guest = getDetailByTable("reservation",$getReservationId)['rv_guest'];
                                    $date = getDetailByTable("reservation",$getReservationId)['rv_ci_date'];

                                    sendMessage2(-1001209156913,$adminName." remove verification of payments receipt photo for

Booking No : ".$bookingNo."
Guest : ".$guest."
Check In : ".$date."
");
                                }
                                    
                                paymentLog($getReservationId,$adminName." remove verification of payment receipt photo for ".$date);
								// logs("Admin ".$adminName." removed verification of payment receipt.",NULL,NULL,$getReservationId);
                                
                                if($_SESSION['redirect']){ $redirectUrl = $_SESSION['redirect'];
                                    header("Location: $redirectUrl");
                                }
                            }
                        ?>
		
		<div class="clearfix"></div>
		<div class="row" id="paymentTable">
			<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>
									<button id="btn-add-payment" type="button" class="btn btn-dark">Add Payment</button>
									<a href="index.php?pagecontent=reservation_view&clear=y&id=<?php echo $getReservationId;?>&amount=<?php echo $amount; ?>&date=<?php echo $reservationDetail['rv_ci_date']; ?>&method=1&note=Paid by Platform"><button type="button" class="btn btn-dark">Ful Clear</button></a>
								</td>
							</tr>
						</table>
						
						<table id="tablePayments" class="table table-striped table-bordered"></table>
					</div>
				</div>
			</div>
        </div>

		<script>
			function displayTable() {
				// Fetch PHP script using XMLHttpRequest (or fetch API)
				var xhr = new XMLHttpRequest();
				xhr.onreadystatechange = function() {
					if (xhr.readyState == 4 && xhr.status == 200) {
						// Insert the fetched HTML content into the table container
						console.log(xhr.responseText)
						document.getElementById("tablePayments").innerHTML = xhr.responseText;
					}
				};

				var phpParamValue = "<?php echo $getReservationId; ?>";
				var param = "rv_id=" + encodeURIComponent(phpParamValue);
				var url = "reservation_view_payment_table.php?" + param;
				xhr.open("GET", url, true);
				xhr.send();
			}

			// Call the function to display the table when the page loads
			window.onload = function() {
				displayTable();
			};
		</script>

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

	  <div class="col-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Payments Activity Log</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">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Log</th>
				  <th>Date/Time</th>
				  <th>Admin</th>
				  <th>Action</th>
				</tr>
			  </thead>
			  <tbody>
				<?php $no = 0; $result = mysqli_query($conn,"SELECT * FROM reservation_payment_log WHERE rv_id=$getReservationId ORDER BY rvpl_id DESC");
				while($row = mysqli_fetch_assoc($result)){
				?>
				<tr>
				  <th scope="row"><?php echo ++$no; ?></th>
				  <td><?php echo $row['rvpl_report']; ?></td>
				  <td><?php echo $row['timestamp']; ?></td>
				  <td><?php echo getDetailFromTable('admin',$row['a_id'])['a_username']; ?></td>
				  <td></td>
				</tr>
				  <?php } ?>
			  </tbody>
			</table>

		  </div>
		</div>
	  </div>

    </div>
</div>

<?php

if(isset($_POST['addBtn']))
	{
		$postDate = $_POST['date'];
		$postAmount = $_POST['amount'];
		$postMethod = $_POST['method'];
		$postNote = $_POST['note'];
		
		//admin id
		$adminId = $_SESSION['admin']['a_id'];
		$adminName = $adminDetail['a_username'];
		
		mysqli_query($conn, "INSERT INTO reservation_payment (rv_id,rvp_date,rvp_amount,rvp_method,rvp_note,a_id) VALUES ($getReservationId,'$postDate','$postAmount',$postMethod,'$postNote',$adminId)");

        paymentLog($getReservationId,"Admin ".$adminName." add payment record for ".$postDate." - amount RM".$postAmount);
		// logs("Admin ".$adminName." add payment record for ".$postDate." - amount RM".$postAmount,NULL,NULL,$getReservationId);
		
		header("Location: ?pagecontent=reservation_view&id=$getReservationId");
	}

if(isset($_REQUEST['clear']))
	{
		//admin id
		$adminId = $_SESSION['admin']['a_id'];
		$adminName = $adminDetail['a_username'];
		
		mysqli_query($conn, "INSERT INTO reservation_payment (rv_id,rvp_date,rvp_amount,rvp_method,rvp_note,rvp_verified_by,a_id) VALUES ($getReservationId,'$getDate','$getAmount',$getMethod,'$getNote',$adminId,$adminId)");
        
        paymentLog($getReservationId,"Admin ".$adminName." clear payment record for ".$getDate." - amount RM".$getAmount);
		// logs("Admin ".$adminName." clear payment record for ".$getDate." - amount RM".$getAmount,NULL,NULL,$getReservationId);
		
		header("Location: ?pagecontent=reservation_view&id=$getReservationId");
	}

if(isset($_POST['updateBtn']))
	{
		$postDate = $_POST['date'];
		$postAmount = $_POST['amount'];
		$postMethod = $_POST['method'];
		$postNote = $_POST['note'];

		$adminName = $adminDetail['a_username'];
		
		mysqli_query($conn, "UPDATE reservation_payment SET rvp_date='$postDate', rvp_amount='$postAmount', rvp_method=$postMethod, rvp_note='$postNote' WHERE rvp_id='$getReservationPaymentId'");

        paymentLog($getReservationId,"Admin ".$adminName." update payment record for ".$postDate." - amount RM".$postAmount);
		// logs("Admin ".$adminName." update payment record for ".$postDate." - amount RM".$postAmount,NULL,NULL,$getReservationId);
		
		header("Location: ?pagecontent=reservation_view&id=$getReservationId");
	}

if(isset($_POST['removeBtn']))
	{
        $date = getDetailByTable('reservation_payment',$getReservationPaymentId)['rvp_date'];
        $adminName = $adminDetail['a_username'];
        paymentLog($getReservationId,"Admin ".$adminName." remove report for.".$date);
		// logs("Admin ".$adminName." remove report for ",NULL,NULL,$getReservationId);

		mysqli_query($conn, "DELETE FROM reservation_payment WHERE rvp_id='$getReservationPaymentId'");
		
		header("Location: ?pagecontent=reservation_view&id=$getReservationId#paymentTable");
	}

if(isset($_REQUEST['status']))
	{
		mysqli_query($conn, "UPDATE reservation_payment SET rvp_status='$getStatus' WHERE rvp_id='$getReservationPaymentId'");
		
		header("Location: ?pagecontent=reservation_view&id=$getReservationId");
	}

    if(isset($_POST['receiverBtn']))
    {
        $postDepositId = $_POST['deposit'];
        $postReservationId = $_POST['reservation'];
        $postReceiverAccount = $_POST['receiverAccount'];
        $postReceiverBank = $_POST['receiverBank'];
        $postReceiverHolder = $_POST['receiverHolder'];
        
        mysqli_query($conn, "UPDATE deposit SET dp_receiver_acc='$postReceiverAccount', dp_receiver_bank='$postReceiverBank', dp_receiver_holder='$postReceiverHolder' WHERE dp_id=$postDepositId");
        
        //RECORD LOG
        $logTitle = ucfirst($adminDetail['a_username'])." update receiver details.";
		// logs($logTitle,NULL,$postDepositId);
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");

		header("Location: ?pagecontent=reservation_view&id=$postReservationId");
    }

    if(isset($_REQUEST['clearReceiver']))
    {
        $postDepositId = $_REQUEST['clearReceiver'];
        $postReservationId = $_POST['reservation'];
        
        mysqli_query($conn, "UPDATE deposit SET dp_receiver_acc=NULL, dp_receiver_bank=NULL, dp_receiver_holder=NULL WHERE dp_id=$postDepositId");
        
        //RECORD LOG
        $logTitle = ucfirst($adminDetail['a_username'])." clear receiver details.";
		// logs($logTitle,NULL,$postDepositId);
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");

		header("Location: ?pagecontent=reservation_view&id=$postReservationId");
    }
?>

<script>
	document.querySelector('.btn_add_deposit').addEventListener('click',function(){
		// console.log('clicked')
		document.getElementById('panel-deposit').classList.add('panel-visible');
	})

	document.getElementById('btn-add-payment').addEventListener('click',function(){
		// console.log('clicked')
		document.getElementById('panel-payment').classList.add('panel-visible');
	})

	document.getElementById('depositSubmitBtn').addEventListener('click', function() {
		var formData = new FormData(document.getElementById('depositForm'));
		var http = new XMLHttpRequest();
		console.log(formData);
		http.open('POST', 'reservation_view_deposit_update.php', true);
		http.onreadystatechange = function() {
			if (http.readyState === 4 && http.status === 200) {
				// console.log(http.responseText);
				// Optionally, you can perform actions after successful submission
				// For example, display a success message or redirect to another page

				location.reload();
			}
		};
		http.send(formData);

		document.querySelector('.panel-hidden').classList.remove('panel-visible');
	});
</script>