<?php
require('connection.inc.php');
require('security.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);

error_reporting(E_ALL);
ini_set('display_errors', 1);

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    die("User not logged in.");
}

$user_id = $_SESSION['user_id'];

$sql = "SELECT 
            wishlist.id AS wishlist_id,
            products.product_id,
            products.product_name,
            products.product_url_name,
            (
                SELECT unit_name 
                FROM product_units 
                WHERE product_units.product_id = products.product_id 
                AND deleted_at IS NULL 
                ORDER BY unit_id ASC 
                LIMIT 1
            ) AS unit_name,
            (
                SELECT unit_price 
                FROM product_units 
                WHERE product_units.product_id = products.product_id 
                AND deleted_at IS NULL 
                ORDER BY unit_id ASC 
                LIMIT 1
            ) AS unit_price,
            (
                SELECT product_img 
                FROM product_imgs 
                WHERE product_imgs.product_id = products.product_id 
                AND deleted_at IS NULL 
                ORDER BY img_id ASC 
                LIMIT 1
            ) AS product_img
        FROM wishlist
        JOIN products ON wishlist.product_id = products.product_id
        WHERE wishlist.user_id = ?";


$stmt = $conn->prepare($sql);
if (!$stmt) {
    die("Prepare failed: (" . $conn->errno . ") " . $conn->error);
}

$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();

$wishlistItems = [];
while ($row = $result->fetch_assoc()) {
    $wishlistItems[] = $row;
}
?>

