diff --git a/src/components/dashboard/Officer_EventManagement.astro b/src/components/dashboard/Officer_EventManagement.astro index 9856fcf..914b27e 100644 --- a/src/components/dashboard/Officer_EventManagement.astro +++ b/src/components/dashboard/Officer_EventManagement.astro @@ -1745,4 +1745,130 @@ declare global { // Initial fetch fetchEvents(); + + // Add openEditModal function + window.openEditModal = async function (event?: any) { + const modal = document.getElementById( + "editEventModal" + ) as HTMLDialogElement; + const form = document.getElementById( + "editEventForm" + ) as HTMLFormElement; + const modalTitle = document.getElementById("editModalTitle"); + const currentFiles = document.getElementById("currentFiles"); + const newFiles = document.getElementById("newFiles"); + + if (!modal || !form) return; + + try { + // Clear previous form data + form.reset(); + if (currentFiles) currentFiles.innerHTML = ""; + if (newFiles) newFiles.innerHTML = ""; + + // Set modal title based on whether we're editing or creating + if (modalTitle) { + modalTitle.textContent = event ? "Edit Event" : "Add New Event"; + } + + if (event) { + // Fetch fresh data from PocketBase for the event + const freshEventData = await get.getOne( + "events", + event.id + ); + + // Populate form with fresh data + const idInput = document.getElementById( + "editEventId" + ) as HTMLInputElement; + const nameInput = document.getElementById( + "editEventName" + ) as HTMLInputElement; + const codeInput = document.getElementById( + "editEventCode" + ) as HTMLInputElement; + const locationInput = document.getElementById( + "editEventLocation" + ) as HTMLInputElement; + const pointsInput = document.getElementById( + "editEventPoints" + ) as HTMLInputElement; + const startDateInput = document.getElementById( + "editEventStartDate" + ) as HTMLInputElement; + const endDateInput = document.getElementById( + "editEventEndDate" + ) as HTMLInputElement; + const descriptionInput = document.getElementById( + "editEventDescription" + ) as HTMLTextAreaElement; + const publishedInput = document.getElementById( + "editEventPublished" + ) as HTMLInputElement; + const hasFoodInput = document.getElementById( + "editEventHasFood" + ) as HTMLInputElement; + + if (idInput) idInput.value = freshEventData.id; + if (nameInput) nameInput.value = freshEventData.event_name; + if (codeInput) codeInput.value = freshEventData.event_code; + if (locationInput) + locationInput.value = freshEventData.location; + if (pointsInput) + pointsInput.value = + freshEventData.points_to_reward.toString(); + if (startDateInput) + startDateInput.value = new Date(freshEventData.start_date) + .toISOString() + .slice(0, 16); + if (endDateInput) + endDateInput.value = new Date(freshEventData.end_date) + .toISOString() + .slice(0, 16); + if (descriptionInput) + descriptionInput.value = freshEventData.event_description; + if (publishedInput) + publishedInput.checked = freshEventData.published; + if (hasFoodInput) + hasFoodInput.checked = freshEventData.has_food; + + // Display current files if any + if ( + currentFiles && + freshEventData.files && + freshEventData.files.length > 0 + ) { + currentFiles.innerHTML = freshEventData.files + .map( + (filename) => ` +
+ ${filename} +
+ + +
+
+ ` + ) + .join(""); + } + } + + // Show the modal + modal.showModal(); + } catch (error) { + console.error("Failed to open edit modal:", error); + // Show error toast or alert + } + };