<?php
	session_start();
	include("library/conn.php");
	
	$role_rs = array();
	$get_role_mode = mysqli_query($conn, "SELECT * FROM user_role_privileges WHERE userID = '". $_SESSION['userIdd'] ."' AND roleName = 'stock_items'") or die (mysqli_error($conn));
	if (mysqli_num_rows($get_role_mode) > 0) {
		$role_rs = mysqli_fetch_array($get_role_mode);
	} else {
		$role_rs = ['-1', '-1', '-1', '-1', '-1', '-1'];
	}
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<?php include("library/head.php");?>
		<title>Manage Items | <?php echo $_SESSION['systemName']; ?></title>
	</head>
	<body class="app sidebar-mini">

		<!-- Navbar-->
		<?php include("library/header.php");?>
		<!-- Sidebar menu-->
		<div class="app-sidebar__overlay" data-toggle="sidebar"></div>
		<?php include("library/sidebar.php");?>
		
		<main class="app-content">

			<?php include("models/admin_config.php");?>
			<?php include("models/system_config.php");?>
      
			<div class="row">
				<div class="col-md-12">
					<div class="tile">
						<div class="tile-body">
                            <div class="row">
								<div class="form-group col-md-12">
									<h5 class="pb-2">
										<span><i class="fa fa-list-ul"></i> Items List</span>
										<span>
											<?php
												if ($_SESSION['userTypeID'] == 'PS0001') {
													?><button type="button" class="btn btn-success pull-right btnRegisterNewItem" data-toggle="modal" data-target="#registerStockItemsModal"><i class="fa fa-plus"></i> Register New Item</button><?php
												} else {
													if ($role_rs[3] == '1') { ?>
														?><button type="button" class="btn btn-success pull-right btnRegisterNewItem" data-toggle="modal" data-target="#registerStockItemsModal"><i class="fa fa-plus"></i>Register New Item</button><?php
													}
												}
											?>
										</span>
									</h5>
									<hr />
								</div>
							</div>
							<div class="table-responsive">
								<table class="table table-bordered table-hover table-sm" id="stockItemsTable">
                                    <thead>
                                        <tr class="text-light bg-primary text-center">
											<th class="align-middle" width="5%">No</th>
											<th class="align-middle" width="8%">Code No</th>
											<th class="align-middle" width="20%">Item Name</th>
                                            <th class="align-middle" width="12%">Category</th>
                                            <th class="align-middle" width="10%">Unit</th>
                                            <th class="align-middle" width="30%">Remarks</th>
                                            <th class="align-middle" width="15%">Register Date</th>
                                            <?php if ($role_rs[4] == 0 && $role_rs[5] == 0) { } else {?><th class="align-middle text-center" width="8%">Action</th><?php } ?>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
											$count_stock_items = 1;
                                            $getStockItems = mysqli_query($conn, "SELECT stock_items.itemID, stock_items.itemName, stock_categories.categoryName, item_units.unitName, stock_items.itemRemarks, stock_items.registerDate
											FROM stock_items
											LEFT JOIN stock_categories ON stock_items.categoryID = stock_categories.categoryID
											LEFT JOIN item_units ON stock_items.unitID = item_units.unitID
											ORDER BY stock_items.itemID ASC") or die(mysqli_error($conn));
                                            while ($stock_items_rs = mysqli_fetch_array($getStockItems)) {
                                                ?><tr>
													<td class="align-middle text-center"><?php echo $count_stock_items; ?></td>
													<td class="align-middle text-center"><?php echo $stock_items_rs[0]; ?></td>
													<td class="align-middle text-left"><?php echo $stock_items_rs[1]; ?></td>
													<td class="align-middle text-left"><?php echo ($stock_items_rs[2] == "") ? "General" : $stock_items_rs[2]; ?></td>
													<td class="align-middle text-center"><?php echo $stock_items_rs[3]; ?></td>
													<td class="align-middle text-left"><?php echo $stock_items_rs[4]; ?></td>
													<td class="align-middle text-center"><?php echo substr($stock_items_rs[5], 0, 10); ?></td>

													<?php
														if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[4] == '1' && $role_rs[5] == '1')) {
															?><td class="align-middle text-center">
																<div class="btn-group col-md-12">
																	<button type="button" class="btn btn-primary btn-sm">Action</button>
																	<button type="button" class="btn btn-primary btn-sm dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">
																		<span class="caret"></span>
																	</button>
																	<div class="dropdown-menu" style="min-width: 8rem;">
																		<a href="#" class="dropdown-item text-primary btnUpdateStockItem" id="<?php echo "Update". $stock_items_rs[0]; ?>" data-id="<?php echo $stock_items_rs[0]; ?>" data-toggle="modal" data-target="#updateStockItemModal"><i class="fa fa-edit"></i> Edit</a>
																		<a href="#" class="dropdown-item text-danger btnDeleteStockItem" id="<?php echo "Delete". $stock_items_rs[0]; ?>" data-id="<?php echo $stock_items_rs[0]; ?>"><i class="fa fa-trash"></i> Delete</a>
																	</div>
																</div>
															</td><?php
														} else if ($role_rs[4] == '1') {
															?><td class="align-middle text-center">
																<div class="btn-group col-md-12">
																	<button type="button" class="btn btn-primary btn-sm">Action</button>
																	<button type="button" class="btn btn-primary btn-sm dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">
																		<span class="caret"></span>
																	</button>
																	<div class="dropdown-menu" style="min-width: 8rem;">
																		<a href="#" class="dropdown-item text-primary btnUpdateStockItem" id="<?php echo "Update". $stock_items_rs[0]; ?>" data-id="<?php echo $stock_items_rs[0]; ?>" data-toggle="modal" data-target="#updateStockItemModal"><i class="fa fa-edit"></i> Edit</a>
																	</div>
																</div>                                                            
															</td><?php
														} else if ($role_rs[5] == '1') {
															?><td class="align-middle text-center">
																<div class="btn-group col-md-12">
																	<button type="button" class="btn btn-primary btn-sm">Action</button>
																	<button type="button" class="btn btn-primary btn-sm dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">
																		<span class="caret"></span>
																	</button>
																	<div class="dropdown-menu" style="min-width: 8rem;">
																		<a href="#" class="dropdown-item text-danger btnDeleteStockItem" id="<?php echo "Delete". $stock_items_rs[0]; ?>" data-id="<?php echo $stock_items_rs[0]; ?>"><i class="fa fa-trash"></i> Delete</a>
																	</div>
																</div>  
															</td><?php
														}
													?>

												</tr><?php
												$count_stock_items++;
                                            }
                                        ?>
                                    </tbody>
                                </table>

							</div>
						</div>
					</div>
				</div>
			</div>
		</main>
	
		<!-- Essential javascripts for application to work-->
		<?php include("library/footer.php");?>	    	
		<?php include("library/scripts.php"); ?>
		<script type="text/javascript"> 
		    $("#stockItemsTable").DataTable({"pageLength": 100})
		    $("#cmbCategoryNameRegisterNewStockItem0, #cmbUnitNameRegisterNewStockItem0").select2({ tags: false, dropdownParent: $("#registerStockItemsModal") })
		    $("#cmbCategoryNameUpdateStockItem, #cmbUnitNameUpdateStockItem").select2({ tags: false, dropdownParent: $("#updateStockItemModal") })
		</script>
		
	</body>
</html>