<?php include ("inc/config.php");
 
if(isset($_REQUEST["term"])){  $sql = "SELECT * FROM `products` where name LIKE ?";
    
if($stmt = $mysqli->prepare($sql)){
 // Bind variables to the prepared statement as parameters
 $stmt->bind_param("s", $param_term);
 // Set parameters
 $param_term = $_REQUEST["term"] . '%' ;
 // Attempt to execute the prepared statement
 if($stmt->execute()){
   $result = $stmt->get_result();
   // Check number of rows in the result set
 if($result->num_rows > 0){

echo "<table >";
echo "<thead>";
echo "<tr class='t2'>";
echo "<th>#</th>";
echo "<th>Product Image</th>";
echo "<th>Category</th>";
echo "<th>Name</th>";
echo "<th>Size</th>";
echo " <th>Status</th>";
echo"  <th>Quantity</th>";
echo"  <th>Price </th>";
echo"  <th>Offer Price</th>";
echo"  <th>Product Description</th>";
echo"<th>Recorded by</th>";
echo"  <th>Time</th>";
echo " <th>Edit record</th>";
echo " <th>Offer Update</th>";
echo "  <th>Add Stock</th>";
echo " <th>Remove Record</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";

 // Fetch result rows as an associative array
 while($row = $result->fetch_array(MYSQLI_ASSOC)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td><img class='product-image' src='../product-images/".$row['image']."' /></td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['size'] . "</td>";
?>

<td><?php   $a = $row['quantity']; if ($a == 0) { echo "<a class='stock-out'>stock out</a>"; } else{ echo " <a class='low-stock'> Low </a> ";} ?></td>

<?php 
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>KSH. " . $row['price'] ."/= </td>";
?>
<td>
<?php  $a = $row['price_1'];;  

if ($a > 0) { echo "<a class='offer'> $a/=  </a> ";}
 else { echo "<a>  -  </a> ";}

?>  
</td>

<?php
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['session'] . "</td>";
echo "<td style='color:#848482; font-weight: bold;'>" . date_format( new DateTime ($row['time'] ),"d M, Y H: m: s"). "</td>";
echo "<td><button class='btn-edit'><a href='edit-products.php?edit=". $row['id'] ."' title='Edit record' data-toggle='tooltip'>Edit</a></button>"."</td>";

echo "<td><button class='btn-offer'><a href='product-offers.php?edit=". $row['id'] ."' title='Edit record' data-toggle='tooltip'>Update</a></button>"."</td>";

echo "<td><button class='btn-edit'><a href='add-add-products.php?edit=". $row['id'] ."' title='Edit record' data-toggle='tooltip'>Add</a></button>"."</td>";

echo "<td><button class='btn-delete'><a href='del-products.php?id=". $row['id'] ."' title='delete Record' data-toggle='tooltip'>Remove</a></button>" ."</td>";

echo "</tr>";
  
  }

  echo "</table>";
      } else{
                echo "<p class='no-results'>No matches found</p>";
            }
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }
    }
     
    // Close statement
    $stmt->close();
}
 
// Close connection
$mysqli->close();
?>