<?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_categories'") 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>
		<title>Manage stock_stock_categories | <?php echo $_SESSION['systemName']; ?></title>
		<?php include("library/head.php");?>
	</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-3">
					<div class="tile">
						<form action="#" method="POST" id="registerNewStockCategoryForm">
							<div class="row">
								<div class="col-md-12">
									<h6 class="text-primary"><i class="fa fa-plus-square"></i> Register New Category</h6>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12">
									<label class="col-form-label col-form-label-sm">Category Name</label>
									<input class="form-control form-control-sm" type="text" name="txtCategoryNameRegisterNewStockCategory" id="txtCategoryNameRegisterNewStockCategory" placeholder="Example: Electronics" required>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12">
									<label class="col-form-label col-form-label-sm">Category Remarks</label>
									<textarea class="form-control form-control-sm" style="resize: none;" rows="4" name="txtCategoryRemarksRegisterNewStockCategory" id="txtCategoryRemarksRegisterNewStockCategory" placeholder="Example: Electronics" required></textarea>
								</div>
							</div>
							<div class="row">
								<div class="form-group col-md-12 text-right mt-3">
									<button type="submit" class="btn btn-success btn-sm" name="btnRegisterNewStockCategory" id="btnRegisterNewStockCategory"><i class="fa fa-fw fa-lg fa-save"></i>Register Category</button>
								</div>
							</div>
						</form>
					</div>
				</div>

				<div class="col-md-9">
					<div class="tile">
						<div class="tile-body">
							<div class="table-responsive">
								<table class="table table-bordered table-sm" id="stockCategoriesTable">
									<thead>
										<tr class="text-center bg-primary text-white">
											<th class="align-middle">No</th>
											<th class="align-middle">Category ID</th>
											<th class="align-middle">Category Name</th>
											<th class="align-middle" width="39%">Remarks</th>
											<th class="align-middle">Register Date</th>
                                            <?php if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[4] == '1') || ($role_rs[5] == '1')) {?><th class="align-middle text-center">Action</th><?php } ?>
										</tr>
									</thead>
									<tbody>
										<?php
											$count_stock_categories = 1;
											$getStockCategories = mysqli_query($conn, "SELECT * FROM stock_categories ORDER BY categoryName ASC") or die(mysqli_error($conn));
											while ($stock_categories_rs = mysqli_fetch_array($getStockCategories)) { ?>

												<tr>
                                                    <td class="align-middle text-center"><?php echo $count_stock_categories; ?></td>
                                                    <td class="align-middle text-center"><?php echo $stock_categories_rs[1]; ?></td>
                                                    <td class="align-middle text-left"><?php echo $stock_categories_rs[2]; ?></td>
                                                    <td class="align-middle text-left"><?php echo substr($stock_categories_rs[3], 0, 45) ." ..."; ?></td>
                                                    <td class="align-middle text-center"><?php echo substr($stock_categories_rs[5], 0, 10); ?></td><?php
                                                    if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[4] == '1' && $role_rs[5] == '1')) {
                                                            ?><td class="text-center">
                                                                <div class="btn-group col-md-12">
                                                                    <button type="button" class="btn btn-primary btn-sm" data-toggle="dropdown">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 btnUpdateStockCategory" id="<?php echo "Update". $stock_categories_rs[1]; ?>" data-id="<?php echo $stock_categories_rs[1]; ?>" data-toggle="modal" data-target="#updateStockCategoryModal"><i class="fa fa-edit"></i> Edit</a>
                                                                        <a href="#" class="dropdown-item text-danger btnDeleteStockCategory" id="<?php echo "Delete". $stock_categories_rs[1]; ?>" data-id="<?php echo $stock_categories_rs[1]; ?>"><i class="fa fa-trash"></i> Delete</a>
                                                                    </div>
                                                                </div>
                                                           </td><?php
                                                        } else if ($role_rs[4] == '1') {
                                                            ?><td class="text-center">
                                                                <div class="btn-group col-md-12">
                                                                    <button type="button" class="btn btn-primary btn-sm" data-toggle="dropdown">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 btnUpdateStockCategory" id="<?php echo "Update". $stock_categories_rs[1]; ?>" data-id="<?php echo $stock_categories_rs[1]; ?>" data-toggle="modal" data-target="#updateStockCategoryModal"><i class="fa fa-edit"></i> Edit</a>
                                                                    </div>
                                                                </div>                                                            
                                                            </td><?php
                                                        } else if ($role_rs[5] == '1') {
                                                            ?><td class="text-center">
                                                                <div class="btn-group col-md-12">
                                                                    <button type="button" class="btn btn-primary btn-sm" data-toggle="dropdown">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 btnDeleteStockCategory" id="<?php echo "Delete". $stock_categories_rs[1]; ?>" data-id="<?php echo $stock_categories_rs[1]; ?>"><i class="fa fa-trash"></i> Delete</a>
                                                                    </div>
                                                                </div>  
                                                            </td><?php
                                                        }
                                                    ?>
												</tr><?php
												$count_stock_categories++;
											}
										?>
									</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">
			$("#stockCategoriesTable").DataTable({"Length": 15, "order": []})
		</script>
	</body>
</html>