<?php require "../config.php";?>
<!DOCTYPE html>
<html>
   <head>
      
      <?php require "inc/head.php";?>

      <title>Products</title>
      <script type="text/javascript">
$(document).ready(function() {
   $(".edit_price").click(function(){
 var row = $(this).closest("tr"); // Find the closest <tr> to the clicked button
 var p = row.find(".new_price").val();
 var max = row.find(".have_max_price").attr("title");
 var id=row.attr("title");
 $.post("back/login.php",{action:"update_price",p:p,id:id,max:max},function (res) {
     if(res==1){
        alert("Updated");
     }
     else{
        alert(res);
     }
 });
          });
});
         
      </script>
   </head>
   <body>
      <?php require "inc/nav.php";?>
      <div class="flex_">
         <?php require "inc/sidebar.php";?>
         <div class="right">
          <div class="padding">
              <h3 class="page-title">All Products</h3>
                  
<br>
<!-- Your HTML and PHP code -->
<!-- Assuming you have your database connection established and session started -->

<!-- Input field for search -->
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." class="form-control">

<!-- Table to display data -->
<table class="table table-striped padding" id="tab">
    <thead>
        <tr>
            <th>ID</th>
            <th>Product</th><th>Available Stock</th>
            <th>Price</th>
            <th>Discount</th>
           
            <th>Reviews</th>
            
           
            <th>Disable</th>
            <th>Stock</th>
        </tr>
    </thead>
    <tbody>
        <?php
        // Retrieve data from the database
        $shop_id = $_SESSION['shop_id'];
        $sql = "SELECT * FROM `item` WHERE shop_id='$shop_id' and disable='0' order by id desc";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $id = $row['id'];
                $name = $row['name'];
                $price = $row['price'];
                $reviews = htmlentities($row['reviews']);
                $star = $row['star'];
                $discount = $row['discount'];
                $max_price = $row['max_price'];
                $sto_=$row['stock'];$num=$row['num'];
                if($sto_=='0'){
                    $td_="<td><button class='btn btn-warning in_' title='".$id."'>Out Of Stock</button></td>";
                }else{
                    $td_="<td><button class='btn btn-success out_' title='".$id."'>In Stock</button></td>";
                }

                echo "
                    <tr title='" . $id . "'>
                        <td class='have_max_price' title='".$max_price."'>" . $id . "</td>
                        <td class='have_discount' title='".$discount."'><a href='single.php?id={$id}'>" . $name . "</a></td><td>" . $num . "</td>
                        <td>" . $max_price . "</td>
                        <td>" . $discount . "</td>
                        <!--<td><input class='form-control new_price' style='width:80px!important;' type='text' value='" . $price . "'></td>-->
                        <td>" . $reviews . "</td>
                       <!-- <td>" . $star . "</td>-->
                       <!-- <td><button class='btn btn-warning edit_price' title='".$id."'>Edit</button></td>-->
                        <td><button class='btn btn-danger disable_product' title='".$id."'>Disable</button></td>".$td_."
                         
                        
                    </tr>
                ";
            }
        } else {
            echo "<tr><td colspan='9'>0 results</td></tr>";
        }
        ?>
    </tbody>
</table>

<script>
function myFunction() {
    var input, filter, table, tr, td, i, txtValue;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    table = document.getElementById("tab");
    tr = table.getElementsByTagName("tr");

    for (i = 0; i < tr.length; i++) {
        td = tr[i].getElementsByTagName("td")[1]; // Get the second column (Food Name)
        if (td) {
            txtValue = td.textContent || td.innerText;
            if (txtValue.toUpperCase().indexOf(filter) > -1) {
                tr[i].style.display = "";
            } else {
                tr[i].style.display = "none";
            }
        }
    }
}
</script>

            </div>
         </div>
      </div>
      </div>
   </body>
</html>