<?php
session_start();
error_reporting(0);
include('includes/config.php');
include_once('includes/settings.php');

$find = "%{$_POST['product']}%";

if (isset($_GET['action']) && $_GET['action'] == "add") {
	$id = intval($_GET['id']);
	if (isset($_SESSION['cart'][$id])) {
		$_SESSION['cart'][$id]['quantity']++;
	} else {
		$sql_p = "SELECT * FROM products WHERE id={$id} AND approval_status='approved' AND productAvailability='In Stock' AND stock_quantity > 0";
		$query_p = mysqli_query($con, $sql_p);
		if (mysqli_num_rows($query_p) != 0) {
			$row_p = mysqli_fetch_array($query_p);
			$_SESSION['cart'][$row_p['id']] = array("quantity" => 1, "price" => $row_p['productPrice']);
			echo "<script>alert('Product has been added to the cart')</script>";
			echo "<script type='text/javascript'> document.location ='my-cart.php'; </script>";
		} else {
			$message = "Product ID is invalid";
		}
	}
}

// Code for Wishlist
if (isset($_GET['pid']) && $_GET['action'] == "wishlist") {
	if (strlen($_SESSION['login']) == 0) {
		header('location:login.php');
	} else {
		mysqli_query($con, "insert into wishlist(userId,productId) values('" . $_SESSION['id'] . "','" . $_GET['pid'] . "')");
		echo "<script>alert('Product added in wishlist');</script>";
		header('location:my-wishlist.php');
	}
}
?>
<!DOCTYPE html>
<html lang="en">
<?php include('includes/head.php'); ?>

