export default { async openBookingWidget() { const calendarModel = Custom5.model || {}; const selection = calendarModel.calendarSelection || null; if (!selection) { showAlert("Bitte zuerst einen Tag oder Zeitraum auswählen.", "warning"); return; } const pad = (value) => String(value).padStart(2, "0"); const parseLocalDateTime = (value) => { if (!value) return null; const text = String(value).trim(); const match = text.match( /^(\d{4})-(\d{2})-(\d{2})(?:T|\s)?(\d{2})?:?(\d{2})?:?(\d{2})?$/ ); if (!match) return null; const date = new Date( Number(match[1]), Number(match[2]) - 1, Number(match[3]), Number(match[4] || 0), Number(match[5] || 0), Number(match[6] || 0) ); return Number.isNaN(date.getTime()) ? null : date; }; const formatDate = (date) => [ date.getFullYear(), pad(date.getMonth() + 1), pad(date.getDate()) ].join("-"); const formatTime = (date) => [ pad(date.getHours()), pad(date.getMinutes()) ].join(":"); const formatDateTimeForSql = (date) => `${formatDate(date)} ${formatTime(date)}:00`; const start = parseLocalDateTime(selection.start); const end = parseLocalDateTime(selection.end); if (!start || !end || end <= start) { showAlert( "Die Kalenderauswahl enthält kein gültiges Datum oder Zeitintervall.", "error" ); return; } const seatModel = Custom4.model || {}; const seatId = String( seatModel.seatId || seatModel.selectedSeat?.id || appsmith.store.selectedSeatId || "" ).trim(); const seatName = String( seatModel.seatName || seatModel.selectedSeat?.name || appsmith.store.selectedSeatName || "" ).trim(); if (!seatId) { showAlert("Es wurde kein Sitzplatz ausgewählt.", "error"); return; } const bookedBy = appsmith.user?.email || appsmith.user?.username || null; if (!bookedBy) { showAlert( "Der angemeldete Benutzer konnte nicht ermittelt werden.", "error" ); return; } const isTimeSelection = selection.type === "time" && selection.allDay !== true; const isAllDay = !isTimeSelection; let startDate; let endDate; let startTime = null; let endTime = null; let startDateTime; let endDateTime; if (isAllDay) { const startOfDay = new Date(start); startOfDay.setHours(0, 0, 0, 0); const lastSelectedDay = new Date(end); // FullCalendar liefert bei Ganztagsauswahl ein exklusives Ende. // Nur wenn end tatsächlich 00:00 Uhr ist, wird ein Tag abgezogen. const endIsMidnight = lastSelectedDay.getHours() === 0 && lastSelectedDay.getMinutes() === 0 && lastSelectedDay.getSeconds() === 0; if (endIsMidnight) { lastSelectedDay.setDate( lastSelectedDay.getDate() - 1 ); } lastSelectedDay.setHours(0, 0, 0, 0); startDate = formatDate(startOfDay); endDate = formatDate(lastSelectedDay); startDateTime = `${startDate} 00:00:00`; endDateTime = `${endDate} 23:59:59`; } else { // Bei einem Timeslot bleiben Start- und Enddatum gleich. startDate = formatDate(start); endDate = formatDate(start); startTime = formatTime(start); endTime = formatTime(end); startDateTime = formatDateTimeForSql(start); endDateTime = formatDateTimeForSql(end); } const recurring = typeof RecurringCheckbox !== "undefined" && RecurringCheckbox.isChecked === true; const recurringWeeks = typeof RecurringWeeksSelect !== "undefined" ? Number(RecurringWeeksSelect.selectedOptionValue || 1) : 1; const recurringDays = appsmith.store.recurringDays || {}; const bookingFormData = { ...(appsmith.store.bookingFormData || {}), seatId, seatName, bookedBy, title: "Platzbuchung", type: selection.type || null, startDate, endDate, startTime, endTime, startDateTime, endDateTime, allDay: isAllDay, recurring, recurringWeeks, recurringDays }; try { await storeValue("bookingFormData", bookingFormData, false); await showModal("Platzbuchung"); } catch (error) { console.error( "Buchungsdaten konnten nicht gespeichert werden:", error ); showAlert( "Die Buchungsdaten konnten nicht gespeichert werden.", "error" ); } } };