<?php $_SESSION['redirect'] = $_SERVER['REQUEST_URI'];
	$getRoomId = $_GET['room'];
?>

		
<div class="right_col" role="main">
	<div class="">
		<div class="row">
			<div class="page-title">
				<div class="title_left">
					<h3>Rental Details</h3>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-md-3 col-sm-6 col-xs-12">
				<div class="x_panel">
					<div class="x_content">
						<table>
							<tr>
								<td class="text-right"><label class="control-label">Owner Name</label></td>
								<td> : </td>
								<td><?php echo getDetailByTable('owners',getDetailByTable('rooms',$getRoomId)['o_id'])['o_name']; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">No Room</label></td>
								<td> : </td>
								<td><?php echo getDetailByTable('rooms',$getRoomId)['r_no'];?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Property</label></td>
								<td> : </td>
								<td><?php echo getDetailByTable('properties',getDetailByTable('rooms',$getRoomId)['p_id'])['pty_title']; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Rental</label></td>
								<td> : </td>
								<td>RM <?php $getRentalRate = getDetailByTable('rooms',$getRoomId)['r_rental']; echo $getRentalRate; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Rental Start Date</label></td>
								<td> : </td>
								<td><?php $rentalStartDate = getDetailByTable('rooms',$getRoomId)['r_rental_date_start']; if($rentalStartDate == "0000-00-00"){$rentalStartDate = "2017-01-01";} echo $rentalStartDate; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Rental End Date</label></td>
								<td> : </td>
								<td><?php $endDate = getDetailByTable('rooms',$getRoomId)['r_rental_date_end']; if($endDate=="0000-00-00"){echo "currently rent";}else{echo $endDate;} ?></td>
							</tr>
						</table>
						
						<br>
						<p>
							<a href="?pagecontent=room_update&room=<?php echo $getRoomId; ?>"><button class="btn btn-primary">Update Room</button></a>
						</p>
						
					</div>
				</div>
			</div>
			
			<div class="col-md-3 col-sm-6 col-xs-12">
				<div class="x_panel">
					<div class="x_content">
						<table>
							<tr>
								<td class="text-right"><label class="control-label">Total Rental</label></td>
								<td> : </td>
								<td><?php echo " RM ".rentalTotal($getRoomId)['rentalTotal']; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Duration</label></td>
								<td> : </td>
								<td><?php echo durationInDays($rentalStartDate,date("Y-m-d")); ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Total Paid</label></td>
								<td> : </td>
								<td><?php echo " RM ".utilityPaymentTotal($getRoomId)['paymentTotal']; ?></td>
							</tr>
							<tr>
								<td class="text-right"><label class="control-label">Total Outstanding</label></td>
								<td> : </td>
								<td><span style="color:red; font-weight: bold;">RM <?php echo rentalTotal($getRoomId)['rentalTotal'] - utilityPaymentTotal($getRoomId)['paymentTotal'];?></span></td>
							</tr>
						</table>
						
					</div>
				</div>
			</div>
			
			<?php if(getDetailByTable('utility_payment',$_GET['paymentActivity'])['utyp_file']){ ?>
			<div class="col-md-6 col-sm-6 col-xs-12">
				<div class="x_panel">
					<div class="x_content">
						<img src="uploads/rental/<?php echo getDetailByTable('utility_payment',$_GET['paymentActivity'])['utyp_file']; ?>" width="100%">
					</div>
				</div>
			</div><?php } ?>
			
			<?php if($_GET['paymentActivity']){ ?>
			
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Utility Payment</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">Month/Year 
								</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="total" type="currency" value="<?php echo date("F / Y",strtotime(getDetailByTable("utilities",getDetailByTable("utility_payment",$_GET['paymentActivity'])['uty_id'])['u_date'])); ?>" readonly>
								</div>
							</div>

							<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 getDetailByTable('utility_payment',$_GET['paymentActivity'])['utyp_date']; ?>">
								</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 getDetailByTable('utility_payment',$_GET['paymentActivity'])['utyp_amount']; ?>">
								</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="submit" class="btn btn-danger" name="remove">Remove</button>
								</div>
							</div>

						</form>
						<?php
							if(isset($_POST['submit'])){
								$postUtilityId = $_GET['paymentActivity'];
								$postDate = $_POST['date'];
								$postAmount = $_POST['amount'];

								mysqli_query($conn,"UPDATE utility_payment SET utyp_amount='$postAmount', utyp_date='$postDate' WHERE utyp_id=$postUtilityId");

								header("Location: ?pagecontent=rental_view&room=$getRoomId");
							}
											   
							if(isset($_POST['remove'])){
								$postUtilityId = $_GET['paymentActivity'];
								$fileName = getDetailByTable('utility_payment',$_GET['paymentActivity'])['utyp_file'];

								if(mysqli_query($conn,"DELETE FROM utility_payment WHERE utyp_id=$postUtilityId")){
									unlink('uploads/rental/'.$fileName);
								};

								header("Location: ?pagecontent=rental_view&room=$getRoomId");
							}
						?>
									
					</div>
                </div>
            </div>
			<?php } ?>
        </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 id="datatable" class="table table-striped table-bordered">
							<thead>
								<tr>
									<th>NO</th>
									<th>Date</th>
									<th>Amount</th>
									<th>Payment Activity</th>
									<th>Unpaid</th>
									<th>Note</th>
									<th>Action</th>
								</tr>
							</thead>
							<tbody>
								<?php
								foreach(array_reverse(getMonthsInRange($rentalStartDate,date("Y-m-31"))) as $monthYear){
									$date = $monthYear['year']."-".$monthYear['month']."-01";
							  ?>
								<tr>
									<td><?php echo ++$no;?></td>
									<td><?php echo $monthYear['month']."/".$monthYear['year'];?></td>
									<td><?php $rentalRate = utilityRecord($date,6,$getRoomId)['u_amount']; if($rentalRate){echo "RM ".$rentalRate;} ?></td>
									<td><?php 
											foreach(array_reverse(paymentRecord(utilityRecord($date,6,$getRoomId)['u_id'])) as $array2){
												echo "<a href='?pagecontent=rental_view&room=".$getRoomId."&paymentActivity=".$array2['utyp_id']."'>";
												echo "RM ";
												$amount = $array2['utyp_amount']; echo $amount; $total = $total + $amount;
												echo " - ";
												echo date_format(date_create($array2['utyp_date']),"d/m/Y");
												echo "</a>"; echo "<br>";
											};
										?></td>
									<td><?php $amountBalance = $total - $rentalRate; if($amountBalance<0){echo "<span style='color:red;font-weight:bold;'>RM ".$amountBalance."</span>";}else{echo "RM ".$amountBalance;}?></td>
									<td><?php echo utilityRecord($date,6,$getRoomId)['u_note']; ?></td>
									<td>
										
									    <a href="?pagecontent=utility_update&room=<?php echo $getRoomId; ?>&date=<?php echo $date; ?>&type=6&amount=<?php echo $getRentalRate; ?>"><button type="button" class="btn btn-xs btn-primary">Update</button></a>
										
									    <a href="?pagecontent=utility_payment&id=<?php echo utilityRecord($date,6,$getRoomId)['u_id']; ?>"><button type="button" class="btn btn-xs btn-primary">Payment</button></a>
									</td>
								</tr>
								<?php
									//clear
									$total = 0; $amountBalance = 0; } 
								?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
        </div>
    </div>
</div>

<?php
	function getMonthsInRange($startDate, $endDate){
		$months = array();
		while (strtotime($startDate) <= strtotime($endDate)) {
			$months[] = array('year' => date('Y', strtotime($startDate)), 'month' => date('m', strtotime($startDate)), );
			$startDate = date('01 M Y', strtotime($startDate.
				'+ 1 month')); // Set date to 1 so that new month is returned as the month changes.
		}

		return $months;
	}

	function paymentRecord($utilityId){
		global $conn; $array = array();
		
		$result = mysqli_query($conn,"SELECT * FROM utility_payment WHERE uty_id=$utilityId");
		while($row = mysqli_fetch_array($result)){
			$array[] = $row;
		}

		return $array;
	}

	function durationInDays($dateStart,$dateEnd){
		$datetime1 = date_create($dateStart);
		$datetime2 = date_create($dateEnd);
		$interval = date_diff($datetime1, $datetime2);
		return $interval->format('%y years %m months %d days');
	} 
?>