<?php
	//find user id
	$getId = $_GET['room']; //to be deleted later
	$getRoomId = $_GET['room'];
	$getRoomReportId = $_GET['report'];
?>
<div class="right_col" role="main">
    <div class="">
        <div class="row">
			
            <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Add Details</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="roomno">Date</label>
							  <div class="col-md-8 col-sm-8 col-xs-12">
								  <?php $date = date("Y-m-d"); if($_GET['date']){ $date = $_GET['date'];} ?>
									<input id="roomno" class="form-control col-12" name="date" type="date" value="<?php echo $date;?>">
							  </div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Title</label>
							  <div class="col-md-8 col-sm-8 col-xs-12">
									<input id="roomno" class="form-control col-12" name="title" type="text">
							  </div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Description</span>
								</label>
								<div class="col-md-8 col-sm-8 col-xs-12">
									<textarea id="message" class="form-control" cols="100%" rows="5" name="description"></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" name="submit" class="btn btn-success">Submit</button>
									<button type="submit" class="btn btn-primary" onclick="javascript:window.location='index.php?pagecontent=dashboard'">Cancel</button>
								</div>
							</div>
						</form>
					</div>
                </div>
            </div>
			
			<?php if(isset($_GET['update'])){ ?>
            <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Update : <?php echo getDetailFromTable('room_report',$_GET['update'])['rr_title']; ?></h2>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">

						<form class="form-horizontal form-label-left" method="post">
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Status</label>
								<div class="col-md-8 col-sm-8 col-xs-12">
								  <select class="form-control" name="status">
									  
									<option value="0" <?php echo select(getDetailFromTable('room_report',$getRoomReportId)['rr_status'],0); ?>>NEW REPORT</option>
									<option value="1" <?php echo select(getDetailFromTable('room_report',$getRoomReportId)['rr_status'],1); ?>>REVIEWED</option>
									<option value="2" <?php echo select(getDetailFromTable('room_report',$getRoomReportId)['rr_status'],2); ?>>IN PROCESS</option>
									<option value="3" <?php echo select(getDetailFromTable('room_report',$getRoomReportId)['rr_status'],3); ?>>COMPLETED</option>
									  
								  </select>
								</div>
							  </div>
							
							<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12">Staff</label>
								<div class="col-md-8 col-sm-8 col-xs-12">
								  <select class="form-control" name="staff">
									  <?php
									  	$result = mysqli_query($conn, "SELECT * FROM staffs");
									  	while($row = mysqli_fetch_assoc($result)){
									  ?>
									<option value="<?php echo $row['s_id']; ?>" <?php echo select(getDetailFromTable('room_report',$getRoomReportId)['s_id'],$row['s_id']); ?>><?php echo $row['s_nickname']; ?></option>
									  <?php } ?>
								  </select>
								</div>
							  </div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Date</label>
							  <div class="col-md-8 col-sm-8 col-xs-12">
								  <?php $date = date("Y-m-d"); if($_GET['date']){ $date = $_GET['date'];} ?>
									<input id="roomno" class="form-control col-12" name="date" type="date" value="<?php echo getDetailFromTable('room_report',$getRoomReportId)['rr_date']; ?>">
							  </div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Title</label>
							  <div class="col-md-8 col-sm-8 col-xs-12">
									<input id="roomno" class="form-control col-12" name="title" type="text" value="<?php echo getDetailFromTable('room_report',$getRoomReportId)['rr_title']; ?>">
							  </div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Description</span>
								</label>
								<div class="col-md-8 col-sm-8 col-xs-12">
									<textarea id="message" class="form-control" cols="100%" rows="5" name="description"><?php echo urldecode(getDetailFromTable('room_report',$getRoomReportId)['rr_description']); ?></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" name="update" class="btn btn-success">Update</button>
								</div>
							</div>
						</form>
					</div>
                </div>
            </div>
			<?php } ?>

	  <div class="col-md-6 col-sm-6 col-xs-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Info</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">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Description</th>
				  <th>Amount</th>
				</tr>
			  </thead>
			  <tbody>
				<tr>
				  <th scope="row">1</th>
				  <td>Total spent on inventories</td>
				  <td>RM <?php echo totalInventoryAmount($getId); ?></td>
				</tr>
			  </tbody>
			</table>

		  </div>
		</div>
	  </div>
			
              <div class="clearfix"></div>

              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Report list</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">

                    <div class="table-responsive">
                      <table class="table table-striped jambo_table bulk_action">
                        <thead>
                          <tr class="headings">
                            <th class="column-title">Bil </th>
                            <th class="column-title">Title </th>
                            <th class="column-title">Date </th>
                            <th class="column-title">Status</th>
                            <th class="column-title">Staff</th>
                            <th class="column-title no-link last"><span class="nobr">Action</span>
                            </th>
                            <th class="bulk-actions" colspan="7">
                              <a class="antoo" style="color:#fff; font-weight:500;">Bulk Actions ( <span class="action-cnt"> </span> ) <i class="fa fa-chevron-down"></i></a>
                            </th>
                          </tr>
                        </thead>

                        <tbody>
							<?php
								$result=mysqli_query($conn, "SELECT * FROM room_report WHERE r_id=$getRoomId ORDER BY rr_id DESC");
								while($row = mysqli_fetch_array($result)){
							?>
                          <tr class="even pointer">
							  
                            <td class=" "><?php echo ++$no; ?></td>
                            <td class=" "><?php echo $row['rr_title']; ?></td>
                            <td class=" "><?php echo $row['rr_date']; ?></td>
                            <td class=" "><a href="?pagecontent=room_report&room=<?php echo $getRoomId; ?>&report=<?php echo $row['rr_id']; ?>"><?php echo status($row['rr_status']); ?></a></td>
                            <td class=" "><?php echo getDetailFromTable('staffs',$row['s_id'])['s_nickname']; ?></td>
                            <td class=" last">
								<a href="?pagecontent=room_report&room=<?php echo $row['r_id']; ?>&update=<?php echo $row['rr_id']; ?>"><button class="btn btn-xs btn-primary">Update</button></a>
								<a href="?pagecontent=room_report&remove=<?php echo $row['rr_id']; ?>" onclick="return confirm('Are you sure you want to delete this item?');"><button class="btn btn-xs btn-danger">Delete</button></a>
                            </td>
                          </tr>
							<?php } ?>
                        </tbody>
                      </table>
                    </div>
							
						
                  </div>
                </div>
              </div>
        </div>
    </div>
