text: sap.ui.define(["covid19/ui/booking/cv19-tracing-ui-booking/controller/BaseController","sap/ui/model/Sorter","sap/ui/model/Filter","sap/ui/model/FilterOperator","sap/m/MessageBox","sap/m/MessageToast","sap/ui/core/Fragment"],function(e,t,a,n,o,i,r){"use strict";return e.extend("covid19.ui.booking.cv19-tracing-ui-booking.controller.Main",{__targetName:"Main",__roomBookerFilters:[],__oDisplayedStartDate:null,__oDisplayedEndDate:null,__addReservationDialog:null,onBeforeRendering:function(){sap.ui.Device.resize.attachHandler(function(){this.__updateCalendarData()}.bind(this))},onAfterRendering:async function(){var e=this.getView().byId("RoomBookerPlanningCalendar");this.__updateLocalDateRangeObjects(e);try{var t=await this.__fetchDataFromBackEnd({path:"/DeviceSet",filters:[new a("Type",n.EQ,"BEACON")],urlParameters:{$select:"DeviceID,Type,Description,Capacity"}}),o=this.getComponentModel();o.setProperty("/rooms",t.results);t=await this.__fetchDataFromBackEnd({path:"/DeviceSet",filters:[new a("Type",n.EQ,"USER")],urlParameters:{$select:"DeviceID,Type,Description"}}),o.setProperty("/users",t.results);await this.__updateCalendarData()}catch(e){this.__handleCommunicationErrorMessageDisplay("errorFetchingDeviceSet",e)}},onPlanningCalendarSearch:async function(){this.setAppBusy(true);this.__updateCalendarData()},onAddNewReservationButtonPress:async function(e){if(!this.__addReservationDialog){await this.__loadAddNewReservationDialog()}this.__addReservationDialog.open();this.__resetNewReservationModelObject();this.__setDatePickerStartDate("startDateDatePickerMinDate",new Date)},onCalendarStartDateChange:async function(e){var t=e.getSource();this.__updateLocalDateRangeObjects(t);this.setAppBusy(true);await this.__updateCalendarData()},onNewReservationDialogAfterOpen:function(e){var t=e.getSource().getBindingContext().getObject(),a="D0_NewAppointmentCreationDialog",n=this.getFragmentControlById(a,"startDate"),o=this.getFragmentControlById(a,"endDate");n.setDateValue(t.DateStart);o.setDateValue(this.__addSecondsToDate(t.DateEnd,1))},onSelectRoomSelectionChange:function(e){var t=e.getParameters().selectedItem.getBindingContext().getPath(),a="D0_NewAppointmentCreationDialog",n=this.getFragmentControlById(a,"nrOfPeopleSlider");n.bindElement(t)},onDateTimePickerChange:function(e){var t=e.getSource().data("whichDate"),a=e.getSource().getBindingContext().getPath(),n=a.indexOf("newReservation")!==-1?"D0_NewAppointmentCreationDialog":"reservationDetailsPopover",i=e.getSource().getDateValue(),r=t==="DateStart"?1:0,s=this.getComponentModel(),l=s.getProperty(a);l[t]=this.__addSecondsToDate(i,r);if(t==="DateStart"){if(!l.ReservationID){this.__setDatePickerStartDate("endDateDatePickerMinDate",i)}if(l.DateEnd&&l.DateEnd<i){l.DateEnd=i;this.__setDateTimePickerDateValue(n,"endDate",i)}}else{if(l.DateStart&&l.DateStart>i){l.DateStart=i;this.__setDateTimePickerDateValue(n,"startDate",i)}}var D=s.getProperty("/rooms"),c=e.getSource().getBindingContext().getObject(),d=_.find(D,function(e){return e.DeviceID==c.RoomID}),p=this.__checkIfReservationOverlaps(d,l.DateStart,l.DateEnd,c);if(!p){o.error(this.getTranslation("roomAlreadyHasReservationsInThatTimeframe"))}},onParticipantsNumberValueChange:function(e){var t=e.getSource().getValue(),a=parseInt(t),n=e.getSource().getBindingContext().getPath(),o=this.getComponentModel();if(a&&!isNaN(a)){var i=n.indexOf("newReservation")!==-1?"D0_NewAppointmentCreationDialog":"reservationDetailsPopover",r=this.getFragmentControlById(i,"nrOfPeopleSlider"),s=r.getMax();a=a<s?a:s;o.setProperty([n,"PartecipantsNumber"].join("/"),a>0?a:1)}else{o.setProperty([n,"PartecipantsNumber"].join("/"),1)}},onNewReservationDialogSaveButton:function(e){var t=this.__checkIfDialogFieldsAreOk();if(!t){o.error(this.getTranslation("newReservationCreationErrorMissingFields"));return}var a=this.getComponentModel(),n=a.getProperty("/rooms"),r=a.getProperty("/newReservation"),s=_.find(n,function(e){return e.DeviceID==r.RoomID}),l=this.__checkIfReservationOverlaps(s,r.DateStart,r.DateEnd,r);if(!l){o.error(this.getTranslation("newReservationOverlapsExistingTasks"));return}this.setAppBusy(true);var D=this.getComponentModel("odata"),r=a.getProperty("/newReservation"),c=a.getProperty("/user"),d={ID:(new Date).getTime(),Subject:r.subject,DateStart:r.DateStart,DateEnd:r.DateEnd,PartecipantsNumber:r.PartecipantsNumber,Notes:r.Notes,RoomID:r.RoomID,EmployeeID:c.id,ApprovalStatus:0};this.__oRoom=s;this.setAppBusy(true);D.create("/ReservationSet",d,{success:async function(e){i.show(this.getTranslation("newReservationSavedSuccessfully"));this.onNewReservationDialogCancelButton();d.RoomName=this.__oRoom.Description;await this.__createWFTask(d,c);this.setAppBusy(false);await this.__updateCalendarData()}.bind(this),error:function(e){this.__handleCommunicationErrorMessageDisplay("newReservationNotSavedSuccessfully",e);this.setAppBusy(false)}.bind(this)})},onNewReservationDialogCancelButton:function(){this.__resetNewReservationModelObject();this.__resetStartAndEndDateTimePickers();this.__addReservationDialog.close()},onCalendarIntervalSelect:async function(e){var t=e.getParameter("startDate"),a=e.getParameter("endDate"),n=e.getParameter("row"),o=n.getBindingContext().getObject(),i=this.getComponentModel();i.setProperty("/newReservation",{RoomID:o.DeviceID,subject:"",DateStart:t,DateEnd:a,Notes:"",PartecipantsNumber:1});if(!this.__addReservationDialog){await this.__loadAddNewReservationDialog()}var r="D0_NewAppointmentCreationDialog",s=this.getFragmentControlById(r,"nrOfPeopleSlider");s.bindElement(n.getBindingContext().getPath());this.__addReservationDialog.bindElement("/newReservation");this.__addReservationDialog.open();this.__setDatePickerStartDate("startDateDatePickerMinDate",new Date)},__selectedAppointment:null,onReservationSelect:async function(e){var t=e.getParameter("appointment"),a=_.cloneDeep(t.getBindingContext().getObject()),n=this.getComponentModel();this.__selectedAppointment=t;if(t===undefined){return}if(!t.getSelected()){this._oDetailsPopover.close();return}if(!this._oDetailsPopover){this._oDetailsPopover=await r.load({id:"reservationDetailsPopover",name:"covid19.ui.booking.cv19-tracing-ui-booking.view.fragment.Main.D1_ReservationDetailsPopover",controller:this});this.getView().addDependent(this._oDetailsPopover)}n.setProperty("/editedReservation",a);this._oDetailsPopover.bindElement("/editedReservation");this.__setDatePickerStartDate("startDateDatePickerMinDate",null);this.__setDatePickerStartDate("endDateDatePickerMinDate",new Date);this._oDetailsPopover.openBy(t)},onDetailsPopoverAfterOpen:function(e){var t=e.getSource().getBindingContext().getObject(),a="reservationDetailsPopover",n=this.getFragmentControlById(a,"startDate"),o=this.getFragmentControlById(a,"endDate");n.setDateValue(t.DateStart);o.setDateValue(t.DateEnd)},onDetailPopoverUpdateButtonPress:function(e){var t=this.getComponentModel(),a=this.getComponentModel("odata"),n=t.getProperty("/user"),o=t.getProperty("/editedReservation"),i=a.createKey("ReservationSet",{ID:o.ReservationID}),r={ID:o.ReservationID,Subject:o.Subject,DateStart:o.DateStart,DateEnd:o.DateEnd,PartecipantsNumber:o.PartecipantsNumber,Notes:o.ReservationNotes,RoomID:o.RoomID,EmployeeID:o.EmployeeID,ApprovalStatus:0},s=t.getProperty("/rooms");this.__oRoom=_.find(s,function(e){return e.DeviceID==o.RoomID});this.setAppBusy(true);a.update("/"+i,r,{success:async function(e){try{r.RoomName=this.__oRoom.Description;await this.__createWFTask(r,n);this.__updateCalendarData();this._oDetailsPopover.close();t.setProperty("/reservationEditMode",false);this.__selectedAppointment.setSelected(false)}catch(e){this.__handleCommunicationErrorMessageDisplay("errorUpdatingReservationWF",e);this.setAppBusy(false)}this.setAppBusy(false)}.bind(this),error:function(e){this.__handleCommunicationErrorMessageDisplay("errorUpdatingReservation",e);this.setAppBusy(false)}.bind(this)})},onDetailPopoverCloseButtonPress:function(){this._oDetailsPopover.close()},onDetailsPopoverAfterClose:function(){if(this.__selectedAppointment){this.__selectedAppointment.setSelected(false)}this.getComponentModel().setProperty("/reservationEditMode",false)},onDeleteReservationButtonPress:function(e){var t=this.getComponentModel("odata"),a=e.getSource().getBindingContext().getObject(),n=t.createKey("/ReservationSet",{ID:a.ReservationID});this.setAppBusy(true);t.remove(n,{success:function(e){this.__updateCalendarData();this.setAppBusy(false);i.show(this.getTranslation("reservationDeletedSuccessfully"))}.bind(this),error:function(e){this.__handleCommunicationErrorMessageDisplay("errorDeletingReservation",e);this.setAppBusy(false)}.bind(this)})},__updateLocalDateRangeObjects:function(e){var t=e._getFirstAndLastRangeDate();this.__oDisplayedStartDate=new Date(t.oStartDate.oDate),this.__oDisplayedEndDate=new Date(t.oEndDate.oDate)},__updateCalendarData:async function(){var e=this.getComponentModel(),o=e.getProperty("/roomBookerSearchString"),i=[],r=[];i.push(new a({path:"DateStart",operator:n.BT,value1:this.__getUTCDate(this.__oDisplayedStartDate),value2:this.__oDisplayedEndDate,caseSensitive:false}));i.push(new a({path:"DateEnd",operator:n.BT,value1:this.__getUTCDate(this.__oDisplayedStartDate),value2:this.__oDisplayedEndDate,caseSensitive:false}));r.push(new a(i,false));r.push(new a({path:"ApprovalStatus",operator:n.NE,value1:2,caseSensitive:false}));if(o&&o.length>0){var s=["Description","Subject","ReservationNotes","EmployeeID"],i=[];for(var l in s){i.push(new a({path:s[l],operator:n.Contains,value1:o,caseSensitive:false}))}r.push(new a(i,false))}r=new a(r,true);try{var D=await this.__fetchDataFromBackEnd({path:"/RoomReservationSet",filters:r,sorters:new t("Description",false)}),c=D.results,d=_.groupBy(c,"DeviceID"),p=e.getProperty("/rooms");for(var l in p){var u=p[l];u.reservations=d[u.DeviceID]||[]}e.refresh();this.setAppBusy(false)}catch(e){this.__handleCommunicationErrorMessageDisplay("anErrorOccurredCallingODataService",e);this.setAppBusy(false)}},__fetchDataFromBackEnd:function(e){return new Promise(function(t,a){var n=e.filters||null,o=e.sorters||null,i=this.getComponentModel("odata");i.read(e.path,{filters:n?[n]:null,sorters:o?[o]:null,urlParameters:e.urlParameters,success:function(a){console.log(e.path+" -- data received",a);t(a)}.bind(this),error:function(e){a(e)}})}.bind(this))},__loadAddNewReservationDialog:async function(){this.__addReservationDialog=await r.load({controller:this,name:"covid19.ui.booking.cv19-tracing-ui-booking.view.fragment.Main.D0_NewAppointmentCreationDialog",id:"D0_NewAppointmentCreationDialog"});this.getView().addDependent(this.__addReservationDialog)},__resetNewReservationModelObject:function(){var e=this.getComponentModel();e.setProperty("/newReservation",{RoomID:null,subject:"",DateStart:null,DateEnd:null,Notes:"",PartecipantsNumber:1});e.refresh()},__resetStartAndEndDateTimePickers:function(){this.__setDateTimePickerDateValue("D0_NewAppointmentCreationDialog","startDate",null);this.__setDateTimePickerDateValue("D0_NewAppointmentCreationDialog","endDate",null)},__setDateTimePickerDateValue:function(e,t,a){var n=this.getFragmentControlById(e,t);n.setDateValue(a)},__addSecondsToDate:function(e,t){return new Date(e.getTime()+t*1e3)},__checkIfDialogFieldsAreOk:function(){var e=this.getComponentModel(),t=e.getProperty("/newReservation"),a="D0_NewAppointmentCreationDialog",n=this.getFragmentControlById(a,"nrOfPeopleSlider"),o=n.getMax(),i=!!t.RoomID&&t.subject.length>0&&!!t.DateStart&&!!t.DateEnd&&t.PartecipantsNumber<=o;return i},__setDatePickerStartDate(e,t){var a=this.getComponentModel();a.setProperty("/"+e,t)},__handleCommunicationErrorMessageDisplay:function(e,t){var a="";try{a=JSON.parse(t.responseText).error.message.value}catch(e){}o.error(this.getTranslation(e,[a]))},__checkIfReservationOverlaps:function(e,t,a,n){var o=_.sortBy(Object.assign({},e.reservations),function(e){return e.DateStart});for(var i in o){var r=o[i];if(r.ReservationID!==n.ReservationID){if(a<r.DateStart){return true}if(a>r.DateStart&&t<r.DateEnd){return false}}}return true},__createWFTask:function(e,t){return new Promise(function(a,n){var i=this.getComponentModel(),r=i.getProperty("/wfInitData");r.context.u={aM:t.approver||"john.doe@sap.demo",e:t.emails[0].value||"mary.pierce@sap.demo",n:[t.name.givenName,t.name.familyName].join(" ")};r.context.r={i:e.ID,s:e.Subject,sd:this.__formatDateTimeString(e.DateStart),ed:this.__formatDateTimeString(e.DateEnd),p:""+e.PartecipantsNumber,n:e.Notes||"",rN:e.RoomName};r.context.originalTask=e;var s=this.__getLocationUrl();r.context.appRouterUrl=s;$.ajax({url:"/bpmworkflowruntime/v1/xsrf-token",method:"GET",headers:{"X-CSRF-Token":"Fetch"},success:function(e,t,i){var s=i.getResponseHeader("X-CSRF-Token");if(s===null)return;$.ajax({url:"/bpmworkflowruntime/v1/workflow-instances",type:"POST",data:JSON.stringify(r),headers:{"X-CSRF-Token":s,"Content-Type":"application/json"},async:false,success:function(){o.information(this.getTranslation("approvalRequestHasBeenSent"));a(true)}.bind(this),error:function(e){n(e)}.bind(this)})}.bind(this)})}.bind(this))}})});