import { Component, OnInit, EventEmitter, Output } from '@angular/core'; import { EventsService } from 'src/app/services/events.service'; import { NavigationExtras, Router } from '@angular/router'; import { ActivatedRoute, NavigationEnd } from '@angular/router'; import { ModalController, Platform } from '@ionic/angular'; import { EventDetailPage } from './event-detail/event-detail.page'; import { ProcessesService } from '../../services/processes.service'; import { ToDayEventStorage } from 'src/app/store/to-day-event-storage.service'; import { ExpedienteTaskPipe } from 'src/app/pipes/expediente-task.pipe'; import { ExpedienteGdStore } from 'src/app/store/expedientegd-store.service'; import { BackgroundService } from 'src/app/services/background.service'; import { ThemeService } from 'src/app/services/theme.service'; import { Storage } from '@ionic/storage'; import { PermissionService } from 'src/app/services/permission.service'; import { ViewEventPage } from 'src/app/modals/view-event/view-event.page'; import { ChangeProfileService } from 'src/app/services/change-profile.service'; import { SessionStore } from 'src/app/store/session.service'; import { TaskService } from 'src/app/services/task.service'; @Component({ selector: 'app-events', templateUrl: './events.page.html', styleUrls: ['./events.page.scss'], }) export class EventsPage implements OnInit { today = new Date(); 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"]; customDate = this.days[this.today.getDay()] + ", " + this.today.getDate() + " de " + (this.months[this.today.getMonth()]); grettings = ["Bom dia", "Boa tarde", "Boa noite"]; greetting = ''; timeDate = this.today.getHours() + ":" + this.today.getMinutes(); segment: string; public profile: string; currentEvent: any; maxSubjectLength = 30; customText = false; totalEvent = 0; currentHoursMinutes: Date | string; showLoader: boolean; expedientList: any; hideSearchBtn: boolean = false; // shared data toDayEventStorage = ToDayEventStorage expedienteGdStore = ExpedienteGdStore listToPresent = []; expedienteTaskPipe = new ExpedienteTaskPipe() @Output() openExpedientListPage: EventEmitter = new EventEmitter(); sessoStore = SessionStore; showAgendaLoader = false showCorrespondenciasLoader = false loadingAllTask = false agendaColor = '' filterName: 'Para hoje' | 'Novos'| 'Lidos'| 'Não lidos'| 'OverdueTasks' | 'Todos' = 'Não lidos' showSearch = true; searchSubject: string = ''; AllProcess = [] ordinance: string = 'old' listSubscription : { delete(): void; } constructor( private eventService: EventsService, private router: Router, public activatedRoute: ActivatedRoute, private processes: ProcessesService, private modalController: ModalController, public platform: Platform, private backgroundservice: BackgroundService, public ThemeService: ThemeService, private storage: Storage, public p: PermissionService, private changeProfileService: ChangeProfileService, public TaskService: TaskService ) { window['zipPhoneCallback'] = function (zipphone) { var frame = document.getElementById('home-iframe'); if(frame) { frame['contentWindow']['postMessage']({call:'cookies', value: { cookies: {} }}); } } this.changeProfileService.registerCallback(() => { this.listToPresent = []; }) if(this.agendaColor != "PR") { this.agendaColor = "MDGPR" } } ngAfterViewInit(): void { this.loadAllTask(); this.listSubscription = this.TaskService.registerCallback({ id: import.meta.url, funx:() => { this.dynamicSearch() } }) this.dynamicSearch() } ngOnDestroy() { this.listSubscription.delete() } ngOnInit() { this.showGreeting(); this.router.events.forEach((event) => { if (event instanceof NavigationEnd && event.url == '/home/events') { this.getEventsFromLocalDb() this.RefreshEvents(); setTimeout(() => { this.LoadList(); }, 1500) } }); this.hideSearch(); this.backgroundservice.registerBackService('Online', () => { this.showGreeting(); this.RefreshEvents(); this.LoadList(); this.hideSearch(); }); } async dynamicSearch() { const ordinance = this.ordinance if(this.showSearch && this.searchSubject) { const AllProcess = this.TaskService.AllProcess.filter((task) => { let subject = task.Folio || task.Subject || task.workflowInstanceDataFields.Subject subject = subject.toLowerCase(); return subject.includes(this.searchSubject.toLowerCase()) }).filter( task => this.TaskService.filter(task, this.filterName)) if(ordinance == this.ordinance) { this.AllProcess = this.TaskService.reorderList(this.ordinance, AllProcess) } } else { const AllProcess = this.TaskService.AllProcess if(ordinance == this.ordinance) { this.AllProcess = this.TaskService.reorderList(this.ordinance, AllProcess) .filter( task => this.TaskService.filter(task, this.filterName)) } } } async loadAllTask() { this.loadingAllTask = true await this.TaskService.LoadTask() this.dynamicSearch() this.loadingAllTask = false } hideSearch() { if (this.router.url == '/home/events') { this.hideSearchBtn = true; } } doRefresh(event) { if (event) { setTimeout(() => { try { event?.target?.complete(); } catch(error) {} }, 2000); } this.RefreshEvents(); this.LoadList(); this.loadAllTask(); } onSegmentChange() { this.RefreshEvents(); this.LoadList(); } async RefreshEvents() { this.currentEvent = ""; this.showLoader = true; this.showAgendaLoader = true; let date = new Date(); let month = date.getMonth() + 1; let start = date.getFullYear() + "-" + month + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds(); let end = date.getFullYear() + "-" + month + "-" + date.getDate() + " 23:59:59"; if(SessionStore.user) { let onwEvent:any = await this.eventService.getAllOwnEvents(start, end).catch((error) => { this.showLoader = false; this.showAgendaLoader = false; console.error(error) }) this.listToPresent = onwEvent; if(onwEvent?.length) { this.totalEvent = onwEvent.length; } this.storage.set('events', this.listToPresent) } this.showLoader = false; this.showAgendaLoader = false; } getEventsFromLocalDb() { let date = new Date(); let month = date.getMonth() + 1; //date.setMonth(date.getMonth() + 1); let dateToday = date.getFullYear() + "-" + month + "-" + date.getDate(); this.storage.get('events').then((events: any[] = []) => { // if(Array.isArray(events)) { this.listToPresent = events this.totalEvent = this.listToPresent.length try { this.currentEvent = this.listToPresent[0].Subject } catch (error) {} try { this.currentHoursMinutes = this.listToPresent[0].StartDate } catch (error) {} } // }) this.showLoader = false; } showGreeting() { if (this.today.getHours() >= 6 && this.today.getHours() < 12) { this.greetting = this.grettings[0]; } else if (this.today.getHours() >= 12 && this.today.getHours() < 18) { this.greetting = this.grettings[1]; } else /* if(this.today.getHours() < 6 && this.today.getHours() >= 18) */ { this.greetting = this.grettings[2]; } } gotTo() { this.router.navigate(['/home/events']); } changeProfile() { if (this.profile == "mdgpr") { // this.profile = "pr"; this.RefreshEvents(); } else { // this.profile = "mdgpr"; this.RefreshEvents(); } } async openEventDetail1(id: any) { // const modal = await this.modalController.create({ component: EventDetailPage, componentProps: { eventId: id, }, cssClass: 'event-detail', backdropDismiss: false }); modal.onDidDismiss(); await modal.present(); } async openEventDetail(id: any) { // const modal = await this.modalController.create({ component: ViewEventPage, componentProps: { eventId: id, }, cssClass: 'view-event', backdropDismiss: false }); modal.onDidDismiss(); await modal.present(); } firstEnter = false LoadList() { if(this.firstEnter) { this.showCorrespondenciasLoader = true this.getEventsFromLocalDb(); } this.firstEnter = true if(window['all-process-gabinete']) { window['all-process-gabinete']() } } sortArrayISODate(myArray: any) { return myArray.sort(function (a, b) { return (a.CreateDate < b.CreateDate) ? -1 : ((a.CreateDate > b.CreateDate) ? 1 : 0); }); } goToEvent(event: any) { let navigationExtras: NavigationExtras = { queryParams: { CalendarId: event.CalendarId } } this.router.navigate(['/home/events', event.EventId, 'agenda'], navigationExtras); } goToAllTaskFilter(event: any) { let navigationExtras: NavigationExtras = { queryParams: { filter: event, processes: true } } this.router.navigate(['/home/gabinete-digital'], navigationExtras); } viewExpedientListPage() { if (this.sessoStore.user.Profile == 'PR') { if (window.innerWidth < 701) { this.router.navigate(['/home/gabinete-digital/expedientes-pr']); } else { let navigationExtras: NavigationExtras = { queryParams: { "expedientes-pr": true, } }; this.router.navigate(['/home/gabinete-digital'], navigationExtras); } } else { if (window.innerWidth < 701) { this.router.navigate(['/home/gabinete-digital/expediente']); } else { let navigationExtras: NavigationExtras = { queryParams: { "expedientes": true, } }; this.router.navigate(['/home/gabinete-digital'], navigationExtras); } } } }