prepare("SELECT * FROM appointments WHERE id = ?"); $stmt->execute([$id]); $appointment = $stmt->fetch(PDO::FETCH_ASSOC); echo json_encode($appointment); exit; } $startStr = $_GET['start'] ?? null; $endStr = $_GET['end'] ?? null; $doctor_id = $_GET['doctor_id'] ?? null; $events = []; $businessHours = []; // Fetch Appointments $query = " SELECT a.id, a.start_time as start, a.end_time as end, a.reason as title, a.status, a.patient_id, a.doctor_id, p.name as patient_name, d.name_$lang as doctor_name FROM appointments a JOIN patients p ON a.patient_id = p.id JOIN doctors d ON a.doctor_id = d.id WHERE 1=1"; $params = []; if ($startStr) { $query .= " AND a.start_time >= ?"; $params[] = $startStr; } if ($endStr) { $query .= " AND a.start_time <= ?"; $params[] = $endStr; } if ($doctor_id) { $query .= " AND a.doctor_id = ?"; $params[] = $doctor_id; } $stmt = $db->prepare($query); $stmt->execute($params); $appointments = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($appointments as $a) { $color = '#0d6efd'; // blue if ($a['status'] === 'Completed') $color = '#198754'; // green if ($a['status'] === 'Cancelled') $color = '#dc3545'; // red $events[] = [ 'id' => $a['id'], 'title' => $a['patient_name'] . ' (' . $a['doctor_name'] . ')', 'start' => $a['start'], 'end' => $a['end'], 'color' => $color, 'extendedProps' => [ 'type' => 'appointment', 'patient_id' => $a['patient_id'], 'doctor_id' => $a['doctor_id'], 'patient_name' => $a['patient_name'], 'doctor_name' => $a['doctor_name'], 'status' => $a['status'], 'reason' => $a['reason'] ] ]; } // Fetch Holidays $holidayQuery = "SELECT holiday_date as start, name_$lang as title FROM holidays WHERE 1=1"; $holidayParams = []; if ($startStr) { $holidayQuery .= " AND holiday_date >= ?"; $holidayParams[] = date('Y-m-d', strtotime($startStr)); } if ($endStr) { $holidayQuery .= " AND holiday_date <= ?"; $holidayParams[] = date('Y-m-d', strtotime($endStr)); } $stmt = $db->prepare($holidayQuery); $stmt->execute($holidayParams); $holidays = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($holidays as $h) { $events[] = [ 'id' => 'hol_' . $h['start'], 'title' => 'Holiday: ' . $h['title'], 'start' => $h['start'], 'allDay' => true, 'color' => '#ffc107', // yellow 'textColor' => '#000', 'display' => 'background', 'extendedProps' => ['type' => 'holiday'] ]; $events[] = [ 'title' => $h['title'], 'start' => $h['start'], 'allDay' => true, 'color' => '#ffc107', 'textColor' => '#000', 'extendedProps' => ['type' => 'holiday'] ]; } // Fetch Doctor Business Hours if ($doctor_id) { $scheduleStmt = $db->prepare("SELECT day_of_week as day, start_time as start, end_time as end FROM doctor_schedules WHERE doctor_id = ?"); $scheduleStmt->execute([$doctor_id]); $schedules = $scheduleStmt->fetchAll(PDO::FETCH_ASSOC); $bhMap = []; foreach ($schedules as $s) { $key = $s['start'] . '-' . $s['end']; if (!isset($bhMap[$key])) { $bhMap[$key] = [ 'daysOfWeek' => [], 'startTime' => $s['start'], 'endTime' => $s['end'] ]; } $bhMap[$key]['daysOfWeek'][] = (int)$s['day']; } $businessHours = array_values($bhMap); } else { $businessHours = [ [ 'daysOfWeek' => [0, 1, 2, 3, 4, 5, 6], 'startTime' => '08:00', 'endTime' => '17:00' ] ]; } echo json_encode([ 'events' => $events, 'businessHours' => $businessHours ]); exit; } if ($method === 'POST') { $input = json_decode(file_get_contents('php://input'), true) ?? $_POST; $action = $input['action'] ?? ''; if ($action === 'create') { $patient_id = $input['patient_id'] ?? ''; $doctor_id = $input['doctor_id'] ?? ''; $start_time = $input['start_time'] ?? ''; $reason = $input['reason'] ?? ''; if ($patient_id && $doctor_id && $start_time) { $stmt = $db->prepare("INSERT INTO appointments (patient_id, doctor_id, start_time, end_time, reason) VALUES (?, ?, ?, DATE_ADD(?, INTERVAL 30 MINUTE), ?)"); $stmt->execute([$patient_id, $doctor_id, $start_time, $start_time, $reason]); echo json_encode(['success' => true, 'id' => $db->lastInsertId()]); } else { echo json_encode(['success' => false, 'error' => 'Missing fields']); } } elseif ($action === 'update') { $id = $input['id'] ?? ''; $patient_id = $input['patient_id'] ?? ''; $doctor_id = $input['doctor_id'] ?? ''; $start_time = $input['start_time'] ?? ''; $status = $input['status'] ?? 'Scheduled'; $reason = $input['reason'] ?? ''; if ($id && $patient_id && $doctor_id && $start_time) { $stmt = $db->prepare("UPDATE appointments SET patient_id = ?, doctor_id = ?, start_time = ?, end_time = DATE_ADD(?, INTERVAL 30 MINUTE), status = ?, reason = ? WHERE id = ?"); $stmt->execute([$patient_id, $doctor_id, $start_time, $start_time, $status, $reason, $id]); echo json_encode(['success' => true]); } else { echo json_encode(['success' => false, 'error' => 'Missing fields']); } } elseif ($action === 'delete') { $id = $input['id'] ?? ''; if ($id) { $stmt = $db->prepare("DELETE FROM appointments WHERE id = ?"); $stmt->execute([$id]); echo json_encode(['success' => true]); } else { echo json_encode(['success' => false, 'error' => 'Missing ID']); } } exit; }