<body class="cnt-home">

	<header class="header-style-1">
		<?php include('includes/top-header.php'); ?>
		<?php include('includes/main-header.php'); ?>
		<?php include('includes/menu-bar.php'); ?>
	</header>

	<div class="body-content outer-top-xs">
		<div class='container'>
			<div class='row outer-bottom-sm'>
				<div class='col-md-3'>
					<!-- Mobile Filter Toggle -->
					<button class="mobile-filter-btn d-lg-none mb-3">
						<i class="fa fa-filter"></i> Filter Products
					</button>

					<!-- Sidebar Overlay -->
					<div class="sidebar-overlay"></div>

					<div class="shop-sidebar">
						<!-- Mobile Close Button -->
						<div class="close-sidebar d-lg-none">
							<i class="fa fa-times"></i>
						</div>

						<!-- Sub Categories Filter -->
						<div class="filter-section">
							<div class="filter-section-title">
								Sub Categories
							</div>
							<div class="filter-options">
								<ul class="list-unstyled">
									<?php $sql = mysqli_query($con, "select id,subcategory from subcategory");
									while ($row = mysqli_fetch_array($sql)) {
										?>
										<li class="filter-option">
											<a href="sub-category.php?scid=<?php echo $row['id']; ?>"
												class="text-decoration-none text-muted">
												<i class="fa fa-angle-right me-2"></i>
												<?php echo $row['subcategory']; ?>
											</a>
										</li>
									<?php } ?>
								</ul>
							</div>
						</div>

						<!-- Categories Filter -->
						<div class="filter-section">
							<div class="filter-section-title">
								Category
							</div>
							<div class="filter-options">
								<ul class="list-unstyled">
									<?php $sql = mysqli_query($con, "select id,categoryName from category");
									while ($row = mysqli_fetch_array($sql)) {
										?>
										<li class="filter-option">
											<a href="category.php?cid=<?php echo $row['id']; ?>"
												class="text-decoration-none text-muted">
												<i class="fa fa-angle-right me-2"></i>
												<?php echo $row['categoryName']; ?>
											</a>
										</li>
									<?php } ?>
								</ul>
							</div>
						</div>
					</div>
				</div>

				<div class='col-md-9'>
					<!-- ========================================== SECTION – HERO ========================================= -->
					<!-- Banner Removed -->

					<div class="search-result-container">
						<div id="myTabContent" class="tab-content">
							<div class="tab-pane active " id="grid-container">
								<div class="category-product inner-top-vs">
									<h3 class="section-title">Search Results for:
										"<?php echo htmlentities($_POST['product']); ?>"</h3>
									<div class="shop-product-grid">
										<?php
										$ret = mysqli_query($con, "select * from products where productName like '$find' and approval_status='approved'");
										$num = mysqli_num_rows($ret);
										if ($num > 0) {
											while ($row = mysqli_fetch_array($ret)) {
												// Calculate discount
												$disc = 0;
												if ($row['productPriceBeforeDiscount'] > 0 && $row['productPriceBeforeDiscount'] > $row['productPrice']) {
													$disc = round((($row['productPriceBeforeDiscount'] - $row['productPrice']) / $row['productPriceBeforeDiscount']) * 100);
												}
												?>

												<div class="am-card wow fadeInUp">
													<?php if ($disc > 0): ?>
														<div class="discount-tag"><?php echo $disc; ?>% <small>OFF</small></div>
													<?php endif; ?>

													<a href="search-result.php?pid=<?php echo htmlentities($row['id']); ?>&&action=wishlist"
														class="btn-wishlist-app" title="Add to Wishlist">
														<i class="fa fa-heart"></i>
													</a>

													<a href="product-details.php?pid=<?php echo htmlentities($row['id']); ?>"
														class="text-decoration-none">
														<div class="img-wrap">
															<img src="__admin__/productimages/<?php echo htmlentities($row['id']); ?>/<?php echo htmlentities($row['productImage1']); ?>"
																alt="<?php echo htmlentities($row['productName']); ?>"
																onerror="this.src='assets/images/blank.gif'">
														</div>
														<div class="p-name"><?php echo htmlentities($row['productName']); ?>
														</div>
														<div class="price-wrap">
															<span class="p-price">Ghs.
																<?php echo number_format($row['productPrice'], 2); ?></span>
															<?php if ($disc > 0): ?>
																<span class="p-old-price">Ghs.
																	<?php echo number_format($row['productPriceBeforeDiscount'], 2); ?></span>
															<?php endif; ?>
														</div>
													</a>

													<!-- Add to Cart Button -->
													<?php if ($row['productAvailability'] == 'In Stock'): ?>
														<a href="search-result.php?action=add&id=<?php echo $row['id']; ?>"
															class="btn-add-cart-app">
															<i class="fa fa-shopping-cart"></i>
														</a>
													<?php else: ?>
														<span class="badge bg-secondary position-absolute"
															style="bottom:12px; right:12px;">Out of Stock</span>
													<?php endif; ?>
												</div>
											<?php }
										} else { ?>
											<div class="no-products-found wow fadeInUp" style="grid-column: 1 / -1;">
												<i class="fa fa-search"></i>
												<h3>No Product Found</h3>
												<p>We couldn't find what you were looking for. Try searching for something
													else.</p>
											</div>
										<?php } ?>
									</div>
								</div>
							</div>
						</div>
					</div>

				</div>
			</div>
			<?php include('includes/brands-slider.php'); ?>
		</div>
	</div>
	<?php include('includes/footer.php'); ?>
	<script src="assets/js/jquery-1.11.1.min.js"></script>
	<script src="assets/js/bootstrap.min.js"></script>
	<script src="assets/js/bootstrap-hover-dropdown.min.js"></script>
	<script src="assets/js/owl.carousel.min.js"></script>
	<script src="assets/js/echo.min.js"></script>
	<script src="assets/js/jquery.easing-1.3.min.js"></script>
	<script src="assets/js/bootstrap-slider.min.js"></script>
	<script src="assets/js/jquery.rateit.min.js"></script>
	<script type="text/javascript" src="assets/js/lightbox.min.js"></script>
	<script src="assets/js/bootstrap-select.min.js"></script>
	<script src="assets/js/wow.min.js"></script>
	<script src="assets/js/scripts.js"></script>
	<script>
		$(document).ready(function () {
			// Mobile Filter Interaction
			$('.mobile-filter-btn').click(function () {
				$('.shop-sidebar').addClass('active');
				$('.sidebar-overlay').addClass('active');
				$('body').css('overflow', 'hidden'); // Prevent background scrolling
			});

			$('.close-sidebar, .sidebar-overlay').click(function () {
				$('.shop-sidebar').removeClass('active');
				$('.sidebar-overlay').removeClass('active');
				$('body').css('overflow', ''); // Restore scrolling
			});
		});
	</script>
</body>

</html>
