<?php
	//find user id
	$getRoomId = isset($_GET['room']) ? $_GET['room'] : "";
	$getDate = isset($_GET['date']) ? $_GET['date'] : "";
	$getUtilityTypeId = isset($_GET['type']) ? $_GET['type'] : "";
	$getAmount = isset($_GET['amount']) ? $_GET['amount'] : "";

	$result = mysqli_query($conn,"SELECT * FROM utilities WHERE r_id=$getRoomId AND u_date='$getDate' AND u_type_id=$getUtilityTypeId");
	$row = mysqli_fetch_assoc($result);
	$utilityId = isset($row['u_id']) ? $row['u_id'] : "";
	$paidBy = isset($row['u_paid_by']) ? $row['u_paid_by'] : "";

	$roomDetail = getDetailFromTable("rooms",$getRoomId);

	$adminDetail = $_SESSION['admin'];
?>
<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="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Room No
								</label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="" class="form-control col-md-3 col-xs-12" name="total" type="currency" value="<?php echo isset($roomDetail['r_no']) ? $roomDetail['r_no'] : ""; ?>" readonly>
								</div>
							</div>
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Month/Year 
								</label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="" class="form-control col-md-3 col-xs-12" name="total" type="currency" value="<?php echo date("F / Y",strtotime($getDate)); ?>" readonly>
								</div>
							</div>
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Utility
								</label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="" class="form-control col-md-3 col-xs-12" name="" type="currency" value="<?php echo getDetailFromTable('utility_type',$getUtilityTypeId)['uty_title']; ?>" readonly>
									<input id="" class="form-control col-md-3 col-xs-12" name="utilityType" type="currency" value="<?php echo $getUtilityTypeId; ?>" hidden>
								</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="" class="form-control col-md-3 col-xs-12" name="amount" type="currency" value="<?php 
									if($getUtilityTypeId == 3 && $getAmount == NULL){
										echo $getAmount = 104.95;}else{
											if($getUtilityTypeId == 6){
												echo $getAmount = $roomDetail['r_rental'];
											}else{
												echo $getAmount;
											}
									} ?>">
								</div>
							</div>
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Paid by</label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<select class="form-control" name="paidBy" required>
										<option value="" <?php echo select($paidBy,''); ?>>PLEASE CHOOSE</option>
										<option value="1" <?php echo select($paidBy,1); ?>>OWNER</option>
										<option value="2" <?php echo select($paidBy,2); ?>>HOST</option>

									</select>
								</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-3 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="update">Update</button>
								</div>
							</div>
						</form>
							<div class="ln_solid"></div>
							<div class="form-group">
								<div class="col-md-6 col-md-offset-3">
									<a href="?pagecontent=utility_update&remove=y&room=<?php echo $getRoomId; ?>&date=<?php echo $getDate; ?>&type=<?php echo $getUtilityTypeId; ?>"><button class="btn btn-danger">Remove</button></a>
								</div>
							</div>
									
					</div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php 

	if(isset($_POST['update']))
	{
		$amount = $_POST['amount'];
		$note = $_POST['note'];
		$paidBy = $_POST['paidBy'];
		
		//admin id
		$adminId = $adminDetail['a_id'];
		
		$result = mysqli_query($conn, "SELECT * FROM utilities WHERE r_id=$getRoomId AND u_date='$getDate' AND u_type_id=$getUtilityTypeId");
		$rowNums = mysqli_num_rows($result);
		
		if($rowNums > 0){
			mysqli_query($conn, "UPDATE utilities SET u_amount='$amount', u_note='$note', u_paid_by=$paidBy WHERE r_id=$getRoomId AND u_date='$getDate' AND u_type_id=$getUtilityTypeId");
		}else{
			mysqli_query($conn, "INSERT INTO utilities (u_type_id, u_date, r_id, u_amount, u_note, u_paid_by, a_id) VALUES ($getUtilityTypeId, '$getDate', $getRoomId, '$amount', '$note', $paidBy, $adminId)");
		};
		
		if($_SESSION['redirect']){ $redirectUrl = $_SESSION['redirect'];
			header("Location: $redirectUrl");
		}
		
		telegramRoomRentalUpdate($getUtilityTypeId,$getDate);
		
		$monthYear = date("F / Y",strtotime($getDate));
		utilityLog(0,$getRoomId,$getUtilityTypeId,$_SESSION['admin']['a_username']." add update RM".$amount." for ".$monthYear);
	}

	if(isset($_REQUEST['remove'])){
		
		$adminName = $_SESSION['admin']['a_username'];
		 $roomNo = getDetailByTable("rooms",$getRoomId)['r_no'];
		 $propertyTitle = getDetailByTable("properties",getDetailByTable("rooms",$getRoomId)['p_id'])['pty_title'];
		 $monthYear = date("F / Y",strtotime($getDate));
		
			//delete paid utilities
			
				$record = "";
				$result = mysqli_query($conn,"SELECT * FROM utility_payment WHERE uty_id=$utilityId");
				while($row = mysqli_fetch_assoc($result)){
					$utilityPaymentId = $row['utyp_id'];
					$utilityPaymentFileName = $row['utyp_file'];
					if(mysqli_query($conn,"DELETE FROM utility_payment WHERE utyp_id=$utilityPaymentId")){
						unlink('uploads/rental/'.$utilityPaymentFileName);
					};
					$record = $record.$utilityPaymentFileName."
";
				}
sendMessage2(-1001209156913,$adminName." removed ".getDetailByTable('utility_type',$getUtilityTypeId)['uty_title']." utility for

Property : ".$propertyTitle."
Room : ".$roomNo."
Month/Year : ".$monthYear."

list of payment removed
".$record);

		mysqli_query($conn, "DELETE FROM utilities WHERE r_id=$getRoomId AND u_date='$getDate' AND u_type_id=$getUtilityTypeId");
		header("Location: ?pagecontent=utility_view&room=$getRoomId&type=$getUtilityTypeId");
		telegramRoomRentalUpdate($getUtilityTypeId,$getDate);
		utilityLog(0,$getRoomId,$getUtilityTypeId,$_SESSION['admin']['a_username']." remove record for ".$monthYear);
	}
?>



