<?php $getItemId = $_GET['id']; ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#addForm").hide();
    $("#addBtn").click(function(){
        $("#addForm").toggle();
    });
});
</script>

<div class="right_col" role="main">
	<div class="">
		<div class="page-title">
			<div class="title_left">
				<h3>Items </h3><button id="addBtn" type="button" class="btn btn-dark">Add Item</button>
			</div>
		</div>
		<div class="clearfix"></div>
		
		<div id="addForm" class="row">
			
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Add Item</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="night">Title 
								</label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="night" class="form-control col-md-3 col-xs-12" name="title" type="text" value="<?php echo getDetailByTable('items',$getItemId)['i_title']; ?>">
								</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" class="btn btn-success" name="submit">Add</button>
								</div>
							</div>

						</form>
						<?php
							if(isset($_POST['submit'])){
								$postTitle = $_POST['title'];

								mysqli_query($conn,"INSERT INTO items (i_title) VALUES ('$postTitle')");
								
								//utilityLog(0,$getRoomId,$getUtilityTypeId,$_SESSION['admin'][1]." update payment RM".$postAmount." for ".$monthYear." on ".$postDate);

								header("Location: ?pagecontent=items");
							}
						?>
									
					</div>
                </div>
            </div>
		</div>
		
			<?php if($_GET['update']){ ?>
		<div class="row">
			
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Add Item</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="night">Title 
								</label>
								<div class="col-md-3 col-sm-3 col-xs-12">
									<input id="night" class="form-control col-md-3 col-xs-12" name="title" type="text" value="<?php echo getDetailByTable('items',$getItemId)['i_title']; ?>">
								</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" class="btn btn-success" name="update">Update</button>
								</div>
							</div>

						</form>
						<?php
							if(isset($_POST['update'])){
								$postTitle = $_POST['title'];

								mysqli_query($conn,"UPDATE items SET i_title='$postTitle'");
								
								//utilityLog(0,$getRoomId,$getUtilityTypeId,$_SESSION['admin'][1]." update payment RM".$postAmount." for ".$monthYear." on ".$postDate);

								header("Location: ?pagecontent=items");
							}
						?>
									
					</div>
                </div>
            </div>
		</div>
			<?php } ?>
		
		<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>Title</th>
									<th>Action</th>
								</tr>
							</thead>
							<tbody>
								<?php
									$result = mysqli_query($conn, "SELECT * FROM items") or die (mysql_error());
									while($row = mysqli_fetch_assoc($result))
									{
										$itemId = $row['i_id'];
								?>
								<tr>
									<td><?php echo $row['i_title'];?></td>
						
									<td>
										<a href="?pagecontent=items&update=y&id=<?php echo $row['i_id']; ?>"><button class="btn btn-xs btn-primary">Update</button></a>
										
										<a href="?pagecontent=items&remove=y&id=<?php echo $row['i_id']; ?>"><button class="btn btn-xs btn-danger">Remove</button></a>
									</td>
								</tr>
								<?php
									}
								?>
						</table>
					</div>
				</div>
			</div>
        </div>
    </div>
</div>

<?php
	if(isset($_REQUEST['remove'])){
		
		$itemId = $_GET['id'];
		
		mysqli_query($conn,"DELETE FROM items WHERE i_id=$itemId") or die (mysql_error());
		
		header('Location: index.php?pagecontent=items');  
	}
?>