import { Component, OnInit } from '@angular/core'; import { AlertController, ModalController, PopoverController, Platform } from '@ionic/angular'; import { EventBody } from 'src/app/models/eventbody.model'; import { EventsService } from 'src/app/services/events.service'; import { Event } from '../../../models/event.model'; import { EditEventPage } from '../edit-event/edit-event.page'; import { ActivatedRoute } from '@angular/router'; import { ToastService } from 'src/app/services/toast.service'; import { EliminateEventPage } from 'src/app/modals/eliminate-event/eliminate-event.page'; import { ExpedientTaskModalPageNavParamsTask } from 'src/app/models/ExpedientTaskModalPage'; import { DocumentSetUpMeetingPage } from 'src/app/modals/document-set-up-meeting/document-set-up-meeting.page'; import { ExpedientTaskModalPage } from '../../gabinete-digital/expediente/expedient-task-modal/expedient-task-modal.page'; import { ViewDocumentPage } from 'src/app/modals/view-document/view-document.page'; import { BackgroundService } from 'src/app/services/background.service'; import { StorageService } from 'src/app/services/storage.service'; import { ThemeService } from 'src/app/services/theme.service' import { RouteService } from 'src/app/services/route.service'; import { SessionStore } from 'src/app/store/session.service'; import { HttpErrorHandle} from 'src/app/services/http-error-handle.service' import { AttachmentsService } from 'src/app/services/attachments.service'; @Component({ selector: 'app-view-event', templateUrl: './view-event.page.html', styleUrls: ['./view-event.page.scss'], }) export class ViewEventPage implements OnInit { loadedEvent: Event; isEventEdited: boolean; eventBody: EventBody; loadedAttachments: any; pageId: string; showLoader: boolean; minDate: Date; profile: string; eventId: string; CalendarId: string; caller: string; customDate: any; today: any; months = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"]; days = ["Domingo", "Segunda-feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado"]; dicIndex = 0; isModal = false header = true task: ExpedientTaskModalPageNavParamsTask; LoadedDocument: any = null; sesseionStora = SessionStore constructor( private modalController: ModalController, /* private navParams: NavParams, */ public eventsService: EventsService, public alertController: AlertController, public popoverController: PopoverController, private activatedRoute: ActivatedRoute, private toastService: ToastService, public platform: Platform, private backgroundservice: BackgroundService, private storage: StorageService, public ThemeService: ThemeService, private RouteService: RouteService, private httpErrorHandle: HttpErrorHandle, private attachmentsService: AttachmentsService, ) { this.isEventEdited = false; this.loadedEvent = new Event(); this.eventBody = { BodyType: "1", Text: "" }; this.loadedEvent.Body = this.eventBody; this.activatedRoute.paramMap.subscribe(params => { this.eventId = params['params'].eventId; const urlParams = new URLSearchParams(window.location.search); this.CalendarId = urlParams.get('CalendarId'); this.eventId = this.eventId.replace(' ', '') if (params["params"].caller) { this.caller = (params["params"].caller); } if (params["params"].isModal) { this.isModal = params["params"].isModal } if (params["params"].header) { this.header = params["params"].header } }); } ngOnInit() { this.loadEvent(); this.backgroundservice.registerBackService('Online', () => { this.storage.get('eventEdit').then((req) => { JSON.parse(req).forEach(element => { this.eventsService.editEvent(element, 2, 3).subscribe((res) => { this.storage.remove('eventEdit') }) }); }) this.storage.get('eventDelete').then((req) => { JSON.parse(req).forEach(element => { this.eventsService.editEvent(element, 2, 3).subscribe((res) => { this.storage.remove('eventDelete') }) }); }) this.loadEvent(); }); window.onresize = (event) => { // if not mobile remove all component if (window.innerWidth >= 1024) { this.modalController.dismiss(this.isEventEdited); } }; } // getEventsFromDB () { // return new Promise((resolve, reject) => { // if (this.platform.is('desktop') || this.platform.is('mobileweb')) { // this.storage.get('agendaResponse').then((events) => { // resolve(events) // }) // } else { // this.sqliteservice.getAllEvents().then((events: any[] = []) => { // resolve(events) // }) // } // }).catch ((error) => { // console.error(error); // }); // } openOptions() {} close() { this.modalController.dismiss(this.isEventEdited); } goBack() { if (this.isModal) { this.close() } else { this.activatedRoute.paramMap.subscribe(params => { if (params["params"].caller == 'expediente') { window.history.back(); } else { // this.router.navigate(['/home', params["params"].caller]); this.RouteService.goBack(); } }); } } doRefresh(ev) { this.loadEvent(); ev.target.complete(); } loadEvent() { const loader = this.toastService.loading(); if(this.sesseionStora.user.Profile == 'MDGPR' || this.sesseionStora.user.Profile == 'PR') { this.eventsService.getEvent(this.eventId).subscribe(res => { this.loadedEvent = res; // this.addEventToDb(res); loader.remove() }, (error) => { if (error.status === 0) { this.getFromDb(); } else { this.httpErrorHandle.httpStatusHandle(error) loader.remove() this.modalController.dismiss('Eevent not Foud'); this.RouteService.goBack(); } loader.remove() }); } else { if(this.CalendarId) { this.eventsService.genericGetEvent(this.eventId, this.CalendarId).subscribe(res => { const div = document.createElement("div") div.innerHTML = res.Body.Text res.Body.Text = div.innerText this.loadedEvent = res; // this.addEventToDb(res); loader.remove() }, (error) => { if (error.status === 0) { this.getFromDb(); } else { this.toastService.badRequest('Este evento já não existe na sua agenda') loader.remove() this.modalController.dismiss('Eevent not Foud'); this.RouteService.goBack(); } loader.remove() }); } } } deleteYesOrNo() { this.alertController.create({ header: 'Eliminar evento?', message: '', buttons: [ { text: 'Não', handler: () => { // sconsole.log('Let me think'); } }, { text: 'Sim', handler: () => { this.deleteEvent(); } } ] }).then(res => { res.present(); }); } deleteEvent() { const loader = this.toastService.loading() if(this.sesseionStora.user.Profile == 'MDGPR' || this.sesseionStora.user.Profile == 'PR') { this.eventsService.deleteEvent(this.loadedEvent.EventId, 0, this.loadedEvent.CalendarName).subscribe(async () => { const alert = await this.alertController.create({ cssClass: 'my-custom-class', header: 'Evento removido', buttons: ['OK'] }); setTimeout(() => { alert.dismiss(); }, 1500); this.goBack(); this.httpErrorHandle.httpsSucessMessagge('delete event') }, () => { }, () => { loader.remove(); }); } else { this.eventsService.genericDeleteEvent(this.loadedEvent.EventId, 0, this.loadedEvent.CalendarName, this.loadedEvent.CalendarId).subscribe(async () => { const alert = await this.alertController.create({ cssClass: 'my-custom-class', header: 'Evento removido', buttons: ['OK'] }); setTimeout(() => { alert.dismiss(); }, 1500); this.goBack(); this.httpErrorHandle.httpsSucessMessagge('delete event') }, (error) => { this.httpErrorHandle.httpStatusHandle(error) }, () => { loader.remove(); }); } } async OpenDeleteEventModal() { const modal = await this.modalController.create({ component: EliminateEventPage, componentProps: { eventId: this.loadedEvent.EventId, caller: this.caller, }, cssClass: 'discart-expedient-modal', }); modal.onDidDismiss().then((res) => { if (res) { setTimeout(() => { /* this.loadEvent(); */ this.loadEvent() }, 250); this.isEventEdited = true; } }); await modal.present(); } toDateString(e) { return new Date(e).toDateString() } async editEventDetail() { const modal = await this.modalController.create({ component: EditEventPage, componentProps: { eventId: this.loadedEvent.EventId, caller: this.caller, }, cssClass: 'modal modal-desktop', }); await modal.present(); modal.onDidDismiss().then((res) => { if (res) { setTimeout(() => { /* this.loadEvent(); */ this.loadEvent() }, 250); this.isEventEdited = true; console.log('res', res) } }); } async editEvent() { let classs; if (window.innerWidth <= 800) { classs = 'modal modal-desktop' } else { classs = 'modal modal-desktop showAsideOptions' } const modal = await this.modalController.create({ component: EditEventPage, componentProps: { event: this.loadedEvent, caller: this.caller, }, cssClass: classs, }); modal.onDidDismiss().then((res) => { if (res) { setTimeout(() => { /* this.loadEvent(); */ this.loadEvent() }, 250); this.isEventEdited = true; if(res.data.Attendees?.length >= 1) { this.loadedEvent.HasAttachments = true this.getAttachments() } console.log('res', res) } this.getAttachments() }); await modal.present(); } docIndex(index: number) { this.dicIndex = index } getAttachments() { if(this.loadedEvent.HasAttachments) { this.attachmentsService.getAttachmentsById(this.loadedEvent.EventId).subscribe(res=>{ this.loadedEvent.Attachments = res; },((erro) => { console.error('editgetAttchament', erro) })); } } async LoadDocumentDetails() { const docId = this.loadedEvent.Attachments[this.dicIndex].SourceId const applicationId: any = this.loadedEvent.Attachments[this.dicIndex].ApplicationId const selectedDoc = this.loadedEvent.Attachments[this.dicIndex] this.task = { serialNumber: '', taskStartDate: '', isEvent: true, workflowInstanceDataFields: { FolderID: '', Subject: selectedDoc.SourceName, SourceSecFsID: selectedDoc.ApplicationId || selectedDoc['ApplicationID'], SourceType: 'DOC', SourceID: selectedDoc.SourceId, DispatchNumber: '' } } const modal = await this.modalController.create({ component: ViewDocumentPage, componentProps: { trustedUrl: '', file: { title: this.task.workflowInstanceDataFields.Subject, url: '', title_link: '', }, Document: this.loadedEvent.Attachments[this.dicIndex], applicationId: this.task.workflowInstanceDataFields.SourceSecFsID, docId: selectedDoc.SourceId, folderId: '', task: this.task }, cssClass: 'modal modal-desktop' }); await modal.present(); } async openBookMeetingModal() { let classs; if (window.innerWidth < 701) { classs = 'book-meeting-modal modal modal-desktop' } else { classs = 'modal modal-desktop showAsideOptions' } const modal = await this.modalController.create({ component: DocumentSetUpMeetingPage, componentProps: { subject: this.task.workflowInstanceDataFields.Subject, document: this.loadedEvent.Attachments[this.dicIndex], }, cssClass: classs, backdropDismiss: false }); modal.onDidDismiss().then(res => { //this.RouteService.goBack(); }); await modal.present(); } // efetuar despacho async openExpedientActionsModal(taskAction: any) { let classs; if (window.innerWidth < 701) { classs = 'modal modal-desktop' } else { classs = 'modal modal-desktop showAsideOptions' } const modal = await this.modalController.create({ component: ExpedientTaskModalPage, componentProps: { taskAction: taskAction, task: this.task, seachDocuments: this.loadedEvent.Attachments[this.dicIndex], aplicationId: this.loadedEvent.Attachments[this.dicIndex].ApplicationId || this.loadedEvent.Attachments[this.dicIndex]['ApplicationID'] }, cssClass: classs, }); await modal.present(); modal.onDidDismiss().then(async (res) => { }); } // addEventToDb(data) { // if (this.platform.is('desktop') || this.platform.is('mobileweb')) { // this.ionicStorage.set('eventDetails', data).then(() => { // }) // } else { // let event = { // Attendees: JSON.stringify(data.Attendees) || JSON.stringify(''), // Body: JSON.stringify(data.Body) || JSON.stringify(''), // CalendarId: data.CalendarId, // CalendarName: data.CalendarName, // Category: data.Category, // EndDate: data.EndDate, // EventId: data.EventId, // EventRecurrence: JSON.stringify(data.EventRecurrence) || JSON.stringify(''), // EventType: data.EventType, // HasAttachments: data.HasAttachments, // IsAllDayEvent: data.IsAllDayEvent, // IsMeeting: data.IsMeeting, // IsRecurring: data.IsRecurring, // Location: data.Location, // Organizer: JSON.stringify(data.Organizer) || JSON.stringify(''), // StartDate: data.StartDate, // Subject: data.Subject, // TimeZone: data.TimeZone // } // this.sqliteservice.updateEvent(event); // } // } getFromDb() { } }