import { get, post } from "../../apiUtil"; import { API_URL } from "../../constants"; // Fetch events for calendar view (with date range) export async function fetchEventsForCalendar(start, end) { const params = new URLSearchParams(); if (start) params.append("start", start.toISOString()); if (end) params.append("end", end.toISOString()); const response = await get(`${API_URL}/Calendar/GetEventsForCalendar?${params.toString()}`); return response?.data || response?.Data || []; } // Fetch events with pagination export async function fetchEventsPaged({ search = "", page = 1, pageSize = 12 }) { const params = new URLSearchParams(); if (search) params.append("search", search); params.append("page", page.toString()); params.append("pageSize", pageSize.toString()); const response = await get(`${API_URL}/Calendar/GetEventList?${params.toString()}`); const data = response?.data || response?.Data || {}; return { rows: data.rows || [], totalCount: data.totalCount || 0, }; } // Get single event by ID export async function getEventById(id) { const response = await get(`${API_URL}/Calendar/GetEventById/${id}`); const event = response?.data || response?.Data; // Handle both camelCase and PascalCase if (event) { return { id: event.id || event.Id, title: event.title || event.Title || "", description: event.description || event.Description || "", location: event.location || event.Location || "", recordID: event.recordID || event.RecordID || null, recordName: event.recordName || event.RecordName || "", recordTargetType: event.recordTargetType || event.RecordTargetType || "", reminderMinutes: event.reminderMinutes || event.ReminderMinutes || 15, eventColor: event.eventColor || event.EventColor || "primary", startDate: event.startDate || event.StartDate || "", startTime: event.startTime || event.StartTime || "", endDate: event.endDate || event.EndDate || "", endTime: event.endTime || event.EndTime || "", allDay: event.allDay || event.AllDay || false, }; } return null; } // Create new event export async function addEvent(eventData) { const response = await post(`${API_URL}/Calendar/AddEvent`, { Title: eventData.title, Description: eventData.description, Location: eventData.location, RecordID: eventData.recordID, RecordName: eventData.recordName, RecordTargetType: eventData.recordTargetType, ReminderMinutes: eventData.reminderMinutes, EventColor: eventData.eventColor, StartDate: eventData.startDate, StartTime: eventData.startTime, EndDate: eventData.endDate, EndTime: eventData.endTime, AllDay: eventData.allDay, }); return response; } // Update existing event export async function editEvent(eventData) { const response = await post(`${API_URL}/Calendar/EditEvent`, { Id: eventData.id, Title: eventData.title, Description: eventData.description, Location: eventData.location, RecordID: eventData.recordID, RecordName: eventData.recordName, RecordTargetType: eventData.recordTargetType, ReminderMinutes: eventData.reminderMinutes, EventColor: eventData.eventColor, StartDate: eventData.startDate, StartTime: eventData.startTime, EndDate: eventData.endDate, EndTime: eventData.endTime, AllDay: eventData.allDay, }); return response; } // Delete event export async function deleteEvent(id) { const response = await fetch(`${API_URL}/Calendar/DeleteEvent/${id}`, { method: "DELETE", headers: { "Content-Type": "application/json", Authorization: `Bearer ${localStorage.getItem("token")}`, }, }); if (!response.ok) { throw new Error("Failed to delete event"); } return response.json(); }