
<?php ob_start(); ?>
<?php
 include('../config/constants.php');
?>
<?php



if(isset($_POST['submit'])){
   $_SESSION['delete-cart'];
   $name = $_POST['name'];
   $number = $_POST['number'];
   $email = $_POST['email'];
   $method = $_POST['method'];
   $street = $_POST['street'];
   $totalproduct = $_POST['totalproduct'];
   $grandtotal = $_POST['grandtotal'];
   $city = $_POST['city'];
   $status = $_POST['status'];
   $detail_query = mysqli_query($conn, "INSERT INTO `tbl_order`(full_name, email, phone_number,method,total_products, total_price, city, street,status) VALUES('$name','$email','$number','$method','$totalproduct','$grandtotal','$city','$street','$status')") or die('query failed');

   if($detail_query){

      echo "
      <div class='order-message-container'>
      <div class='message-container'>
         <h3>thank you for shopping!</h3>
         <div class='order-detail'>
            <span>".$totalproduct."</span>
            <span class='total'> total : $".$grandtotal."/-  </span>

         </div>
         <div class='customer-details'>
            <p> your name : <span>".$name."</span> </p>
            <p> your number : <span>".$number."</span> </p>
            <p> your email : <span>".$email."</span> </p>
            <p> your address : <span> ".$street.", ".$city." </span> </p>
            <p> your payment mode : <span>".$method."</span> </p>
            <p>(*pay when product arrives*)</p>
         </div>
            <a href='index.php' class='btn'>continue shopping</a>
         </div>
      </div>
      ";
      if (isset($_SESSION['userid'])) {
             // code...
             $userid = $_SESSION['userid'];
         }


            $cart_query2 = "SELECT prod_id, qty, size FROM cart WHERE user_id = '$userid'";
$cart_result2 = mysqli_query($conn, $cart_query2);

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

// Loop through the results and get the product details from the products table
while ($cart_row2 = mysqli_fetch_assoc($cart_result2)) {
  $prod_id2 = $cart_row2['prod_id'];
  $qty2 = $cart_row2['qty'];
  $size2 = $cart_row2['size'];
  // Get the product details
  $product_query2 = "SELECT title, price, image_name, qty FROM products WHERE id = '$prod_id2'";
  $product_result2 = mysqli_query($conn, $product_query2);
  $product_row2 = mysqli_fetch_assoc($product_result2);

  $name2 = $product_row2['title'];
  $price2 = $product_row2['price'];
  $image2= $product_row2['image_name'];
  //$qty2 = $product_row2['qty'];

  // Add the product details to the products array
  $products2[] = array(
    'id' => $prod_id2,
    'title' => $name2,
    'price' => $price2,
    'image_name' => $image2,
    'qty' => $qty2,
    'size' => $size2
  );
}
// Loop through the items in the shopping cart
foreach ($products2 as $item2) {
    $product_id2 = $item2['id'];
    $ordered_qty = $item2['qty'];

    // Get the current quantity of the product from the database
    $sql = "SELECT qty FROM products WHERE id = $product_id2";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    $current_qty = $row['qty'];

    // Update the quantity of the product in the database
    $updated_qty = $current_qty - $ordered_qty;
    $sql = "UPDATE products SET qty = $updated_qty WHERE id = $product_id2";

    if (mysqli_query($conn, $sql)) {
        echo "Product ID $product_id quantity updated successfully<br>";
         $sql3 = "DELETE FROM cart WHERE user_id=$userid";
    //execute the query
    $res3 = mysqli_query($conn, $sql3);
    } else {
        echo "Error updating product ID $product_id quantity: " . mysqli_error($conn) . "<br>";
    }
}


     
      $_SESSION['cart'] = array();
   }

}


?>

<!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>checkout</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/style4.css">
   <link rel="stylesheet" href="css/css.css">

</head>
<body>
   <div class="checkout-container">
         <a href="index.php">Home</a>
      </div>
<div class="container">
      
      <section class="checkout-form">
      <h3 class="heading">Complete your order</h3>
      <form class="form" action="" method="post" enctype="multipart/form-data">
         
         <div class="display-order">
             

  
         <?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 FROM products WHERE id = '$prod_id'";
  $product_result = mysqli_query($conn, $product_query);
  $product_row = mysqli_fetch_assoc($product_result);

  $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
  );
}

    // Loop through the products array and echo the data
foreach ($products as $key => $product) {
   $product_name[] = $product['title'] .' '.$product['qty'].' '.$product['size'].' ';
             $i = $key;
             $i++;
             $count = $i;

             $grandtotal =  $grandtotal + (int)$product['qty']* (int) $product['price'];
            
  
 $count;
}

            $total_product = implode(' ,',$product_name);
         ?>
            <span class="products">
               <?php echo $total_product ?>
            </span>
            <span class="grand-total"> grand total : $<?php echo $grandtotal; ?>/- </span>
         </div>
         <div class="flex">
            <div class="inputBox">
                  <span>your name</span>
                  <input type="text" placeholder="enter your name" name="name" required>
               </div>
             
               <div class="inputBox">
                  <span>your number</span>
                  <input type="number" placeholder="enter your number" name="number" required>
               </div>
               <div class="inputBox">
                  <span>your email</span>
                  <input type="email" placeholder="enter your email" name="email" required>
               </div>
               

               <div class="inputBox">
                  <span>Street</span>
                  <input type="text" placeholder="e.g. Macedonia" name="street" required>
               </div>
               <div class="inputBox">
                  <span>city</span>
                  <input type="text" placeholder="e.g. Nairobi" name="city" required>
               </div>
               <div class="inputBox">
                  <span>payment method</span>
                  <select name="method">
                     <option value="cash on delivery" selected>cash on devlivery</option>
                     <option value="credit cart">credit cart</option>
                     <option value="paypal">paypal</option>
                  </select>
               </div>


         </div>
          
            <input class="btn" type="submit" name="submit" value="order now">
          <input type="hidden" value="Ordered" name="status">
         <input type="hidden" value="<?php echo $total_product?>" name="totalproduct" class="btn">
         <input type="hidden" value="<?php echo $grandtotal?>" name="grandtotal" class="btn">
       
      
      </form>
    
      
   </section>
   </div>
  
<!-- custom js file link  -->
<script src="js/script.js"></script>









</body>
</html>
