38960-vm/api/patients.php
2026-03-21 09:33:13 +00:00

35 lines
1.1 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
header('Content-Type: application/json');
$action = $_GET['action'] ?? '';
$pdo = db();
try {
switch ($action) {
case 'search':
$q = $_GET['q'] ?? '';
// Allow empty search to return nothing or some default?
// Select2 usually sends a query.
if (empty($q)) {
echo json_encode([]);
exit;
}
// Search by name or phone
$sql = "SELECT id, name_en as name, phone FROM patients WHERE name_en LIKE ? OR name_ar LIKE ? OR phone LIKE ? LIMIT 20";
$stmt = $pdo->prepare($sql);
$term = "%$q%";
$stmt->execute([$term, $term, $term]);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($results);
break;
default:
http_response_code(400);
echo json_encode(['error' => 'Invalid action']);
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}