<?php   
ob_start(); //এটা এবং এর ইনড-ফ্লাস না দিলে; যদি নেট গতি কম হয় তাহলে ডাটাবেজ পাবে না।
include("configa.php");

session_start();

// Start capturing the output of the category display


if( isset($_GET['catagory']) && isset($_GET['item']) && isset($_GET['ref']) ){
  $catagory = $_GET['catagory'];
  $item = $_GET['item'];
  $ref = $_GET['ref'];
}else{
  // ডিফল্ট মান সেট করো না থাকলে
  $catagory = 1;
  $item = ''; // অথবা উপযুক্ত ডিফল্ট মান
  $ref = ''; // অথবা উপযুক্ত ডিফল্ট মান
  // মন্তব্যও সংশোধন করা উচিত:
  // যদি সরাসরি view.php হিট করে; তাহলে Error এড়াতে ডিফল্ট মান দেওয়া হলো
}


if( isset($_GET['track_number'])  ){

  $track_number = $_GET['track_number'];
 
}else{
  $track_number = 1; //এই কোড $id = 1; না দিলে; যদি সরাসরি view.php হিট করে; তাহলে Error দেখাবেন//
}


if( isset($_GET['mobile'])  ){
  $mobile = $_GET['mobile'];
 
}else{
  $mobile = NULL; //এই কোড $id = 1; না দিলে; যদি সরাসরি view.php হিট করে; তাহলে Error দেখাবেন//
}





?>
<style type="text/css">
  .add{
  cursor: pointer;
}

.add:hover{
  color : magenta;
}

.go{
  cursor: pointer;
}
.go:hover{
  color: red;
}

</style>

  <div style="width: 85%; margin-left: auto; margin-right: auto;  background-color:#eef6f9;" class="catagory_display_container">

  <?php 
 
    $result2 = mysqli_query($conn,"SELECT * FROM goods_category where item = '$item' ") or die(mysqli_error($conn));


    while ($row2 = mysqli_fetch_object($result2) ) {?>  
  <div class="catagory_display">
  <div class="catagory_display_inner" style="background-color: #FDE8C0; padding: 5pt; box-sizing: border-box;  display: flex; flex-direction: column;"  >
  <div>


<div class="add">Add</div> <!-- id="add" বাদ, class="add" রাখুন -->
  <div class="go"> 
  <a href="order_edit.php?track_number=<?php echo$track_number?>"><-----Go-----></a>
  </div>

<input type="hidden" name="id[]" value="<?php echo isset($row2->id) ? $row2->id : ''; ?>">
<input type="hidden" name="category_picture[]" value="<?php echo isset($row2->category_picture) ? $row2->category_picture : ''; ?>">
<input type="hidden" name="product_name[]" value="<?php echo isset($row2->product_name) ? $row2->product_name : ''; ?>">
<input type="hidden" name="item[]" value="<?php echo isset($row2->item) ? $row2->item : ''; ?>">
<input type="hidden" name="person[]" value="<?php echo isset($row2->person) ? $row2->person : ''; ?>">
<input type="hidden" name="gks[]" value="<?php echo isset($row2->gks) ? $row2->gks : ''; ?>">
<input type="hidden" name="seller[]" value="<?php echo isset($row2->seller) ? $row2->seller : ''; ?>">
<input type="hidden" name="price[]" value="<?php echo isset($row2->price) ? $row2->price : ''; ?>">
<input type="hidden" name="discount[]" value="<?php echo isset($row2->discount) ? $row2->discount : ''; ?>">
<input type="hidden" name="current_price[]" value="<?php echo isset($row2->current_price) ? $row2->current_price : ''; ?>">
<input type="hidden" name="size[]" value="<?php echo isset($row2->size) ? $row2->size : ''; ?>">
<input type="hidden" name="weight[]" value="<?php echo isset($row2->weight) ? $row2->weight : ''; ?>">
<input type="number" class="quantity" name="quantity[]" value="<?php echo isset($row2->quantity) ? $row2->quantity : 1; ?>" min="1">
<input type="hidden" name="discount_code[]" value="<?php echo isset($row2->discount_code) ? $row2->discount_code : ''; ?>">
<input type="hidden" name="discount_special[]" value="<?php echo isset($row2->discount_special) ? $row2->discount_special : ''; ?>">
<input type="hidden" name="mobile" value="<?php echo isset($mobile) ? $mobile : ''; ?>">
<input type="hidden" name="track_number" value="<?php echo isset($track_number) ? $track_number : ''; ?>">


    <strong> 
    <a class="category_name" href="product_individual.php?id=<?php echo$row2->id?>&ref=<?php echo$ref?>" data-category="Food"><?php echo$row2->product_name;?></a>    
    </strong>    
  </div>

  <a href="product_individual.php?id=<?php echo$row2->id?>&ref=<?php echo$ref?>" data-category="Food">
  <img class="category_image" src='<?php echo"category/$row2->category_picture";?>'>  
  </a>

<!--   <div>
    <a class="aro" style="color: blue;" href="product_individual.php?id=<?php echo$row2->id?>&ref=<?php echo$ref?>" data-category="Food"><?php echo "বিস্তারিত";?></a>
  </div> -->
    
   

  </div>

  </div>
  <?php }?>
</div>
  <!-- ------------------------------------ -->
<center><div style="width: 100%; height: 20pt; padding: 5pt; box-sizing: border-box; background-color: #699C91;">অন্যান্য</div><hr>
</center>


