<?php
	//find user id
	$getRoomId = $_GET['room'];
?>
<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>Inventory 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-4 col-sm-3 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 $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-7 col-sm-7 col-xs-12">
									<input id="roomno" class="form-control col-xs-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="roomno">Price</label>
							  <div class="col-md-7 col-sm-7 col-xs-12">
									<input id="roomno" class="form-control col-md-7 col-xs-12" name="price" type="currency">
							  </div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Photo Link</span>
								</label>
								<div class="col-md-7 col-sm-7 col-xs-12">
									<textarea id="message" class="form-control" name="photoLink"></textarea>
								</div>
							</div>
						
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Note</span>
								</label>
								<div class="col-md-7 col-sm-7 col-xs-12">
									<textarea id="message" class="form-control" name="note"></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($_GET['update']){ ?>
            <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Update 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-4 col-sm-3 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 getDetailByTable('room_inventory',$_GET['update'])['ri_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-9 col-sm-9 col-xs-12">
									<input id="roomno" class="form-control col-md-12 col-xs-12" name="title" type="text" value="<?php echo getDetailByTable('room_inventory',$_GET['update'])['ri_title']; ?>">
							  </div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Price</label>
							  <div class="col-md-9 col-sm-9 col-xs-12">
									<input id="roomno" class="form-control col-md-7 col-xs-12" name="price" type="currency" value="<?php echo getDetailByTable('room_inventory',$_GET['update'])['ri_price']; ?>">
							  </div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="roomno">Quantity</label>
							  <div class="col-md-9 col-sm-9 col-xs-12">
									<input id="roomno" class="form-control col-md-7 col-xs-12" name="qty" type="number" value="<?php echo getDetailByTable('room_inventory',$_GET['update'])['ri_qty']; ?>">
							  </div>
							</div>
							
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Photo Link</span>
								</label>
								<div class="col-md-9 col-sm-9 col-xs-12">
									<textarea id="message" class="form-control" name="photoLink"><?php echo urldecode(getDetailByTable('room_inventory',$_GET['update'])['ri_photo_link']); ?></textarea>
								</div>
							</div>
						
							<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Note</span>
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<textarea id="message" class="form-control" name="note"><?php echo urldecode(getDetailByTable('room_inventory',$_GET['update'])['ri_note']); ?></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="updateBtn" class="btn btn-success">Update</button>
								</div>
							</div>
						</form>
						<?php
							if(isset($_POST['updateBtn']))
							{
								$roomId = $getRoomId;
								$inventoryId = $_GET['update'];
								$date = $_POST['date'];
								$title = $_POST['title'];
								$price = $_POST['price'];
								$qty = $_POST['qty'];
								$photoLink = urlencode($_POST['photoLink']);
								$note = urlencode($_POST['note']);
								
								$adminId = $_SESSION['admin'][0];
								
								mysqli_query($conn, "UPDATE room_inventory SET ri_title='$title', ri_price='$price', ri_date='$date', ri_qty=$qty, ri_photo_link='$photoLink', ri_note='$note', a_id=$adminId
								WHERE ri_id=$inventoryId") or die (mysql_error());
								
								header('Location: ?pagecontent=room_inventory&room='.$getRoomId);
							}
						?>
					</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($getRoomId); ?></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>Inventory 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">Inventory </th>
                            <th class="column-title">Title </th>
                            <th class="column-title">Date </th>
                            <th class="column-title">Qty </th>
                            <th class="column-title">Price</th>
                            <th class="column-title">Unit</th>
                            <th class="column-title">Amount </th>
                            <th class="column-title">Status </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_inventory WHERE r_id=$getRoomId ORDER BY ri_id DESC") or die (mysql_error());
								while($row = mysqli_fetch_array($result)){
							?>
                          <tr class="even pointer">
							  
                            <td class=" "><a href="?pagecontent=room_inventory&room=<?php echo $row['r_id']; ?>&update=<?php echo $row['ri_id']; ?>"><?php echo getDetailFromTable('inventories',$row['iv_id'])[1]; ?></a></td>
                            <td class=" "><a href="?pagecontent=room_inventory&room=<?php echo $row['r_id']; ?>&update=<?php echo $row['ri_id']; ?>"><?php echo $row['ri_title']; ?></a></td>
                            <td class=" "><?php echo $row['ri_date']; ?></td>
                            <td class=" "><?php $qty = $row['ri_qty']; echo $qty; ?></td>
                            <td class=" ">RM <?php echo getDetailFromTable('room_inventory',$row['ri_id'])['ri_price']; ?></td>
                            <td class=" ">RM <?php $unitPrice = getDetailFromTable('inventories',$row['iv_id'])[4]; echo $unitPrice; ?></td>
                            <td class=" ">RM <?php $amount = $unitPrice*$qty; echo $amount; ?></td>
                            <td class="a-right a-right ">-</td>
                            <td class=" last"><a href="?pagecontent=room_inventory&room=<?php echo $getRoomId; ?>&remove=<?php echo $row['ri_id']; ?>">Delete</a>
                            </td>
                          </tr>
							<?php } ?>
                        </tbody>
                      </table>
                    </div>
							
						
                  </div>
                </div>
              </div>
        </div>
    </div>
</div>

<?php
	if(isset($_REQUEST['remove'])){
		
		$id = $_REQUEST['remove'];
		
		mysqli_query($conn,"DELETE FROM room_inventory WHERE ri_id=$id") or die (mysql_error());
		
		header('Location: ?pagecontent=room_inventory&room='.$getRoomId.'&update='.$_GET['update']);  
	}

	if(isset($_POST['submit']))
	{
		$roomId = $getRoomId;
		$date = $_POST['date'];
		$title = $_POST['title'];
		$price = $_POST['price'];
		$photoLink = urlencode($_POST['photoLink']);
		$note = urlencode($_POST['note']);
		
		$adminId = $_SESSION['admin'][0];
		
		mysqli_query($conn, "INSERT INTO room_inventory (r_id, ri_title, ri_date, ri_price, ri_photo_link, ri_note, a_id)
		VALUES ($roomId, '$title', '$date', $price, '$photoLink', '$note', $adminId)") or die (mysql_error());
		
		header('Location: ?pagecontent=room_inventory&room='.$getRoomId);
	}
?>