exec(" CREATE TABLE IF NOT EXISTS `oncall_days` ( `day_of_week` int(11) NOT NULL, `staff_id` int(11) NOT NULL, PRIMARY KEY (`day_of_week`), CONSTRAINT `fk_oncall_staff` FOREIGN KEY (`staff_id`) REFERENCES `staff` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; "); } catch (PDOException $e) {} switch ($method) { case 'GET': try { $stmt = $pdo->query("SELECT day_of_week, staff_id FROM oncall_days"); $data = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); // returns [day_of_week => staff_id] // If empty, return defaults 1->1, 2->2... if (empty($data)) { $data = [1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5]; } echo json_encode(['status' => 'success', 'data' => $data]); } catch (PDOException $e) { http_response_code(500); echo json_encode(['status' => 'error', 'message' => $e->getMessage()]); } break; case 'POST': $input = json_decode(file_get_contents('php://input'), true); if (!isset($input['day_of_week']) || !isset($input['staff_id'])) { http_response_code(400); echo json_encode(['status' => 'error', 'message' => 'Missing required fields']); exit; } try { $stmt = $pdo->prepare(" INSERT INTO oncall_days (day_of_week, staff_id) VALUES (:day_of_week, :staff_id) ON DUPLICATE KEY UPDATE staff_id = :staff_id_update "); $stmt->execute([ ':day_of_week' => $input['day_of_week'], ':staff_id' => $input['staff_id'], ':staff_id_update' => $input['staff_id'] ]); echo json_encode(['status' => 'success', 'message' => 'On-call updated successfully']); } catch (PDOException $e) { http_response_code(500); echo json_encode(['status' => 'error', 'message' => $e->getMessage()]); } break; default: http_response_code(405); echo json_encode(['status' => 'error', 'message' => 'Method not allowed']); break; } ?>