<?php
	include("includes/dbconfig.php");
	$pagetitle = "Store";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
	
	$err= "";
	$stocklistfound = FALSE;
	
	 	#$stockitemsdata = DB::query("SELECT item_stock_level.id, stock_item.item_name, stock_item.item_code, stock_item.bar_code AS bar_code, stock_item.item_image_file, stock_item.reorder_level, stock_item.status, item_category.category_name, item_stock_level.quantity, item_stock_level.version FROM item_stock_level INNER JOIN stock_item ON item_stock_level.item = stock_item.id  LEFT JOIN item_category ON stock_item.category = item_category.id WHERE item_stock_level.shop IS NULL  AND  item_stock_level.quantity  < 6 
			#UNION SELECT item_stock_level.id, stock_item.item_name, stock_item.item_code, stock_item.bar_code AS bar_code, stock_item.item_image_file, stock_item.reorder_level, stock_item.status, item_category.category_name, item_stock_level.quantity, item_stock_level.version FROM item_stock_level 
			#INNER JOIN stock_item ON item_stock_level.item = stock_item.id LEFT JOIN item_category ON stock_item.category = item_category.id WHERE item_stock_level.shop IS NULL UNION SELECT item_stock_level.id, stock_item.item_name, stock_item.item_code, stock_item.bar_code AS bar_code, stock_item.item_image_file, stock_item.reorder_level, stock_item.status, item_category.category_name, item_stock_level.quantity, item_stock_level.version FROM item_stock_level INNER JOIN stock_item ON item_stock_level.item = stock_item.id  LEFT JOIN item_category ON stock_item.category = item_category.id WHERE item_stock_level.shop IS NULL UNION SELECT item_stock_level.id, stock_item.item_name, stock_item.item_code, stock_item.alt_barcode AS bar_code, stock_item.item_image_file, stock_item.reorder_level, stock_item.status, item_category.category_name, item_stock_level.quantity, item_stock_level.version FROM item_stock_level INNER JOIN stock_item ON item_stock_level.item = stock_item.id  LEFT JOIN item_category ON stock_item.category = item_category.id WHERE item_stock_level.shop  IS NULL  AND  item_stock_level.quantity  < 6 ORDER BY status DESC, category_name, item_name, item_code", trim($_POST['itemsearchtext']), trim($_POST['itemsearchtext']), trim($_POST['itemsearchtext']), trim($_POST['itemsearchtext']));
	
			#$stockitemsdata = DB::query("SELECT id  FROM `item_stock_level` WHERE `shop` IS NULL AND `quantity` <= 5 AND `is_active` = 1");
			$stockitemsdata = DB::query("SELECT item_name, item_code, bar_code, stock_item.status, reorder_level, quantity, default_sell_price, category_name, category_description FROM item_stock_level  INNER JOIN stock_item ON item_stock_level.item = stock_item.id LEFT JOIN item_category ON stock_item.category = item_category.id  WHERE item_stock_level.shop IS NULL AND item_stock_level.quantity <= 5 AND item_stock_level.is_active = 1 ORDER BY item_name ");
	 
			if(DB::count() == 0)
			{
				$err=$err."No Item Found!<br>";
			}
			else
			{
				$stocklistfound = TRUE;
			}
		
	
	#$stockitemsdata = DB::query("SELECT item_name, item_code, bar_code, reorder_level, default_sell_price, category_name, category_description FROM item_stock_level  INNER JOIN stock_item ON item_stock_level.item = stock_item.id LEFT JOIN item_category ON stock_item.category = item_category.id  WHERE item_stock_level.shop IS NULL AND item_stock_level.quantity <= 5 AND item_stock_level.is_active = 1 ORDER BY item_name ");
	 
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h1>Manage Store</h1>
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<!-- Default card -->
					<div class="card card-primary">
						<?php
							if($_SESSION["connectedusergroup"] == 1 || $_SESSION["connectedusergroup"] == 2)
							{
						?>
						 
							<div id="servererrormessage">
							</div>
							<?php
								
								if($err!="")
								{
							?>
							<div class="alert alert-danger">
								<i class="fa fa-ban-circle"></i><br>
								<?php 
									echo $err; 
								?>
							</div>
							<?php
								}
							?>
							 
							
							<?php
								if($stocklistfound)
								{
							?>
							<h3 class="card-title text-center">Stock Item Level</h3>
							<div class='card-body'>
							
							 
								 
									<table id="stockitemstable" class="table table-striped table-bordered">
										<thead>
											<tr>
											 
												<th>Name</th>
												<th>Category</th>
												<th>Code</th>
												<th>Barcode</th>
												<th>Reorder Level</th>
												<th>Quantity At Hand</th>
												<th class="text-center">Status</th>
											</tr>
										</thead>
										<tfoot>
											<tr>
												 
												<th>Name</th>
												<th>Category</th>
												<th>Code</th>
												<th>Barcode</th>
												<th>Reorder Level</th>
												<th>Quantity At Hand</th>
												<th class="text-center">Status</th>
											</tr>
										</tfoot>
										<tbody>
											<?php
												$status = "";
												
												foreach($stockitemsdata as $row)
												{
													if($row['status'] == 1)
													{
														$status = '<label class="label label-success" >Active</label>';
													}
													else
													{
														$status = '<label class="label label-danger" >Inactive</label>';
													}
											?>
											<tr>
												 
												<td style="vertical-align:middle;"><?php echo $row['item_name']; ?></td>
												<td style="vertical-align:middle;"><?php echo $row['category_name']; ?></td>
												<td style="vertical-align:middle;"><?php echo $row['item_code']; ?></td>
												<td style="vertical-align:middle;"><?php echo $row['bar_code']; ?></td>
												<td style="vertical-align:middle;"><?php echo number_format($row['reorder_level'], 2); ?></td>
												<td style="vertical-align:middle;"><?php echo number_format($row['quantity'], 2); ?></td>
												<td style="vertical-align:middle;" class="text-center"><?php echo $status; ?></td>
											</tr>
											<?php
												}
											?>
										</tbody>
									</table>
									<div>
								 
							 
							<?php
								}
							?>
						</div><!-- /.card-body -->
						 
						<?php
							}
							else
							{
						?>
						<div class="alert alert-danger alert-dismissable">
							<i class="fa fa-ban-circle"></i><br>
							Limited Access
						</div>
						<?php
							}
						?>
					</div><!-- /.card -->
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->

			 
		</div><!-- ./wrapper -->
		
		<div class="modal" id="waitdialogmodal"  data-backdrop="static">
            <div class="modal-dialog modal-sm">
              <div class="modal-content">
                  <div class="modal-body">
					<div class="text-center">
						<img src="<?php echo $base_url . $ajax_loading_image; ?>" />
					</div>
				  </div>
              </div>
            </div>
        </div>
		
		<div class="modal" id="mediummodal">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-body">					
						<div class="row" id="mediummodaldetails">
					
						</div>
					</div>
				</div>
			</div>
		</div>
		
		<div class="modal" id="largemodal">
			<div class="modal-dialog modal-lg">
				<div class="modal-content">
					<div class="modal-body">					
						<div class="row" id="largemodaldetails">
							
						</div>
					</div>
				</div>
			</div>
		</div>

		<?php include ('includes/general_footer.php')?> 
		<script src="dist/js/app.min.js" type="text/javascript"></script>
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="plugins/bert/store.js" type="text/javascript"></script>
	</body>
</html>