<?php
session_start();
error_reporting(0);
include('includes/config.php');
include_once('includes/settings.php');
if (strlen($_SESSION['login']) == 0) {
	header('location:login.php');
} else {
	// Code forProduct deletion from  wishlist	
	$wid = intval($_GET['del']);
	if (isset($_GET['del'])) {
		$query = mysqli_query($con, "delete from wishlist where id='$wid'");
	}


	if (isset($_GET['action']) && $_GET['action'] == "add") {
		$id = intval($_GET['id']);
		$query = mysqli_query($con, "delete from wishlist where productId='$id'");
		if (isset($_SESSION['cart'][$id])) {
			$_SESSION['cart'][$id]['quantity']++;
		} else {
			$sql_p = "SELECT * FROM products WHERE id={$id}";
			$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']);
				header('location:my-wishlist.php');
			} else {
				$message = "Product ID is invalid";
			}
		}
	}

	?>
	<!DOCTYPE html>
	<html lang="en">
	<?php include('includes/head.php'); ?>

	<body class="cnt-home">
		<header class="header-style-1">

			<!-- ============================================== TOP MENU ============================================== -->
			<?php include('includes/top-header.php'); ?>
			<!-- ============================================== TOP MENU : END ============================================== -->
			<?php include('includes/main-header.php'); ?>
			<!-- ============================================== NAVBAR ============================================== -->
			<?php include('includes/menu-bar.php'); ?>
			<!-- ============================================== NAVBAR : END ============================================== -->

		</header>

		<!-- ============================================== HEADER : END ============================================== -->

		<!-- Breadcrumb -->
		<div class="breadcrumb-container bg-light py-3 border-bottom mb-4">
			<div class="container">
				<nav aria-label="breadcrumb">
					<ol class="breadcrumb mb-0">
						<li class="breadcrumb-item"><a href="index.php" class="text-decoration-none"><i
									class="fa fa-home"></i> Home</a></li>
						<li class="breadcrumb-item active" aria-current="page">My Account</li>
						<li class="breadcrumb-item active" aria-current="page">Wishlist</li>
					</ol>
				</nav>
			</div>
		</div>

		<div class="body-content pb-5">
			<div class="container">
				<div class="checkout-box shadow-sm rounded-4 overflow-hidden bg-white">
					<div class="row g-0">
						<!-- Main Content -->
						<div class="col-lg-9 border-end">
							<div class="p-4">
								<div class="d-flex justify-content-between align-items-center mb-4">
									<h3 class="display-6 fw-bold mb-0">My Wishlist</h3>
									<span class="badge bg-danger rounded-pill"><i class="fa fa-heart"></i> Favorites</span>
								</div>

								<div class="row g-3">
									<?php
									$ret = mysqli_query($con, "SELECT products.productName as pname, products.id as proid, products.productImage1 as pimage, products.productPrice as pprice, products.productPriceBeforeDiscount as pbefore, products.productAvailability as pavail, wishlist.productId as pid, wishlist.id as wid FROM wishlist JOIN products ON products.id=wishlist.productId WHERE wishlist.userId='" . $_SESSION['id'] . "' AND products.approval_status='approved'");
									$num = mysqli_num_rows($ret);
									if ($num > 0) {
										while ($row = mysqli_fetch_array($ret)) {
											$disc = 0;
											if ($row['pbefore'] > 0 && $row['pbefore'] > $row['pprice']) {
												$disc = round((($row['pbefore'] - $row['pprice']) / $row['pbefore']) * 100);
											}
											?>
											<div class="col-6 col-md-4 col-xl-3">
												<div class="am-card">
													<?php if ($disc > 0): ?>
														<div class="discount-tag">
															<?php echo $disc; ?>% <small>OFF</small>
														</div>
													<?php endif; ?>

													<!-- Remove Button -->
													<button type="button" data-del-id="<?php echo htmlentities($row['wid']); ?>"
														class="btn-wishlist-delete"
														style="position: absolute; top: 8px; right: 8px; z-index: 10; color: #ff4757; background: white; border: 1px solid #eee; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; box-shadow: 0 2px 4px rgba(0,0,0,0.1); transition: all 0.3s ease;"
														onmouseover="this.style.background='#ff4757'; this.style.color='white';"
														onmouseout="this.style.background='white'; this.style.color='#ff4757';"
														title="Remove from wishlist">
														<i class="fa fa-trash-o" style="font-size: 1rem;"></i>
													</button>

													<div class="img-wrap">
														<a href="product-details.php?pid=<?php echo htmlentities($row['pid']); ?>">
															<img src="__admin__/productimages/<?php echo htmlentities($row['pid']); ?>/<?php echo htmlentities($row['pimage']); ?>"
																alt="<?php echo htmlentities($row['pname']); ?>"
																onerror="this.src='assets/images/blank.gif'">
														</a>
													</div>

													<div class="p-name">
														<a href="product-details.php?pid=<?php echo htmlentities($row['pid']); ?>"
															class="text-decoration-none">
															<?php echo htmlentities($row['pname']); ?>
														</a>
													</div>

													<div class="price-wrap">
														<span class="p-price">
															<?php echo htmlentities($settings['currency_symbol'] ?? 'GH₵'); ?>
															<?php echo number_format($row['pprice'], 2); ?>
														</span>
														<?php if ($disc > 0): ?>
															<span class="p-old-price">
																<?php echo htmlentities($settings['currency_symbol'] ?? 'GH₵'); ?>
																<?php echo number_format($row['pbefore'], 2); ?>
															</span>
														<?php endif; ?>
													</div>

													<?php if ($row['pavail'] == 'In Stock'): ?>
														<a href="my-wishlist.php?page=product&action=add&id=<?php echo $row['pid']; ?>"
															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; font-size: 0.7rem;">Out of Stock</span>
													<?php endif; ?>
												</div>
											</div>
										<?php }
									} else { ?>
										<div class="col-12 text-center py-5">
											<div class="py-5">
												<i class="fa fa-heart-o fa-4x text-muted opacity-25 mb-4"></i>
												<h4 class="fw-bold">Your wishlist is empty</h4>
												<p class="text-muted mb-4">Save items you love to find them easily later!</p>
												<a href="shop.php"
													class="btn btn-primary px-5 py-2 rounded-pill fw-bold">Explore Products</a>
											</div>
										</div>
									<?php } ?>
								</div>
							</div>
						</div>

						<!-- Sidebar -->
						<div class="col-lg-3 bg-light p-0">
							<div class="sticky-top" style="top: 2rem;">
								<?php include('includes/myaccount-sidebar.php'); ?>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>

		<?php include('includes/footer.php'); ?>
		<?php include('includes/foot.php'); ?>
	</body>

	</html>
<?php } ?>