<?php
if (isset($_GET['type'])) {
	$getUtilityTypeId = $_GET['type'];
}
?>

<div class="right_col" role="main">
	<div class="">
		<div class="page-title">
			<div class="title_left">
				<table>
					<tr>
						<td width="150px">
							<h3><?php echo strtoupper(getDetailByTable('utility_type', $getUtilityTypeId)['uty_title']); ?>
								Bill</h3>
						</td>
						<td><a href="index.php?pagecontent=room_new"><button type="button" class="btn btn-dark">Add
									Room</button></a></td>
					</tr>
				</table>
			</div>
		</div>
		<div class="clearfix"></div>
		<div class="row">

			<div class="col-md-6 col-sm-6 col-xs-12">
				<div class="x_panel">
					<div class="x_title">
						<h2>Report</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-striped">
							<?php $no = 0; ?>
							<thead>
								<tr>
									<th>#</th>
									<th>Due Date</th>
									<th>Total (This Month)</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<th scope="row"><?php echo ++$no; ?></th>
									<td>Room</td>
									<td></td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
		<div class="clearfix"></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 id="datatable" class="table table-striped table-bordered">
						<thead>
							<tr>
								<th>No</th>
								<th>Unit No</th>
								<th>Property</th>
								<th>Onwer</th>
								<th>Average Rate</th>
								<?php if ($_GET['type'] == 6) { ?>
									<th>Rental Rate</th>
									<th>Due Date</th>
								<?php } ?>
								<th>Status</th>
								<th>Action</th>
							</tr>
						</thead>
						<tbody>
							<?php
							$no = 0;
							$result = mysqli_query($conn, "SELECT * FROM rooms WHERE r_status=1 AND r_profit_type IN (1,4) ORDER BY p_id ASC");
							while ($row = mysqli_fetch_array($result)) {
								$rid = $row['r_id'];
								?>
								<tr>
									<td><?php echo ++$no; ?></td>
									<td><a href="?pagecontent=report_profit_room&room=<?php echo $row['r_id']; ?>"><?php echo roomNo($row['r_id'])['pty_code']; ?>
											<?php echo roomNo($row['r_id'])['r_no']; ?></a></td>
									<td><?php echo roomNo($row['r_id'])['pty_short']; ?></td>
									<td><?php echo getDetailByTable('owners', $row['o_id'])[4]; ?></a></td>
									<td><?php echo round(getAverageBill($row['r_id'], $getUtilityTypeId), 2); ?></td>
									<?php if ($_GET['type'] == 6) { ?>
										<td><?php echo $row['r_rental'] ? "RM " . $row['r_rental'] : '' ?></td>
										<td><?php echo isset($row['r_rental_date_start']) && $row['r_rental_date_start'] !== '' ?
											(date('d/m/Y', strtotime(date('d', strtotime($row['r_rental_date_start'])) . '-' . date('m-Y'))) . (strtotime(date('d-m-Y', strtotime(date('d', strtotime($row['r_rental_date_start'])) . '-' . date('m-Y')))) < time() ? ' <strong style="color: red;">due</strong>' : '')) :
											'<strong style="color: red;">NO RECORD</strong>'; ?></td>

									<?php } ?>
									<td><?php $balance = utilityTotal($row['r_id'], $getUtilityTypeId)['total'] - utilityPaidTotal($row['r_id'], $getUtilityTypeId)['total'];
									if ($balance > 0) {
										echo "<span style='color:red; font-weight: bold;'>RM  " . $balance . "</span>";
									} else {
										echo "<span style='color:blue; font-weight: bold;'>PAID</span>";
									} ?></span>
									</td>
									<td>
										<a
											href="?pagecontent=utility_view&room=<?php echo $row['r_id']; ?>&type=<?php echo $getUtilityTypeId; ?>"><button
												class="btn btn-xs btn-primary">Update</button></a>
									</td>
								</tr>
								<?php
							}
							?>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
</div>