<?php
	//find user id
	$reservationId = isset($_GET['id']) ?  $_GET['id'] : "";

	$getDate = isset($_GET['date']) ? $_GET['date'] : "";

	$result = mysqli_query($conn, "SELECT * FROM reservation AS RV LEFT JOIN users AS U ON RV.u_id=U.u_id WHERE RV.rv_id=$reservationId");
	$row = mysqli_fetch_assoc($result);

	$reservationDetail = getDetailByTable('reservation',$reservationId);

	$userFullname = $row['u_fullname'];
	$userContact = $row['u_contact'];

	$rvGuest = $row['rv_guest'];
	$rvContact = $row['rv_contact'];
	$rvEmail = $row['rv_email'];
	$rvApartment = $row['rv_apartment'];

	$roomId = $row['r_id'];
	//$checkIn = $row['rv_ci_date'];
	$checkInTime = $row['rv_ci_time'];
	$checkOutTime = $row['rv_co_time'];
	$noNight = $row['rv_night'];
	$bookingNo = $row['rv_no'];
	$platformId = $row['rv_platform'];
	$byowner = $row['rv_byowner'];
	$note = $row['rv_notes'];
	$total = $row['rv_total'];
	$totalImport = $row['rv_total_by_import'];
	$cleaningFee = $row['rv_cleaning_fee'] ? $row['rv_cleaning_fee'] : getDetailByTable('rooms',$roomId)['r_cleaning_fee'];
	$respondioId = $row['rv_respondio_id'];
	$airbnbId = $row['rv_airbnb_id'];
	$occuptionTypeId = $reservationDetail['rv_occ_id'];
	$roomBathroomId = $reservationDetail['rv_room_bathroom'];
?>

