<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>
					</tr>
				</table>
			</div>
		</div>

        <?php if(isset($_GET['update'])){ ?>
		<div class="clearfix"></div>
        <div class="row">
            <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2><?php echo $_GET['update'] ? 'Update ' : 'Add '; ?>Details</h2>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">

						<form class="form-horizontal form-label-left" method="post">
							<input type="hidden" name="id" value="<?php echo $_GET['update'] ? $_GET['update'] : ''; ?>">

							  <div class="form-group">
								<label class="control-label col-md-2 col-sm-2 col-xs-12">Type</label>
								<div class="col-md-6 col-sm-6 col-xs-12">
								  <select class="form-control" name="type">
									  <?php
									  	foreach(maintenanceType() AS $data){
									  ?>
									<option value="<?php echo $data['id']; ?>" <?php select($data['id'],getDetailByTable('room_expenses',$_GET['update'] ? $_GET['update'] : 0)['re_type']); ?>><?php echo strtoupper($data['title']); ?></option>
									  <?php } ?>
								  </select>
								</div>
								  <?php $redirectUrl = urlencode("?pagecontent=room_inventory&room=".$getId); ?>
								  <?php if(isset($_SESSION['staff'])){}else{ ?>
								 <a href="?pagecontent=expenses_type"><button type="button" class="btn btn-primary">Add Type</button></a><?php } ?>
							  </div>
							
							<div class="item form-group">
								<label class="control-label col-md-2 col-sm-2 col-xs-12" for="roomno">Date</label>
							  <div class="col-md-6 col-sm-6 col-xs-12">
								  <?php $date = date("Y-m-d"); if($_GET['date']){ $date = $_GET['date'];} ?>
									<input id="roomno" class="form-control col-md-7 col-xs-12" name="date" type="date" value="<?php echo $_GET['claim'] ? urldecode(getDetailByTable('room_expenses',$_GET['claim'])['re_date']) : ($_GET['update'] ? $date : $date); ?>">
							  </div>
							</div>
						
							<div class="item form-group">
								<label class="control-label col-md-2 col-sm-2 col-xs-12" for="roomno">Title</label>
							  <div class="col-md-6 col-sm-6 col-xs-12">
								  
									<input id="roomno" class="form-control col-md-7 col-xs-12" name="title" type="text" value="<?php echo $_GET['claim'] ? urldecode(getDetailByTable('room_expenses',$_GET['claim'])['re_title']) : ($_GET['update'] ? '' : ''); ?>">
							  </div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-2 col-sm-2 col-xs-12" for="note">Note</span>
								</label>
								<div class="col-md-9 col-sm-9 col-xs-12">
									<textarea id="message" class="form-control" name="note"><?php echo $_GET['claim'] ? urldecode(getDetailByTable('room_expenses',$_GET['claim'])['re_note']) : ($_GET['update'] ? '' : ''); ?></textarea>
								</div>
							</div>
							
							<div class="ln_solid"></div>
							<div class="form-group">
								<div class="col-md-6 col-md-offset-2">
									<button id="send" type="submit" name="<?php echo $_GET['update'] ? 'update' : 'submit'; ?>" class="btn btn-success"><?php if(isset($_GET['update'])){echo "Update";}else{echo "Submit";} ?></button>
									<button type="submit" class="btn btn-primary" onclick="javascript:window.location='index.php?pagecontent=dashboard'">Cancel</button>
								</div>
							</div>
						</form>

                        <?php
                            if(isset($_POST['update']))
                            {
                                $roomId = $_POST['id'];
                                $postType = $_POST['type'];
                                $postDate = $_POST['date'];
                                $postTitle = urlencode($_POST['title']);
                                $postNote = urlencode($_POST['note']);
                                
                                $adminId = $_SESSION['admin'][0] ?: 0;
                                $adminName = $_SESSION['admin'][1];
                                
                                $staffId = $_SESSION['staff']['s_id'] ?: 0;
                                
                                $adminStaff = $adminName ?: $_SESSION['staff']['s_nickname'];
                                
                                mysqli_query($conn, "INSERT INTO room_maintenance (r_id,rm_type,rm_date,a_id)
                                VALUES ($roomId,$postType,'$postDate',$adminId)");
                                
                                // expensesLog($getRoomId,0,$adminStaff." add expenses record : ".$postTitle);
                                
                                header('Location: ?pagecontent=maintenance&update='.$roomId);
                            }
                        ?>

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

		<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>Property</th>
									<th>Aircond</th>
									<th>Action</th>
								</tr>
							</thead>
							<tbody>
								<?php
									$result = mysqli_query($conn, "SELECT *, R.r_id AS room_id, RM.rm_date AS maintenance_date FROM rooms AS R LEFT JOIN room_maintenance AS RM ON R.r_id=RM.r_id WHERE R.r_status=1 ORDER BY R.r_status ASC");
									while($row = mysqli_fetch_array($result))
									{
                                        // if(filterRoomAdmin($row['r_id'],$_SESSION['admin'][0])==true){continue;};
								?>
								<tr>
									<td><?php echo getDetailByTable('properties',$row['p_id'])['pty_title']; ?></td>
									<td><a href="?pagecontent=maintenance&update=<?php echo $row['r_id']; ?>"><?php echo roomNo($row['room_id'])['pty_code'];?> <?php echo roomNo($row['room_id'])['r_no'];?></a></td>
									<td><?php echo $ow['maintenance_date']; ?></td>
									<td>
										<a href="?pagecontent=maintenance&update=<?php echo $row['room_id']; ?>"><button class="btn btn-xs btn-primary">Update</button></a>
									</td>
								</tr>
								<?php
									}
								?>
						</table>
					</div>
				</div>
			</div>
        </div>
    </div>
</div>

<?php
	
?>