<div style="width: 85%; margin-left: auto; margin-right: auto;" class="catagory_display_container">
  <?php 


 
$result2 = mysqli_query($conn,"SELECT item, MIN(category_name) as category_name, MIN(category_picture) as category_picture 
FROM goods_category
GROUP BY item; ") or die(mysqli_error($conn));


    while ($row2 = mysqli_fetch_object($result2) ) {?>  
  <div class="catagory_display">
  <div class="catagory_display_inner" style="background-color: #FDE8C0; padding: 5pt; box-sizing: border-box;  display: flex; flex-direction: column;"  >
  <div>  


<div class="add">Add</div> <!-- id="add" বাদ, class="add" রাখুন -->
  <div class="go"> 
  <a href="order_edit.php?track_number=<?php echo$track_number?>"><-----Go-----></a>
  </div>

<input type="hidden" name="id[]" value="<?php echo isset($row2->id) ? $row2->id : ''; ?>">
<input type="hidden" name="category_picture[]" value="<?php echo isset($row2->category_picture) ? $row2->category_picture : ''; ?>">
<input type="hidden" name="product_name[]" value="<?php echo isset($row2->product_name) ? $row2->product_name : ''; ?>">
<input type="hidden" name="item[]" value="<?php echo isset($row2->item) ? $row2->item : ''; ?>">
<input type="hidden" name="person[]" value="<?php echo isset($row2->person) ? $row2->person : ''; ?>">
<input type="hidden" name="gks[]" value="<?php echo isset($row2->gks) ? $row2->gks : ''; ?>">
<input type="hidden" name="seller[]" value="<?php echo isset($row2->seller) ? $row2->seller : ''; ?>">
<input type="hidden" name="price[]" value="<?php echo isset($row2->price) ? $row2->price : ''; ?>">
<input type="hidden" name="discount[]" value="<?php echo isset($row2->discount) ? $row2->discount : ''; ?>">
<input type="hidden" name="current_price[]" value="<?php echo isset($row2->current_price) ? $row2->current_price : ''; ?>">
<input type="hidden" name="size[]" value="<?php echo isset($row2->size) ? $row2->size : ''; ?>">
<input type="hidden" name="weight[]" value="<?php echo isset($row2->weight) ? $row2->weight : ''; ?>">
<input type="number" class="quantity" name="quantity[]" value="<?php echo isset($row2->quantity) ? $row2->quantity : 1; ?>" min="1">
<input type="hidden" name="discount_code[]" value="<?php echo isset($row2->discount_code) ? $row2->discount_code : ''; ?>">
<input type="hidden" name="discount_special[]" value="<?php echo isset($row2->discount_special) ? $row2->discount_special : ''; ?>">
<input type="hidden" name="mobile" value="<?php echo isset($mobile) ? $mobile : ''; ?>">
<input type="hidden" name="track_number" value="<?php echo isset($track_number) ? $track_number : ''; ?>">


    <strong>  
    <a class="category_name" href="#" data-category="Food"><?php echo$row2->category_name;?></a>    
    </strong>    
  </div>

  <a href="category_as_individual.php?catagory=<?php echo$row2->category_name?>&item=<?php echo$row2->item?>&ref=<?php echo$ref?>" data-category="Food">
  <img class="category_image" src='<?php echo"category/$row2->category_picture";?>'>  
  </a>

  <div>
        <a class="aro" style="color: blue;" href="category_as_individual.php?catagory=<?php echo$row2->category_name?>&item=<?php echo$row2->item?>&ref=<?php echo$ref?>&track_number=<?php echo$track_number?>&mobile=<?php echo$mobile?>" data-category="Food"><?php echo "আরও-->>";?></a>
  </div>
    

   

  </div>

  </div>
  <?php }?>

</div>




<script>
document.addEventListener("DOMContentLoaded", function() {
    document.querySelectorAll(".add").forEach(button => {
        button.addEventListener("click", function() {
            let container = button.closest(".catagory_display_inner");
            let formData = new FormData();
            let allValid = true;

            // Collect all inputs within the specific container
            container.querySelectorAll("input").forEach(el => {
                // Check if the input is a quantity field and its value is negative
                if (el.name === 'quantity[]' && parseInt(el.value, 10) < 0) {
                    allValid = false;
                    alert("Quantity can't be a negative number.");
                    return; // Exit the loop
                }
                
                if (el.name) {
                    formData.append(el.name, el.value);
                }
            });

            if (!allValid) {
                return; // Stop the function if validation fails
            }
            
            // Get mobile and track_number
            let mobileInput = document.querySelector("input[name='mobile']");
            let trackNumberInput = document.querySelector("input[name='track_number']");
            
            if (mobileInput) {
                formData.append("mobile", mobileInput.value);
            }
            if (trackNumberInput) {
                formData.append("track_number", trackNumberInput.value);
            }

            fetch("insert_final_order_item.php", {
                method: "POST",
                body: formData
            })
            .then(res => res.text())
            .then(data => {
                alert(data);
            })
            .catch(err => console.error(err));
        });
    });
});
</script>




<?php
// End capturing the output and store it in a variable
$catagory_display_content = ob_get_clean();

// Now include frameForAll.php, which will use $catagory_display_content
include("frameForAll.php");

// mysqli_close($conn); // You might want to close the connection after all database operations
ob_end_flush();
?>



<!DOCTYPE html>
<html>
<head>

  <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

<meta name="theme-color" content="#FF0000">
    
<title>অনলাইন শপ</title>
    

  
</head>
<body>


<style type="text/css">




</style>


</body>
</html>