<?php
include "db.php";

$search = $_GET['search'] ?? '';
$brand  = $_GET['brand'] ?? '';
$cat    = $_GET['category'] ?? '';

$where = "1";

if($search != ''){
    $s = mysqli_real_escape_string($conn,$search);
    $where .= " AND product_name LIKE '%$s%'";
}

if($brand != ''){
    $b = mysqli_real_escape_string($conn,$brand);
    $where .= " AND brand='$b'";
}

if($cat != ''){
    $c = mysqli_real_escape_string($conn,$cat);
    $where .= " AND categories LIKE '%$c%'";
}

$q = mysqli_query($conn,"SELECT * FROM products WHERE $where ORDER BY position ASC");

while($p=mysqli_fetch_assoc($q)){
?>
<tr id="row_<?= $p['id'] ?>">
    <td><?= $p['id'] ?></td>
    <td><?= $p['product_name'] ?></td>
    <td><?= $p['brand'] ?></td>
    <td><?= str_replace('|',' | ',$p['categories']) ?></td>
    <td><?= str_replace('|',' | ',$p['detection_systems']) ?></td>
    <td><?= str_replace('|',' | ',$p['tags']) ?></td>
    <td>₹ <?= $p['price'] ?></td>
    <td>
        <a href="?edit=<?= $p['id'] ?>" class="btn btn-sm btn-warning">Edit</a>
        <a href="?delete=<?= $p['id'] ?>" class="btn btn-sm btn-danger"
           onclick="return confirm('Delete this product?')">Delete</a>
    </td>
</tr>
<?php } ?>
