import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core'; import { ModalController } from '@ionic/angular'; import { EventBody } from 'src/app/models/eventbody.model'; import { EventPerson } from 'src/app/models/eventperson.model'; import { EventsService } from 'src/app/services/events.service'; import { Event } from 'src/app/models/event.model'; import { AlertController } from '@ionic/angular'; import { removeDuplicate } from 'src/plugin/removeDuplicate.js' import { SearchPage } from 'src/app/pages/search/search.page'; import { AttachmentsService } from 'src/app/services/attachments.service'; import { Attachment } from 'src/app/models/attachment.model'; import { ToastService } from 'src/app/services/toast.service'; import { FormControl, FormGroup, Validators } from '@angular/forms'; import * as moment from 'moment'; @Component({ selector: 'app-edit-event', templateUrl: './edit-event.page.html', styleUrls: ['./edit-event.page.scss'], }) export class EditEventPage implements OnInit { stEvent: Event; isRecurring:string; isEventEdited: boolean; loadedEvent: Event; initCalendarName: string; eventBody: EventBody; segment:string = "true"; eventAttendees: EventPerson[]; // minDate: string; loadedEventAttachments: Attachment[]; public date: any; public disabled = false; public showSpinners = true; public showSeconds = false; public touchUi = false; public enableMeridian = false; public minDate: any; public maxDate: any; public stepHour = 1; public stepMinute = 5; public stepSecond = 5; Form: FormGroup; validateFrom = false @Input() taskParticipants: EventPerson[]; @Input() taskParticipantsCc: EventPerson[]; @Input() profile:string; @Input() selectedSegment: string; @Input() postEvent: Event; @Output() clearContact = new EventEmitter(); @Output() openAttendeesComponent = new EventEmitter(); @Output() closeComponent = new EventEmitter(); @Output() setIntervenient = new EventEmitter(); @Output() setIntervenientCC = new EventEmitter(); @Output() clearPostEvent = new EventEmitter(); public dateControlStart = new FormControl(moment("DD MM YYYY hh")); public dateControlEnd = new FormControl(moment("DD MM YYYY hh")); showLoader = false get dateStart () { return this.dateControlStart.value } get dateEnd () { return this.dateControlEnd.value } @ViewChild('picker') picker: any; @ViewChild('fim') fim: any; @ViewChild('inicio') inicio: any; @ViewChild('picker1') picker1: any; public options = [ { value: true, label: 'True' }, { value: false, label: 'False' } ]; public listColors = ['primary', 'accent', 'warn']; public stepHours = [1, 2, 3, 4, 5]; public stepMinutes = [1, 5, 10, 15, 20, 25]; public stepSeconds = [1, 5, 10, 15, 20, 25]; constructor( private modalController: ModalController, private eventsService: EventsService, public alertController: AlertController, private attachmentsService: AttachmentsService, private toastService: ToastService ) { } ngOnInit() { if(!this.restoreTemporaryData()) { // clear if(this.postEvent) { if( this.postEvent.Body){ if(typeof(this.postEvent.Body.Text) == 'string'){ this.postEvent.Body.Text = this.postEvent.Body.Text.replace(/<[^>]+>/g, ''); } } } // attendees list if(this.postEvent.Attendees != null) { this.postEvent.Attendees.forEach(e =>{ if(e.IsRequired) { this.taskParticipants.push(e); } else { this.taskParticipantsCc.push(e); } }) } this.taskParticipants = removeDuplicate(this.taskParticipants); this.taskParticipantsCc = removeDuplicate(this.taskParticipantsCc); this.setIntervenient.emit(this.taskParticipants); this.setIntervenientCC.emit(this.taskParticipantsCc); this.isEventEdited = false; if(this.postEvent.IsRecurring == false) { this.isRecurring = "Não se repete"; } else{ this.isRecurring = "Repete"; } } this.getAttachments(this.postEvent.EventId); this.restoreDatepickerData() } close() { this.closeComponent.emit(); this.setIntervenient.emit([]); this.setIntervenientCC.emit([]); this.clearContact.emit(); this.deleteTemporaryData(); } runValidation() { this.validateFrom = true } injectValidation() { this.Form = new FormGroup({ Subject: new FormControl(this.postEvent.Subject, [ Validators.required, // Validators.minLength(4) ]), Location: new FormControl(this.postEvent.Location, [ Validators.required, ]), CalendarName: new FormControl(this.postEvent.CalendarName), Categories: new FormControl(this.postEvent.Categories[0], [ Validators.required ]), dateStart: new FormControl(this.dateStart, [ Validators.required ]), dateEnd: new FormControl(this.dateEnd, [ Validators.required ]), IsRecurring: new FormControl(this.postEvent.IsRecurring, [ Validators.required ]), participantes: new FormControl(this.taskParticipantsCc.concat(this.taskParticipants), [ // Validators.required ]), }) } async save() { this.injectValidation() this.runValidation() if(this.Form.invalid) return false this.getDatepickerData() this.postEvent.Attendees = this.taskParticipants.concat(this.taskParticipantsCc); this.showLoader = true await this.eventsService.editEvent(this.postEvent, 2, 3).subscribe(async () => { if(this.initCalendarName != this.postEvent.CalendarName){ let body = { "EventId": this.postEvent.EventId, "CalendarDestinationName": this.postEvent.CalendarName, } console.log(body); await this.eventsService.changeAgenda(body).toPromise(); } this.showLoader = false; this.toastService.successMessage() }, error => { this.showLoader = false this.toastService.badRequest() }); this.clearPostEvent.emit(); this.deleteTemporaryData(); this.close(); } async addParticipants() { this.saveTemporaryData(); this.openAttendeesComponent.emit({ type: "intervenient" }); this.clearContact.emit(); } async addParticipantsCc() { this.saveTemporaryData(); this.openAttendeesComponent.emit({ type: "CC" }); this.clearContact.emit(); } getDatepickerData() { if (this.postEvent) { this.postEvent.StartDate = this.dateStart this.postEvent.EndDate = this.dateEnd } } restoreDatepickerData() { if (this.postEvent) { this.dateControlStart = new FormControl(moment(this.postEvent.StartDate)); this.dateControlEnd = new FormControl(moment(this.postEvent.EndDate)); } } saveTemporaryData() { this.getDatepickerData() window['temp.path:/home/agenda/edit-event.component.ts'] = { postEvent: this.postEvent, eventBody: this.eventBody, segment: this.segment } } restoreTemporaryData(): boolean { const restoredData = window['temp.path:/home/agenda/edit-event.component.ts'] if(JSON.stringify(restoredData) != "{}" && undefined != restoredData){ this.postEvent = restoredData.postEvent this.eventBody = restoredData.eventBody this.segment = restoredData.segment this.restoreDatepickerData() return true; } else { return false; } } deleteTemporaryData(){ window['temp.path:/home/agenda/edit-event.component.ts'] = {} } getAttachments(eventId: string){ this.attachmentsService.getAttachmentsById(eventId).subscribe(res=>{ this.loadedEventAttachments = res; console.log('res', res); }); } deleteAttachment(attachmentID: string) { this.attachmentsService.deleteEventAttachmentById(attachmentID).subscribe( res=>{ this.loadedEventAttachments = this.loadedEventAttachments.filter(e=> e.Id.toString() != attachmentID); }) } async getDoc() { const modal = await this.modalController.create({ component: SearchPage, cssClass: 'modal-width-100-width-background modal', componentProps: { type: 'AccoesPresidenciais & ArquivoDespachoElect', showSearchInput: true, select: true, } }); await modal.present(); modal.onDidDismiss().then( async (res)=>{ if(res){ const data = res.data; //data.selected const DocumentToSave = { SourceTitle: data.selected.Assunto, ParentId: this.postEvent.EventId, Source: '1', SourceId: data.selected.Id, ApplicationId: data.selected.ApplicationType.toString(), Id: '0', Link: '', SerialNumber: '', }; await this.attachmentsService.setEventAttachmentById(DocumentToSave).subscribe(()=>{ this.getAttachments(this.postEvent.EventId); }); } }); } }