class ReservationViewController extends ViewController { viewDidAppear() { ReservationManager.loadReservations(this.onReservationsLoaded.bind(this)); } onReservationsLoaded() { if (this.reservationButton) { this.reservationButton.disabled = false; } } onBackButtonPressed(event) { this.dismissModally(); } onInputFieldChanged(event) { this.validateFields(); } onReserveButtonPressed(event) { this.validateFields(); if (!this.allValid) { alert("Bitte schau noch mal alle Eingabefelder durch. Es gibt Fehleingaben."); return; } //Get the dates. var fromDate = new Date(this.startDateField.value); var toDate = new Date(this.returnDateField.value); //Gather the data and send the reservation request. ReservationManager.performReservation({ name: this.nameField.value, email: this.emailField.value, destination: this.destinationField.value, description: this.descriptionField.value, from_time: fromDate.getTime() + fromDate.getTimezoneOffset() * 60 * 1000, to_time: toDate.getTime() + toDate.getTimezoneOffset() * 60 * 1000, code: localStorage.getItem("code") }, this.onReservationSucceeded.bind(this), this.onReservationFailed.bind(this)); } onReservationSucceeded(data) { this.presentModalViewController(UIKit.getViewControllerById("success-view-controller")); this.clearFields(); } onReservationFailed(request, status, error) { alert("Die Reservation konnte nicht erfasst werden. Melde diese Fehlermeldung, damit wir das Problem lösen können:\r\n\r\n" + error); } clearFields() { var fieldsToClear = [ "nameField", "emailField", "destinationField", "descriptionField", "startDateField", "returnDateField" ]; for (var i = 0; i < fieldsToClear.length; i++) { this[fieldsToClear[i]].value = null; } } validateFields() { this.allValid = true; //Make sure all required fields are set. var requiredFieldNames = [ "nameField", "emailField", "destinationField", "startDateField", "returnDateField" ]; for (var i = 0; i < requiredFieldNames.length; i++) { var field = this[requiredFieldNames[i]]; if (!field.value) { this.allValid = false; this[requiredFieldNames[i] + "ValidationResponseLabel"].innerText = "Dieses Feld musst du ausfüllen."; this[requiredFieldNames[i] + "ValidationResponseLabel"].style.display = ""; } else { this[requiredFieldNames[i] + "ValidationResponseLabel"].style.display = "none"; } } //Make sure the return date is after the start date. if (this.startDateField.value && this.returnDateField.value) { var startDate = new Date(this.startDateField.value); var returnDate = new Date(this.returnDateField.value); var startTime = startDate.getTime(); var returnTime = returnDate.getTime(); if (returnTime <= startTime) { this.allValid = false; this.returnDateFieldValidationResponseLabel.innerText = "Frou Monk kann nicht mit Überlichtgeschwindigkeit fahren. Deswegen kannst du damit auch nicht in die Vergangenheit reisen. Wähle stattdessen ein Rückgabedatum, das nach dem Startdatum kommt."; this.returnDateFieldValidationResponseLabel.style.display = ""; } else { this.returnDateFieldValidationResponseLabel.style.display = "none"; //Also check if there are any other reservations in the selected date range. var checkingDate = new Date(startDate.getTime() - 86400000); while (checkingDate.getFullYear() != returnDate.getFullYear() || checkingDate.getMonth() != returnDate.getMonth() || checkingDate.getDate() != returnDate.getDate()) { checkingDate.setDate(checkingDate.getDate() + 1); if (ReservationManager.getReservationStatusForDate(checkingDate) != 0) { this.allValid = false; this.returnDateFieldValidationResponseLabel.innerText = "Zu dieser Zeit ist Frou Monk schon reserviert. Schau im Kalender nach, wann sie frei ist."; this.returnDateFieldValidationResponseLabel.style.display = ""; break; } } if (this.allValid) { this.returnDateFieldValidationResponseLabel.style.display = "none"; } } } } } UIKit.registerViewControllerType(ReservationViewController);