<div class="right_col" role="main">
	<div class="">
		<div class="page-title">
			<div class="title_left">
				<table>
					<tr>
						<td width="150px"><h3>Room list</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-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>Unit No</th>
									<th>Code</th>
									<th>Onwer</th>
									<th>Property</th>
									<th>Profit Type</th>
									<th>Rental</th>
									<th>Status</th>
									<th>Action</th>
								</tr>
							</thead>
							<tbody>
								<?php
									$result=mysqli_query($conn, "SELECT * FROM rooms ORDER BY r_status ASC") or die (mysql_error());
									while($row = mysqli_fetch_array($result))
									{
                                        // if(filterRoomAdmin($row['r_id'],$_SESSION['admin'][0])==true){continue;};
                                        
										$rid = $row['r_id'];
								?>
								<tr>
									<td><a href="?pagecontent=report_profit_room&room=<?php echo $row['r_id']; ?>"><?php echo $row['r_no'];?></a></td>
									<td><a href="?pagecontent=report_profit_room&room=<?php echo $row['r_id']; ?>"><?php echo $row['r_code'];?></a></td>
									<td><?php echo $row['o_id'] ? getDetailByTable('owners',$row['o_id'])['o_name'] : ""; ?></a></td>
									<td><?php echo getDetailByTable('properties',$row['p_id'])['pty_title']; ?></td>
									<td><?php 
										switch($row['r_profit_type']){
											case 1: echo "DAILY PROFIT"; break;
											case 2: echo "HOUSEKEEPING"; break;
											case 3: echo "PROFIT SHARING"; break;
											case 4: echo "TENANT"; break;
										}
										?></td>
									<td><?php 
										if($row['r_rental']!="0.00"){ echo "RR RM ".$row['r_rental'];}
										if($row['r_housekeep_fee']!="0.00"){echo "HK RM ".$row['r_housekeep_fee'];}
										if($row['r_profit_sharing']!="0.00"){echo " PS (".$row['r_profit_sharing']*100 ."%)";}
										?></td>
									<td>
										<?php if($row['r_status']==1){
											echo "<a href='?pagecontent=rooms&activate=$rid'><button class='btn btn-primary btn-xs'>Activate</button></a>";
										}else{
											echo "<a href='?pagecontent=rooms&deactivate=$rid'><button class='btn btn-danger btn-xs'>Deactivate</button></a>";
										} ?>
									</td>
						
									<td>
										<a href="?pagecontent=room_report&room=<?php echo $row['r_id']; ?>"><button class="btn btn-xs btn-danger">Report</button></a>
										
										<a href="?pagecontent=room_inventory&room=<?php echo $row['r_id']; ?>"><button class="btn btn-xs btn-success">Inventories</button></a>
										
										<a href="?pagecontent=expenses&room=<?php echo $row['r_id']; ?>"><button class="btn btn-xs btn-success">Expenses</button></a>
										
										<a href="?pagecontent=room_update&room=<?php echo $row['r_id']; ?>"><button class="btn btn-xs btn-primary">Update</button></a>
										
										<a href="?pagecontent=room_item&room=<?php echo $row['r_id']; ?>"><button class="btn btn-xs btn-warning">Items</button></a>
										
										<a href="?pagecontent=room_cards&room=<?php echo $row['r_id']; ?>"><button class="btn btn-xs btn-warning">A. Card</button></a>
										
										<a href="?pagecontent=rooms&remove=<?php echo $row['r_id']; ?>"><button class="btn btn-xs btn-danger" disabled>Remove</button></a>
									</td>
								</tr>
								<?php
									}
								?>
						</table>
					</div>
				</div>
			</div>
        </div>
    </div>
</div>

<?php
	if(isset($_REQUEST['remove'])){
		
		$roomId = $_REQUEST['remove'];
		
		mysqli_query($conn,"DELETE FROM rooms WHERE r_id=$roomId");
		
		header('Location: index.php?pagecontent=rooms');  
	}

	if(isset($_REQUEST['activate'])){
		
		$roomId = $_REQUEST['activate'];
		
		mysqli_query($conn,"UPDATE rooms SET r_status=0 WHERE r_id=$roomId");
		
		header('Location: index.php?pagecontent=rooms');  
	}

	if(isset($_REQUEST['deactivate'])){
		
		$roomId = $_REQUEST['deactivate'];
		
		mysqli_query($conn,"UPDATE rooms SET r_status=1 WHERE r_id=$roomId");
		
		header('Location: index.php?pagecontent=rooms');  
	}
?>