37863-vm/api/clients.php
2026-01-27 14:35:19 +00:00

44 lines
1.6 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
$method = $_SERVER['REQUEST_METHOD'];
try {
$pdo = db();
if ($method === 'GET') {
$stmt = $pdo->query("SELECT * FROM clients ORDER BY created_at DESC");
echo json_encode($stmt->fetchAll());
} elseif ($method === 'POST') {
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['action']) && $data['action'] === 'save_client') {
$stmt = $pdo->prepare("INSERT INTO clients (name, job, age_range, hobbies) VALUES (?, ?, ?, ?)");
$stmt->execute([
$data['name'],
$data['job'] ?? null,
$data['age_range'] ?? null,
$data['hobbies'] ?? null
]);
echo json_encode(['success' => true, 'id' => $pdo->lastInsertId()]);
} elseif (isset($data['action']) && $data['action'] === 'log_interaction') {
$stmt = $pdo->prepare("INSERT INTO interactions (client_id, day_number, notes) VALUES (?, ?, ?)");
$stmt->execute([
$data['client_id'],
$data['day_number'],
$data['notes']
]);
// Update current day for client
$stmt = $pdo->prepare("UPDATE clients SET current_day = ? WHERE id = ?");
$stmt->execute([$data['day_number'], $data['client_id']]);
echo json_encode(['success' => true]);
}
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}