<!doctype html>
<html class="no-js" lang="zxx">
<head>
      <meta charset="utf-8">
      <meta http-equiv="x-ua-compatible" content="ie=edge">
      <title>LoveBeri</title>
      <meta name="description" content="">
      <meta name="viewport" content="width=device-width, initial-scale=1">

      <!-- Place favicon.ico in the root directory -->
      <link rel="shortcut icon" type="image/x-icon" href="assets/img/allnew/logo.png">

      <!-- CSS here -->
      <link rel="stylesheet" href="assets/css/bootstrap.min.css">
      <link rel="stylesheet" href="assets/css/animate.css">
      <link rel="stylesheet" href="assets/css/swiper-bundle.css">
      <link rel="stylesheet" href="assets/css/slick.css">
      <link rel="stylesheet" href="assets/css/magnific-popup.css">
      <link rel="stylesheet" href="assets/css/spacing.css">
      <link rel="stylesheet" href="assets/css/meanmenu.css">
      <link rel="stylesheet" href="assets/css/nice-select.css">
      <link rel="stylesheet" href="assets/css/fontawesome.min.css">
      <link rel="stylesheet" href="assets/css/icon-dukamarket.css">
      <link rel="stylesheet" href="assets/css/jquery-ui.css">
      <link rel="stylesheet" href="assets/css/main.css">
   </head>
   <body>


      <!-- Scroll-top -->
      <button class="scroll-top scroll-to-target" data-target="html">
         <i class="icon-chevrons-up"></i>
      </button>
      <!-- Scroll-top-end-->


      <!-- header-area-start -->
     <?php 
     include 'header.php'
     ?>
      <!-- header-area-end -->

      <main>

         <!-- breadcrumb-area-start -->
         <div class="breadcrumb__area pt-5 pb-5">
            <div class="container">
               <div class="row">
                  <div class="col-lg-12">
                     <div class="tp-breadcrumb__content">
                        <div class="tp-breadcrumb__list">
                           <span class="tp-breadcrumb__active"><a href="index.php">Home</a></span>
                           <span class="dvdr">/</span>
                           <span>Wishlist</span>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
         </div>
         <!-- breadcrumb-area-end -->

         <!-- wishlist-area-start -->
         <div class="cart-area pb-80">
            <div class="container">
            <div class="row">
               <div class="col-12">
                     <form action="#">
                        <div class="table-content table-responsive">
                           <table class="table">
                                 <thead>
                                    <tr>
                                       <th class="product-thumbnail">Images</th>
                                       <th class="cart-product-name">Courses</th>
                                       <th class="product-price">Unit Price</th>
                                      
                                       <th class="product-remove">Remove</th>
                                    </tr>
                                 </thead>
                                 <tbody>
                                 <?php if(empty($wishlistItems)): ?>
                                       <tr>
                                             <td colspan="6" class="text-center">
                                                
                                                <img src="assets/img/cart/cartempty.gif" height="150">
                                                <p>Your Wishlist is empty. Add items to get started!</p>
                                                <a href="product.php" class="text-danger">Shop Now</a>
                                             </td>
                                       </tr>
                                    <?php else: ?>
                                    <?php foreach ($wishlistItems as $item): ?>
                                    <tr>
                                       <td class="product-thumbnail">
                                       <a href="product-details.php/<?php echo urlencode($item['product_url_name']); ?>">
                                                   <img src="admin/<?php echo htmlspecialchars($item['product_img']); ?>" alt="" width="80">
                                                </a>
                                       </td>
                                       <td class="product-name">
                                       <a href="product-details.php/<?php echo urlencode($item['product_url_name']); ?>">
                                                   <?php echo htmlspecialchars($item['product_name']); ?>

                                                  

                                                </a>
                                       </td>
                                       <td class="product-price">
                                       <span class="amount">₹<?php echo number_format($item['unit_price'], 2); ?></span>
                                       </td>
                                       <td class="product-remove">
                                          <a  href="javascript:void(0);" onclick="removeFromWishlist(<?php echo $item['wishlist_id']; ?>)">
                                             <i class="fa fa-times"></i>
                                          </a>
                                       </td>
                                    </tr>
                                    <?php endforeach; ?>
                                    <?php endif; ?>
                                 </tbody>
                           </table>
                        </div>
                     </form>
               </div>
            </div>
            </div>
         </div>
         <!-- wishlist-area-end-->

         
         <!-- feature-area-start -->
    
         <!-- feature-area-end -->

      </main>

      <!-- footer-area-start -->
     <?php 
     include "footer.php"
     ?>
      <!-- footer-area-end -->
       
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
      <script>
         function removeFromWishlist(wishlistId) {
            Swal.fire({
               title: 'Are you sure?',
               text: "Do you want to remove this item from Wishlist?",
               icon: 'warning',
               showCancelButton: true,
               confirmButtonColor: '#d33',
               cancelButtonColor: '#3085d6',
               confirmButtonText: 'Yes, remove it!'
            }).then((result) => {
               if (result.isConfirmed) {
                  fetch('remove_from_wishlist.php', {
                     method: 'POST',
                     headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                     },
                     body: `wishlist_id=${wishlistId}`
                  })
                  .then(response => response.json())
                  .then(data => {
                     if (data.success) {
                        Swal.fire({
                           icon: 'success',
                           title: 'Removed!',
                           text: 'The item has been removed from your Wishlist.',
                           timer: 1500,
                           showConfirmButton: false
                        }).then(() => {
                           location.reload();
                        });
                     } else {
                        Swal.fire('Error', 'Failed to remove item from Wishlist.', 'error');
                     }
                  });
               }
            });
         }

      </script>
      

      <!-- JS here -->
      <script src="assets/js/jquery.js"></script>
      <script src="assets/js/waypoints.js"></script>
      <script src="assets/js/bootstrap.bundle.min.js"></script>
      <script src="assets/js/swiper-bundle.js"></script>
      <script src="assets/js/nice-select.js"></script>
      <script src="assets/js/slick.js"></script>
      <script src="assets/js/magnific-popup.js"></script>
      <script src="assets/js/counterup.js"></script>
      <script src="assets/js/wow.js"></script>
      <script src="assets/js/isotope-pkgd.js"></script>
      <script src="assets/js/imagesloaded-pkgd.js"></script>
      <script src="assets/js/countdown.js"></script>
      <script src="assets/js/ajax-form.js"></script>
      <script src="assets/js/jquery-ui.js"></script>
      <script src="assets/js/meanmenu.js"></script>
      <script src="assets/js/main.js"></script>
   </body>
</html>
