
<?php ob_start(); ?>
<?php
 include('../config/constants.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>shopping cart</title>

   <!-- font awesome cdn link  -->
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">

   <!-- custom css file link -->
   <link rel="stylesheet" href="css/pstyle.css">

</head>
<body>


<div class="container">

<section class="viewcart">

   <h1 class="heading">shopping cart</h1>

   <table>

      <thead>
         <th>image</th>
         <th>name</th>
         <th>Size</th>
         <th>price</th>
         <th>quantity</th>

         <th>total price</th>
         <th>action</th>
      </thead>

      <tbody>

         <?php
         if (isset($_SESSION['userid'])) {
             // code...
             $userid = $_SESSION['userid'];
         }
        
         $total = 0;
         $grandtotal = 0;

         $count = 0;
        

        // Get the products in the cart for the logged-in user
$cart_query = "SELECT prod_id, qty, size FROM cart WHERE user_id = '$userid'";
$cart_result = mysqli_query($conn, $cart_query);

// Initialize an array to store the product details
$products = array();

// Loop through the results and get the product details from the products table
while ($cart_row = mysqli_fetch_assoc($cart_result)) {
  $prod_id = $cart_row['prod_id'];
  $qty = $cart_row['qty'];
  $size = $cart_row['size'];
  // Get the product details
  $product_query = "SELECT title, price, image_name, qty FROM products WHERE id = '$prod_id'";
  $product_result = mysqli_query($conn, $product_query);
  $product_row = mysqli_fetch_assoc($product_result);
  $qty2 = $product_row['qty'];
  $name = $product_row['title'];
  $price = $product_row['price'];
  $image = $product_row['image_name'];

  // Add the product details to the products array
  $products[] = array(
    'id' => $prod_id,
    'title' => $name,
    'price' => $price,
    'image_name' => $image,
    'qty' => $qty,
    'size' => $size,
    'qty2' => $qty2
  );
}

    // Loop through the products array and echo the data
foreach ($products as $key => $product) {
  
  $i = $key;
  $i++;
  $count = $i;
  $_SESSION['count'] = $count;
  ?>
  <form action="" method="post" enctype="multipart/form-data">
         <tr>
           <td>
            <img src="<?php echo SITEURL; ?>images/products/<?php echo $product['image_name']?>" width="50px">
           </td>

            <td><?php echo $product['title']; ?></td>
              <td><?php echo $product['size']; ?></td>
            <td>$<?php echo number_format($product['price']); ?>/-</td>
            
            <td >

                <div class="qtyupdate">
                  <?php if ($product['qty'] >$product['qty2']) {
                    //echo $product['qty2']; 
                    ?>
                    <input type="number" name="qty" min="1" max="<?php echo $product['qty2']; ?>" value="<?php echo $product['qty2']; ?>">
                    <?php
                   
                    
                   
                  }else{
                     ?>
                    <input type="number" name="qty" min="1" max="<?php echo $product['qty2']; ?>" value="<?php echo $product['qty']; ?>">
                    <?php
                    

                   
                  }
                  ?>
                  <input type="submit" value="update" name="update">
                </div>
                 

            </td>
            <?php 
             $grandtotal =  $grandtotal + (int)$product['qty']* (int) $product['price'];
            ?>

            <td>$<?php echo $total = (int)$product['price'] * (int)$product['qty']; ?>/-</td>
            <td> <input type="submit" name="remove" value="remove" onclick="return confirm('remove item from cart?')" class="delete-btn"></td>
            <td>
                  <input type="hidden" name="title" value="<?php echo $product['title']?>">
                  
                 
                 
            </td>
            
            <td> <input type='hidden' name="price" value="<?php echo $product['price']?>"></td>
            <td><input type="hidden" name="id" value="<?php echo $product['id'] ?>"></td>
            <td> 
                  <input type="hidden" name="size" value="<?php echo $product['size'] ?>"></td>

         </tr>
       </form>
       <?php 



}

            
       

         

        
    
         ?>
         <tr class="table-bottom">
           <form action="" method="post">
            <?php 

            ?>
            <td><a href="index.php" class="option-btn" style="margin-top: 0;">continue shopping</a></td>
            <td colspan="3">grand total</td>
            <td>$<?php echo number_format($grandtotal,2); ?>/-</td>

            <td><input type="submit" value="delete all" name="clear" onclick="return confirm('are you sure you want to delete all?');" class="delete-btn"></td>
            </form>

            <?php
              if (isset($_POST['clear'])) {
                // code...
                $_SESSION['cart'] = array();
              }

              if (isset($_POST['remove'])) {
                $id = $_POST['id'];
                  $sqlremove = "DELETE  FROM cart WHERE prod_id = $id AND user_id = $userid";
                  $resremove = mysqli_query($conn, $sqlremove);
              }
              if (isset($_POST['clear'])) {
                  $sqlremoveall = "DELETE  FROM cart WHERE user_id = $userid";
                  $resremoveall = mysqli_query($conn, $sqlremoveall);
              }
              if (isset($_POST['update'])) {
                  // code...
                $qty2 = $_POST['qty'];
                $id2 = $_POST['id'];
                 $sqlupdate= "UPDATE cart SET
              qty = '$qty2'

              WHERE prod_id = $id2 AND user_id = $userid
            ";

            //execute the querry
            $resupdate = mysqli_query($conn, $sqlupdate);


              }
             ?>
         </tr>

      </tbody>

   </table>

   <div class="checkoutbtn">
      <a href="checkout.php" class="btn1 <?php echo ($grandtotal > 1)?'':'disabled'; ?>">procced to checkout</a>
   </div>

</section>

</div>









<!-- custom js file link  -->
<script src="js/script.js"></script>

</body>
</html>
