<?php
require_once "../config/db.php";
require_once "../config/helpers.php";

$search = trim($_GET['search'] ?? '');

$where = "";

if($search){
    $search = $conn->real_escape_string($search);
    $where = "WHERE name LIKE '%$search%' OR email LIKE '%$search%' OR phone LIKE '%$search%'";
}

$query = "SELECT * FROM customers $where ORDER BY id DESC LIMIT 50";
$result = $conn->query($query);

if($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= e($row['name']) ?></td>
<td><?= e($row['email']) ?></td>
<td><?= e($row['phone']) ?></td>
<td><?= e($row['address']) ?></td>

<td>
<a href="edit_customer.php?id=<?= $row['id'] ?>" class="btn btn-warning btn-sm">Edit</a>

<a href="../actions/delete_customer.php?id=<?= $row['id'] ?>" 
class="btn btn-danger btn-sm"
onclick="return confirm('Delete?')">Delete</a>
</td>
</tr>
<?php
    }
} else {
    echo "<tr><td colspan='6' class='text-center'>No data found</td></tr>";
}