<?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 = 'restaurant_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>
        <?php include("library/head.php");?>
        <title>Categories | <?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">
			<div class="app-title">
				<div>
					<h4 class="text-primary"><i class="fa fa-list fa-lg"></i> CATEGORIES</h4>
				</div>
				<ul class="app-breadcrumb breadcrumb">
					<li class="breadcrumb-item"><i class="fa fa-home"></i></li>
					<li class="breadcrumb-item"><a href="categories">Categories</a></li>
				</ul>
			</div>
      
			<?php include("models/admin_config.php");?>
			<?php include("models/system_config.php");?>

			<div class="row">

				<div class="col-md-4">
					<div class="tile">
						<form action="#" method="POST" id="registerNewRestaurantCategoryForm" enctype="multipart/form-data" >
							<div class="row">
								<div class="form-group col-md-12">
									<h5 class="text-primary"><i class="fa fa-plus"></i> Add New Category</h5>
								</div>
								<div class="form-group col-md-4 text-center">
									<img class="rounded" src="img/upload-image.png" id="restaurantCategoryPhotoTarget" width="100%" height="100px" /><br>
									<label class="btn btn-info btn-sm d-block mt-1">
										<i class="fa fa-upload"> </i> Photo<input type="file" class="form-control" name="restaurantCategoryPhoto" id="restaurantCategoryPhoto" hidden>
									</label>
									<input type="hidden" id="photoNewRestaurantCategory1" name="photoNewRestaurantCategory1"/>                        

									<script>
										var rest_category_photo_src = document.getElementById("restaurantCategoryPhoto");
										var rest_category_photo_target = document.getElementById("restaurantCategoryPhotoTarget");

										function showRestaurantCategoryPhoto(rest_category_photo_src, rest_category_photo_target) {
											var upl = new FileReader();
											upl.onload = function (e) {
												rest_category_photo_target.src = this.result;
											};
											rest_category_photo_src.addEventListener("change", function () {
												upl.readAsDataURL(rest_category_photo_src.files[0]);
											})
										}
										showRestaurantCategoryPhoto(rest_category_photo_src, rest_category_photo_target);
									</script>
								</div>

								<div class="form-group col-md-8">
									<div class="row">
										<div class="form-group col-md-12">
											<label class="control-label">Category Name</label>
											<input class="form-control font-weight-bold" type="text" name="txtCategoryNameNewRestaurantCategory" id="txtCategoryNameNewRestaurantCategory" placeholder="Exmple: Launch, pizza" required>
										</div>
										<div class="form-group col-md-12">
											<label class="control-label">Category Section</label>
											<select id="cmbCategorySectionNewRestaurantCategory" name="cmbCategorySectionNewRestaurantCategory" class="form-control" style="width:100%" required />

											</select>
										</div>
									</div>
								</div>
								<div class="form-group col-md-12">
									<div class="row">
										<div class="form-group col-md-12">
											<label class="control-label">Category Desciption</label>
											<textarea class="form-control" rows="2" name="txtCategoryDescriptionNewRestaurantCategory" id="txtCategoryDescriptionNewRestaurantCategory" placeholder="Category description" required></textarea>
											<input class="form-control" type="hidden" name="txtRegisteredByNewRestaurantCategory" id="txtRegisteredByNewRestaurantCategory" value="<?php echo $_SESSION['userIdd']; ?>" required>
											<input class="form-control" type="hidden" name="txtRegisterDateNewRestaurantCategory" id="txtRegisterDateNewRestaurantCategory" value="<?php echo date("Y-m-d H:i:s"); ?>" required>
										</div>
										<div class="form-group col-md-12" style="text-align: right;">
											<button type="submit" class="btn btn-success" name="btnRegisterNewRestaurantCategory" id="btnRegisterNewRestaurantCategory"><i class="fa fa-fw fa-lg fa-save"></i>Register Category</button>
										</div>
									</div>
								</div>
							</div>
						</form>	
					</div>
				</div>

				<div class="col-md-8">
					<div class="tile">
						<div class="row">
							<div class="col-md-12">
								<span class="text-primary font-weight-bold" style="font-size: 17px;"><i class="fa fa-list-ul"></i> Categories List</span>
							</div>
						</div>
					</div>
					<div class="row mt-4">
						<?php
							$getRestaurantCategories = mysqli_query($conn, "SELECT restaurant_categories.categoryID, restaurant_categories.categoryName, working_areas.workingAreaName, restaurant_categories.categoryDescription, restaurant_categories.categoryPhoto
							FROM restaurant_categories
							JOIN working_areas ON restaurant_categories.workingAreaID = working_areas.workingAreaID
							ORDER BY restaurant_categories.categoryName ASC") or die(mysqli_error($conn));
							while ($category_rs = mysqli_fetch_array($getRestaurantCategories)) { ?>
								<div class="col-md-4 mb-4">
									<div class="bs-component">
										<div class="card">
											<img style="height: 200px; width: 100%; display: block;" src="<?php echo $category_rs[4]; ?>" alt="Card image">
											<div class="card-body">
												<div class="row">
													<div class="col-md-12">
														<h5 style="text-transform: capitalize;"><?php echo $category_rs[1]; ?></h5>
														<span class="badge badge-success p-2"><i class="fa fa-star text-warning"></i> <?php echo $category_rs[2]; ?></span>
													</div>
												</div>
												<div class="row pt-4">
													<div class="col-md-12"><p class="text-justify"><?php echo $category_rs[3]; ?></p></div>
												</div>
												<div class="row pt-2">
													<div class="col-md-12">
														<?php
															if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[4] == '1' && $role_rs[5] == '1')) {
																?><div class="btn-group pull-right">
																	<button type="button" class="btn btn-primary" data-toggle="dropdown"">Action</button>
																	<button type="button" class="btn btn-primary 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 btnUpdateRestaurantCategory" id="<?php echo "Update". $category_rs[0]; ?>" data-id="<?php echo $category_rs[0]; ?>" data-toggle="modal" data-target="#updateRestaurantCategoryModal"><i class="fa fa-edit"></i> Edit</a>
																		<a href="#" class="dropdown-item text-danger btnDeleteRestaurantCategory" id="<?php echo "Delete". $category_rs[0]; ?>" data-id="<?php echo $category_rs[0]; ?>"><i class="fa fa-trash"></i> Delete</a>
																	</div>
																</div><?php
															} else if ($role_rs[4] == '1') {
																?><div class="btn-group pull-right">
																	<button type="button" class="btn btn-primary" data-toggle="dropdown"">Action</button>
																	<button type="button" class="btn btn-primary 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 btnUpdateRestaurantCategory" id="<?php echo "Update". $category_rs[0]; ?>" data-id="<?php echo $category_rs[0]; ?>" data-toggle="modal" data-target="#updateRestaurantCategoryModal"><i class="fa fa-edit"></i> Edit</a>
																	</div>
																</div><?php
															} else if ($role_rs[5] == '1') {
																?><div class="btn-group pull-right">
																	<button type="button" class="btn btn-primary" data-toggle="dropdown"">Action</button>
																	<button type="button" class="btn btn-primary 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 btnDeleteRestaurantCategory" id="<?php echo "Delete". $category_rs[0]; ?>" data-id="<?php echo $category_rs[0]; ?>"><i class="fa fa-trash"></i> Delete</a>
																	</div>
																</div><?php
															}
														?>
													</div>
												</div>
											</div>
										</div>
									</div>
								</div><?php
							}
						?>
					</div>
				</div>
			</div>

		</main>
  
		<!-- Essential javascripts for application to work-->
		<?php include("library/footer.php");?>  
		<?php include("library/scripts.php");?>
		<script type="text/javascript">
			$("#cmbCategorySectionNewRestaurantCategory").select2({ tags: false })
			$("#cmbCategorySectionUpdateRestaurantCategory").select2({ tags: false, dropdownParent: $("#updateRestaurantCategoryModal") })
		</script>

	</body>
</html>