<?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_products'") 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>Products | <?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> PRODUCTS</h4>
				</div>
				<ul class="app-breadcrumb breadcrumb">
					<li class="breadcrumb-item"><i class="fa fa-home"></i></li>
					<li class="breadcrumb-item"><a href="products">Products</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="registerNewRestaurantProductForm" 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 Product</h5>
								</div>
								<div class="form-group col-md-4 text-center">
									<img class="rounded" src="img/upload-image.png" id="restaurantProductPhotoTarget" 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="restaurantProductPhoto" id="restaurantProductPhoto" hidden>
									</label>
									<input type="hidden" id="photoNewRestaurantProduct1" name="photoNewRestaurantProduct1"/>                        

									<script>
										var rest_product_photo_src = document.getElementById("restaurantProductPhoto");
										var rest_product_photo_target = document.getElementById("restaurantProductPhotoTarget");

										function showRestaurantProductPhoto(rest_product_photo_src, rest_product_photo_target) {
											var upl = new FileReader();
											upl.onload = function (e) {
												rest_product_photo_target.src = this.result;
											};
											rest_product_photo_src.addEventListener("change", function () {
												upl.readAsDataURL(rest_product_photo_src.files[0]);
											})
										}
										showRestaurantProductPhoto(rest_product_photo_src, rest_product_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">Product Name</label><input class="form-control font-weight-bold" type="text" name="txtProductNameNewRestaurantProduct" id="txtProductNameNewRestaurantProduct" placeholder="Exmple: burger, shuwaarma" required>
										</div>
										<div class="form-group col-md-12">
											<label class="control-label">Product Category</label>
											<select id="cmbProductCategoryNewRestaurantProduct" name="cmbProductCategoryNewRestaurantProduct" 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-5">
											<label class="control-label">Price($)</label>
											<input class="form-control" type="number" name="txtProductPriceNewRestaurantProduct" id="txtProductPriceNewRestaurantProduct" step="0.01" placeholder="Exmple: 10" required>
										</div>
										<div class="form-group col-md-7">
											<label class="control-label">Section</label>
											<select id="cmbProductSectionNewRestaurantProduct" name="cmbProductSectionNewRestaurantProduct" class="form-control" style="width:100%" required />

											</select>
										</div>
										<div class="form-group col-md-5">
											<label class="control-label">Kitchen</label>
											<select id="cmbProcessingKitchenNewRestaurantProduct" name="cmbProcessingKitchenNewRestaurantProduct" class="form-control" style="width:100%" required />
	
											</select>
										</div>
										<div class="form-group col-md-7">
											<label class="control-label">Desciption</label>
											<textarea class="form-control" rows="2" name="txtProductDescriptionNewRestaurantProduct" id="txtProductDescriptionNewRestaurantProduct" placeholder="Product description" required></textarea>
											<input class="form-control" type="hidden" name="txtRegisteredByNewRestaurantProduct" id="txtRegisteredByNewRestaurantProduct" value="<?php echo $_SESSION['userIdd']; ?>" required>
											<input class="form-control" type="hidden" name="txtRegisterDateNewRestaurantProduct" id="txtRegisterDateNewRestaurantProduct" 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="btnRegisterNewRestaurantProduct" id="btnRegisterNewRestaurantProduct"><i class="fa fa-fw fa-lg fa-save"></i>Register Product</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> Products List</span>
							</div>
						</div>
					</div>
					
					<div class="row">
						<div class="col-md-12">
                            <div class="row mt-3 text-right">
                                <div class="col-md-8"></div>
                                <div class="col-lg-4">
									<div class="form-group has-search">
										<span class="fa fa-search fa-lg form-control-feedback text-warning pl-2"></span>
										<input type="text" class="form-control pt-3 pb-3 rounded-pill" name="txtSearchRestaurantProductProductsPage" id="txtSearchRestaurantProductProductsPage" placeholder="Search product" autocomplete="off">
									</div>                                       
								</div>
                            </div>
						</div>
					</div>
					
					<div id="restaurantProductsProductsPageArea">
					    
					    <?php
    						$getRestaurantCategories = mysqli_query($conn, "SELECT restaurant_products.categoryID, restaurant_categories.categoryName, working_areas.workingAreaName, restaurant_categories.categoryPhoto
    						FROM restaurant_products
    						JOIN restaurant_categories ON restaurant_products.categoryID = restaurant_categories.categoryID
    						JOIN working_areas ON restaurant_categories.workingAreaID = working_areas.workingAreaID
    						GROUP BY restaurant_categories.categoryID
    						ORDER BY restaurant_categories.categoryName ASC") or die(mysqli_error($conn));
    						if ($getRestaurantCategories) {
    							if (mysqli_num_rows($getRestaurantCategories) > 0) {
    								while ($rest_categories_rs = mysqli_fetch_array($getRestaurantCategories)) { ?>
    									<div class="row mt-2 mx-auto">
    										<div class="col-md-12 bg-primary pt-2 rounded-top">
    											<h4 class="text-white"><img class="img-fluid rounded" src="<?php echo $rest_categories_rs[3]; ?>" width="25px" height="25px" /> <?php echo $rest_categories_rs[1]; ?> (<?php echo $rest_categories_rs[2]; ?>)</h4>
    										</div>
    										<div class="col-md-12 pt-2 rounded">
    											<div class="row pt-2">
    												<?php
    													$getCategoryProducts = mysqli_query($conn, "SELECT restaurant_products.productID, restaurant_products.productName, restaurant_products.productPrice, working_areas.workingAreaName, restaurant_products.productStatus, restaurant_products.productPhoto
    													FROM restaurant_products
    													JOIN working_areas ON restaurant_products.workingAreaID = working_areas.workingAreaID
    													WHERE categoryID = '". $rest_categories_rs[0] ."'
    													ORDER BY restaurant_products.productName ASC") or die(mysqli_error($conn));
    													if ($getCategoryProducts) {
    														if (mysqli_num_rows($getCategoryProducts) > 0) {
    															while ($category_products_rs = mysqli_fetch_array($getCategoryProducts)) { ?>
    																<div class="col-md-4 pb-3 rounded-lg">
    																	<div class="bs-component">
    																		<div class="card">
    																			<img class="rounded-top" style="height: 200px; width: 100%; display: block;" src="<?php echo $category_products_rs[5]; ?>" alt="Card image">
    																			<div class="card-body">
    																				<div class="row">
    																					<div class="col-md-12"><h5 class="text-primary text-center"><?php echo $category_products_rs[1]; ?></h5></div>
    																				</div>
    																				<div class="row">
    																					<div class="col-md-12"><h6 class="text-success text-center"><?php echo "$". number_format($category_products_rs[2], 2); ?></h6></div>
    																				</div>
    																				<div class="row pt-2">
    																					<div class="col-md-4">
    																						<span class="badge badge-success p-2"><i class="fa fa-star text-warning"></i> <?php echo $category_products_rs[3]; ?></span>
    																					</div>
    																					<div class="col-md-8">
    																						<?php
    																							if (($_SESSION['userTypeID'] == 'PS0001') || ($role_rs[4] == '1' && $role_rs[5] == '1')) {
    																								?><div class="btn-group btn-group-sm pull-right" role="group" aria-label="Basic example">
    																									<button type="button" class="btn btn-primary">
    																										<span class="text-white btnUpdateRestaurantProduct" id="<?php echo "Update". $category_products_rs[0]; ?>" data-id="<?php echo $category_products_rs[0]; ?>" data-toggle="modal" data-target="#updateRestaurantProductModal"><i class="fa fa-pencil"></i>Edit</span>
    																									</button>
    																									<button type="button" class="btn btn-danger">
    																										<span class="text-white btnDeleteRestaurantProduct" id="<?php echo "Delete". $category_products_rs[0]; ?>" data-id="<?php echo $category_products_rs[0]; ?>"><i class="fa fa-trash"></i>Delete</span>
    																									</button>
    																								</div><?php
    																							} else if ($role_rs[4] == '1') {
    																								?><div class="btn-group btn-group-sm pull-right" role="group" aria-label="Basic example">
    																									<button type="button" class="btn btn-primary">
    																										<span class="text-white btnUpdateRestaurantProduct" id="<?php echo "Update". $category_products_rs[0]; ?>" data-id="<?php echo $category_products_rs[0]; ?>" data-toggle="modal" data-target="#updateRestaurantProductModal"><i class="fa fa-pencil"></i>Edit</span>
    																									</button>
    																								</div><?php
    																							} else if ($role_rs[5] == '1') {
    																								?><div class="btn-group btn-group-sm pull-right" role="group" aria-label="Basic example">
    																									<button type="button" class="btn btn-danger">
    																										<span class="text-white btnDeleteRestaurantProduct" id="<?php echo "Delete". $category_products_rs[0]; ?>" data-id="<?php echo $category_products_rs[0]; ?>"><i class="fa fa-trash"></i>Delete</span>
    																									</button>
    																								</div><?php
    																							}
    																						?>
    																					</div>
    																				</div>
    																			</div>
    																		</div>
    																	</div>
    																</div><?php
    															}
    														}
    													}
    												?>
    											</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">
			$("#cmbProductCategoryNewRestaurantProduct, #cmbProductSectionNewRestaurantProduct, #cmbProcessingKitchenNewRestaurantProduct").select2({ tags: false })
			$("#cmbProductCategoryUpdateRestaurantProduct, #cmbProcessingKitchenUpdateRestaurantProduct").select2({ tags: false, dropdownParent: $("#updateRestaurantProductModal") })
		</script>

	</body>
</html>