<div class="right_col" role="main">
    <div class="">
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Reservation</h2>
						<div class="clearfix"></div>
					</div>

					<div class="x_content">
						<form class="form-horizontal form-label-left" method="post">
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">RV Guest Name</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="name" class="form-control col-md-7 col-xs-12" name="rvGuest" type="text" value="<?php if($rvGuest){echo $rvGuest;}else{echo getDetailByTable('reservation',$reservationId)['rv_guest_name'];}; ?>">
							    </div>
                                <?php if(empty($rvGuest) && !empty(getDetailByTable('reservation',$reservationId)['rv_guest_name'])){ ?>
                                <span style="font-weight: bold; color: red;">filled by guest</span>
                                <?php } ?>
							</div>
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">RV Contact</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="name" class="form-control col-md-7 col-xs-12" name="rvContact" type="text" value="<?php if($rvContact){echo $rvContact;}else{echo getDetailByTable('reservation',$reservationId)['rv_guest_contact'];}; ?>">
							    </div>
                                <?php if(empty($rvContact) && !empty(getDetailByTable('reservation',$reservationId)['rv_guest_contact'])){ ?>
                                <span style="font-weight: bold; color: red;">filled by guest</span>
                                <?php } ?>
							</div>
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">RV E-Mail</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="name" class="form-control col-md-7 col-xs-12" name="rvEmail" type="text" value="<?php echo $rvEmail; ?>">
							    </div>
							</div>
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Apartment</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="name" class="form-control col-md-7 col-xs-12" name="rvApartment" type="text" value="<?php echo $rvApartment; ?>" readonly>
							    </div>
							</div>

							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Room No</label>
								<div class="col-md-4 col-sm-3 col-xs-12">

									<select class="form-control" name="roomId" <?php select($row['r_id'], $roomId); ?>>
										<option value="0" <?php echo select($row['r_id'],$roomId); ?>>NO ROOM SELECTED</option>

										<?php 
											$result=mysqli_query($conn, "SELECT * FROM rooms WHERE r_status=1 ORDER BY p_id ASC, r_no ASC") or die (mysql_error());

											while($row = mysqli_fetch_array($result))
											{
                                                // if(filterRoomAdmin($row['r_id'],$_SESSION['admin']['a_id'])==true){continue;};
												$getCIDate = getDetailFromTable('reservation',$reservationId)['rv_ci_date'];
												$rvStatus = getRVByDateRoom($row['r_id'],date('Y-m-d'))['rv_status'];
												
												if($rvStatus >= 2){$fontWeight = "bold"; $fontColor = "blue";
																  }else{
													$fontWeight = "light"; $fontColor = "black";
												}
										
										?>
										
										<option style="font-weight:<?php echo $fontWeight;?>; color:<?php echo $fontColor;?>;" value="<?php echo $row['r_id'];?>" <?php echo select($row['r_id'],$roomId); ?>><?php echo $row['r_no'];?> - <?php echo getDetailFromTable('properties',$row['p_id'])[1];?></option>

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

                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="reservationbooking">Occuption Type
                                </label>
                                <div class="col-md-4 col-sm-3 col-xs-12">
                                    <select class="form-control" name="occuptionType" id="dropdown" onchange="updateInput()">
                                        <?php foreach(occuptionType('ALL') as $item){ ?>
                                        <option value="<?php echo $item['id']; ?>" <?php echo select($item['id'], $occuptionTypeId); ?>><?php echo $item['title']; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                            </div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="reservationbooking">Room & Bathroom
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<select class="form-control" name="roomBathroom" id="dropdown" onchange="updateInput()">
										<?php foreach(roomBathroom('ALL') as $item){ ?>
										<option value="<?php echo $item['id']; ?>" <?php echo select($item['id'], $roomBathroomId); ?>><?php echo $item['title']; ?></option>
										<?php } ?>
									</select>
								</div>
							</div>
							<!-- ALTER TABLE `reservation` ADD `rv_room_bathroom` INT(155) NULL DEFAULT '1' AFTER `rv_occ_id`; -->

							<script>
								window.addEventListener('load', function() {
									var inputA = document.getElementById("inputA");
									// inputA.value = "30"; // set inputB to default value of 40
									var phpValue = <?php echo json_encode(getDetailFromTable('reservation',$reservationId)['rv_cleaning_fee']); ?>
									// any other code you want to run on page load
									if (phpValue !== null && phpValue !== undefined) {
										inputA.value = phpValue;
									} else {
										// If PHP variable is not available, set a default value
										inputA.value = "30";
									}
								});

								function updateInput() {
									var dropdown = document.getElementById("dropdown");
									var selectedValue = dropdown.options[dropdown.selectedIndex].value;
									var inputA = document.getElementById("inputA");
									
									switch(selectedValue) {
										case "0":
											inputA.value = "30";
											break;
										case "1":
											inputA.value = "20";
											break;
										case "2":
											inputA.value = "20";
											break;
										case "3":
											inputA.value = "20";
											break;
										default:
											inputA.value = "30";
									}
								}
							</script>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="">Check In Date
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="night" class="form-control col-md-4 col-xs-12" name="checkInDate" type="date" value="<?php echo getDetailFromTable('reservation',$reservationId)['rv_ci_date']; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="">Check In Time
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="night" class="form-control col-md-4 col-xs-12" name="checkInTime" type="time" value="<?php echo $checkInTime; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="">Check Out Time
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="night" class="form-control col-md-4 col-xs-12" name="checkOutTime" type="time" value="<?php echo $checkOutTime; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Nights <span class="required">*</span>
								</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="nightNo" type="number" value="<?php echo $noNight; ?>">
								</div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="reservationbooking">Platform
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<select class="form-control" name="platformId">
										<option value="1" <?php echo select(1, $platformId); ?>>Direct</option>

										<option value="2" <?php echo select(2, $platformId); ?>>Booking.com</option>

										<option value="3"<?php echo select(3, $platformId); ?>>Airbnb</option>
										
										<option value="4"<?php echo select(4, $platformId); ?>>Agoda</option>
									</select>
								</div>
							</div>
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Booking ID</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="name" class="form-control col-md-7 col-xs-12" name="bookingNo" type="text" value="<?php echo $bookingNo; ?>">
							    </div>
							</div>
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Respondio ID</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="name" class="form-control col-md-7 col-xs-12" name="respondioId" type="text" value="<?php echo $respondioId; ?>">
							    </div>
							</div>
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Airbnb ID</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="name" class="form-control col-md-7 col-xs-12" name="airbnbId" type="text" value="<?php echo $airbnbId; ?>">
							    </div>
							</div>
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Vehicle Plat No</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="name" class="form-control col-md-7 col-xs-12" name="platNo" type="text" value="<?php echo getDetailFromTable('reservation',$reservationId)['rv_guest_platno']; ?>">
							    </div>
							</div>

							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Managed by Owner?</span>
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input type="radio" class="js-switch" name="managedByOwner" value="1" <?php echo checked($byowner,1); ?> /> Yes
									<input type="radio" class="js-switch" name="managedByOwner" value="0" <?php echo checked($byowner,0); ?> /> No
								</div>
							</div>
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Total</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="name" class="form-control col-md-7 col-xs-12" name="total" type="text" value="<?php echo $total; ?>">
							    </div>
							</div>
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Total (Import)</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="name" class="form-control col-md-7 col-xs-12" name="total" type="text" value="<?php echo $totalImport; ?>" disabled>
							    </div>
							</div>
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Cleaning Fee</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<input id="inputA" class="form-control col-md-7 col-xs-12" name="cleaningFee" type="text" value="">
							    </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 $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="submit">Update</button>
									<button id="send" type="remove" class="btn btn-danger" name="remove">Remove</button>

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

<?php 

	if(isset($_POST['submit'])){
		$postRVGuest = $_POST['rvGuest'];
		$postRVContact = $_POST['rvContact'] ? fixWhatsappNo($_POST['rvContact']) : NULL;
		$postRVEmail = $_POST['rvEmail'];

		$roomId = $_POST['roomId'];
		$checkInDate = $_POST['checkInDate'];
		$checkInTime = $_POST['checkInTime'];
		$checkOutTime = $_POST['checkOutTime'];
		$nightNo = $_POST['nightNo'];
		$checkOutDate = addDaysToDate($checkInDate,$nightNo,"+");
		$platformId = $_POST['platformId'];
		$managedByOwner = $_POST['managedByOwner'];
		$bookingNo = $_POST['bookingNo'];
		$note = $_POST['note'];
		$total = $_POST['total'];
		$cleaningFee = $_POST['cleaningFee'];
		$respondioId = $_POST['respondioId'];
		$platNo = $_POST['platNo'];
		$airbnbId = $_POST['airbnbId'];
		$occuptionTypeId = $_POST['occuptionType'];
		$roomBathroomId = $_POST['roomBathroom'];

		$query = "UPDATE reservation SET 
            rv_guest='$postRVGuest', 
            rv_contact='$postRVContact', 
            rv_email='$postRVEmail', 
            r_id=$roomId, 
            rv_ci_date='$checkInDate', 
            rv_co_date='$checkOutDate', 
            rv_ci_time='$checkInTime', 
            rv_co_time='$checkOutTime', 
            rv_night=$nightNo, 
            rv_no='$bookingNo', 
            rv_platform=$platformId, 
            rv_total='$total', 
            rv_cleaning_fee='$cleaningFee', 
            rv_respondio_id='$respondioId', 
            rv_airbnb_id='$airbnbId', 
            rv_guest_platno='$platNo', 
            rv_notes='$note', 
            rv_occ_id=$occuptionTypeId, 
            rv_room_bathroom=$roomBathroomId 
          WHERE rv_id=$reservationId";
		mysqli_query($conn, $query);

		
		//$redirect = urldecode($_GET['redirect']);
		header('Location: ?pagecontent=reservation_status&id='.$reservationId.'&date='.$getDate);
		//header('Location: ?pagecontent=reservation_communication&rv='.$reservationId);

		if($postRVContact){
			sendMessage4('Guest details updated
Guest: '.$postRVGuest.'
Contact: '.$postRVContact.'

wa.me/'.$postRVContact);
		}else{
			sendMessage4('Guest details updated
Guest: '.$postRVGuest.'

THIS GUEST HAD NO CONTACT UPDATED');
		}
		
		tgRVTodayEarned($checkInDate,-550653212);
		
		$adminUsername = $_SESSION['admin']['a_username'];
		reservationLog($reservationId,$adminUsername." update reservation info"); //to be removed
		logs("Admin ".$adminUsername." updates reservation info.",$reservationId);
	}

	if(isset($_POST['remove'])){
		
		mysqli_query($conn, "DELETE FROM housekeep WHERE rv_id=$reservationId");
		mysqli_query($conn, "DELETE FROM reservation WHERE rv_id=$reservationId");
        
        $depositFilename = getDetailByMysqliWhere('deposit','rv_id',$reservationId)['dp_refund_file'];
        unlink('uploads/deposit/'.$depositFilename);
		mysqli_query($conn, "DELETE FROM deposit WHERE rv_id = $reservationId");
		
		header('Location: ?pagecontent=reservations&date='.$getDate);
	}

?>