<?php
include('../includes/config.php');

$q = trim($_GET['q']);
if (strlen($q) < 2) exit;

$stmt = $conn->prepare("
    SELECT id, full_name, phone, email, loyalty_points 
    FROM customers 
    WHERE full_name LIKE CONCAT('%', ?, '%') 
       OR email LIKE CONCAT('%', ?, '%') 
       OR phone LIKE CONCAT('%', ?, '%')
    LIMIT 10
");
$stmt->bind_param("sss", $q, $q, $q);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    echo "<p>No customers found.</p>";
} else {
    while ($row = $result->fetch_assoc()) {
        echo "<div class='glass-card p-2 mb-2 border'>
                <strong>{$row['full_name']}</strong><br>
                📞 {$row['phone']} | ✉️ {$row['email']}<br>
                ⭐ Loyalty Points: {$row['loyalty_points']}
                <br><button class='btn btn-sm btn-success mt-2' onclick='selectCustomer(" . json_encode($row) . ")'>Select</button>
              </div>";
    }
}
?>
