<?php 
include 'dheader.php'; 


if(isset($_GET['delete'])){
	$listItems = $db->query("DELETE FROM itemsetup WHERE id = '".$_GET['delete']."'");
}

?>
	<div class="modal fade hidden-print" id="myModal" tabindex="-1" role="dialog" aria-hidden="true"></div>
	<div class="wrapper ">
		<?php include 'leftbar.php'; ?>
		<!-- left-bar -->
		<?php include 'topbar.php'; ?>
			<!-- top-bar -->
			<div class="main-content">


<div class="manage_buttons">
	<div class="row">
		<div class="col-md-8">
			<form action="" id="search_form" autocomplete="off" class="" method="get" accept-charset="utf-8">
				<div class="search search-items no-left-border">
					<ul class="list-inline">
						<li>
							<span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span><input type="text" list="listItems" class="form-control ui-autocomplete-input" name="item" id="search" value="" placeholder="Search Items" autocomplete="off">
							<datalist id="listItems">
							<?php
								$listItems = $db->query("SELECT * FROM itemsetup");
								while($listrow = $listItems->fetchArray()){
							?>
								<option value="<?php echo $listrow['item_name']; ?>"><?php echo $listrow['item_name']; ?></option>
							<?php } ?>	
						</datalist>
						</li>
						
						<li>
						Category:
						<select name="category" class="form-control" id="fields">
						<option value="">All</option>
						<?php
							$categories_all = $db->query("SELECT * FROM categories");
							while($fetch_category = $categories_all->fetchArray()){
						?>	
							<option value="<?php echo $fetch_category['id']; ?>"><?php echo $fetch_category['name']; ?></option>
						<?php } ?>	
							</select>
						</li>
						<li><input type="submit" name="submitf" value="Submit" class="btn btn-primary btn-lg">
						</li>
					</ul>
				</div>
			</form>
		</div>


		<div class="col-md-4">
			<div class="buttons-list items-buttons">
				<div class="pull-right-btn">
						<a href="newitems.php" class="btn btn-primary btn-lg" title="New Item"><span class="" <?php echo (@$_SESSION['itemsadd'] == '') ? 'style="display:none;"': ''; ?>>New Item</span></a>
				</div>
			</div>
		</div>
	</div>
</div>

	<div class="container-fluid">
		<div class="row manage-table">
			<div class="panel panel-piluku">
				<div class="panel-heading">
				<h3 class="panel-title">
				<?php
					$countItems = $db->query("SELECT count(*) AS totalItems FROM itemsetup");
					$count = $countItems->fetchArray();
				?>
					List Of Items				<span title="<?php echo $count['totalItems']; ?> total Items" class="badge bg-primary tip-left"><?php echo $count['totalItems']; ?></span>
					<span class="panel-options custom">
											</span>
				</h3>
			</div>
				<div class="panel-body nopadding table_holder table-responsive" >
					<table class="tablesorter table  table-hover" id="sortable_table">
						<thead>
							<tr>
								<th>Item Id</th>
								<th>UPC/EAN/ISBN</th>
								<th>Name</th>
								<th>Unit</th>
								<th>Category</th>
								<th>Cost Price</th>
								<th>Selling Price</th>
								<th>Quantity</th>
								<th>Total Cost</th>
								<th>Edit</th>
							</tr>
						</thead>
						<tbody>
						<?php
							if(!isset($_GET['item']) || $_GET['item'] == "" && $_GET['category'] == ""){
                        		$items  = "SELECT * FROM itemsetup ";
							}else{
								$items  = "SELECT * FROM itemsetup WHERE ";
								if($_GET['item'] != "" && $_GET['category'] != ""){
									$items  .= "item_name = '".$_GET['item']."' AND category = '".$_GET['category']."' ";
								}
								elseif($_GET['item'] != ""){
									$items  .= "item_name = '".$_GET['item']."' ";
								}
								elseif($_GET['category'] != ""){
									$items  .= "category = '".$_GET['category']."' ";
								}
							}
							$items = $db->query($items);
							while($row = $items->fetchArray()){
						?>
							<tr>
								<td><?php echo $row['item_code']; ?></td>
								<td><?php echo $row['uei']; ?></td>
								<td><a href="#" class="underline"><?php echo $row['item_name']; ?></a></td>
							<?php
							$categories = $db->query("SELECT * FROM categories WHERE id = '".$row['category']."'");
							$row2 = $categories->fetchArray();

							$units = $db->query("SELECT * FROM units WHERE id = '".$row['unit']."'");
							$row3 = $units->fetchArray();
							?>	
								<td><?php echo $row3['unit']; ?></td>
								<td><?php echo $row2['name']; ?></td>
								<td><?php echo round($row['purchase_price'],2); ?></td>
								<td><?php echo round($row['selling_price'],2); ?></td>
								<td><?php echo $row['stock']; ?></td>
								<td><?php echo round($row['purchase_price'] * $row['stock'], 2); ?></td>
								<td><a href="edititems.php?edit=<?php echo $row['id']; ?>" class="update-item" title="Update" <?php echo (@$_SESSION['itemsedit'] == '') ? 'style="display:none;"': ''; ?>>Edit</a></td>
								<td><a href="items.php?delete=<?php echo $row['id']; ?>" class="delete-item" title="Delete" <?php echo (@$_SESSION['itemsedit'] == '') ? 'style="display:none;"': ''; ?>>Delete</a></td>
							</tr>
						<?php } ?>		
							</tbody>
						</table>			
				</div>	
		</div>	
				</div>
	</div>
</div>

			
<?php include 'dfooter.php'; ?>