query("SELECT points_for_free_meal FROM loyalty_settings WHERE id = 1"); $settings = $settingsStmt->fetch(PDO::FETCH_ASSOC); $threshold = $settings ? $settings['points_for_free_meal'] : 70; $stmt = $pdo->prepare("SELECT id, name, phone, email, points FROM customers WHERE name LIKE ? OR phone LIKE ? LIMIT 10"); $searchTerm = "%$q%"; $stmt->execute([$searchTerm, $searchTerm]); $customers = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($customers as &$customer) { $customer['points'] = intval($customer['points']); $customer['eligible_for_free_meal'] = $customer['points'] >= $threshold; $customer['points_needed'] = max(0, $threshold - $customer['points']); $customer['threshold'] = $threshold; } echo json_encode($customers); } catch (Exception $e) { error_log("Customer Search Error: " . $e->getMessage()); echo json_encode(['error' => 'Database error']); }