</div>

<?php
	function status($input){
		switch($input){
			case '0': return "NEW REPORT"; break;
			case '1': return "REVIEW"; break;
			case '2': return "IN PROCESS"; break;
			case '3': return "COMPLETED"; break;
		}
	}

	if(isset($_REQUEST['remove'])){
		
		$id = $_REQUEST['remove'];
		$roomId = getDetailFromTable('room_report',$id)['r_id'];
		
		mysqli_query($conn,"DELETE FROM room_report WHERE rr_id=$id");
		
		header('Location: index.php?pagecontent=room_report&room='.$roomId);  
	}

	if(isset($_POST['submit']))
	{
		$postDate = $_POST['date'];
		$postTitle = $_POST['title'];
		$postDescription = urlencode($_POST['description']);
		
		$sessionAdminId = $_SESSION['admin'][0];
		
		mysqli_query($conn, "INSERT INTO room_report (r_id, rr_date, rr_title, rr_description, a_id)
		VALUES ($getRoomId, '$postDate', '$postTitle', '$postDescription', $sessionAdminId)");
		
		header('Location: ?pagecontent=room_report&room='.$getRoomId);
	}

	if(isset($_POST['update']))
	{
		$postStatus = $_POST['status'];
		$postStaff = $_POST['staff'];
		$postDate = $_POST['date'];
		$postTitle = $_POST['title'];
		$postDescription = urlencode($_POST['description']);
		
		//$sessionAdminId = $_SESSION['admin'][0];
		
		mysqli_query($conn, "UPDATE room_report SET rr_status=$postStatus, s_id=$postStaff, rr_date='$postDate', rr_title='$postTitle', rr_description='$postDescription' WHERE rr_id=$getRoomReportId");
		
		header('Location: ?pagecontent=room_report&room='.$getRoomId.'&report='.$getRoomReportId);
	}
?>