import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { EventBody } from 'src/app/models/eventbody.model';
import { EventPerson } from 'src/app/models/eventperson.model';
import { ProcessesService } from 'src/app/services/processes.service';
import { ModalController } from '@ionic/angular';
import { NavigationEnd, Router } from '@angular/router';
import { AuthService } from 'src/app/services/auth.service';
import { LoginUserRespose } from 'src/app/models/user.model';
import { SortService } from 'src/app/services/functions/sort.service';

@Component({
  selector: 'app-event-list',
  templateUrl: './event-list.page.html',
  styleUrls: ['./event-list.page.scss'],
})
export class EventListPage implements OnInit {
  // [desktop] event list to approve

  showLoader: boolean;
  eventsPRList: any = []
  eventsMDGPRList: any = []
  eventPerson: EventPerson;
  eventBody: EventBody;
  categories: string[];
  serialnumber:string;
  loggeduser: LoginUserRespose;
  segment:string;

  @Input() profile:string;
  @Input() showComponent:string;
  @Output() cloneAllmobileComponent = new EventEmitter<any>();
  @Output() approveEventDismiss = new EventEmitter<any>();


  constructor(
    private processes:ProcessesService,
    private modalController: ModalController,
    private router: Router,
    private userAuth: AuthService,
    private sortService: SortService,
    ) {
      this.loggeduser = userAuth.ValidatedUser;
  }

  ngOnInit() {

    this.segment = this.loggeduser.Profile;

    const pathname = window.location.pathname

    this.router.events.forEach((event) => {
      if(event instanceof NavigationEnd && event.url == pathname) {
        this.LoadToApproveEvents();
      }
    });
  }

  ngOnChanges() {
    this.LoadToApproveEvents();
  }

  segmentChanged(ev: any) {
    this.LoadToApproveEvents();
  }

  toDateString(e) {
    return new Date(e).toDateString()
  }

  async LoadToApproveEvents() {
    this.showLoader = true;
    console.log(this.segment);
    if(this.segment == 'MDGPR'){
      let mdEventsOficial = await this.processes.GetTasksList('Agenda Oficial MDGPR', false).toPromise();
      let mdEventsPessoal = await this.processes.GetTasksList('Agenda Pessoal MDGPR', false).toPromise();

      let allEvents = mdEventsOficial.concat(mdEventsPessoal);
      if(allEvents.length > 0){
        this.eventsMDGPRList = this.sortService.sortArrayByDate(allEvents).reverse();
      }
      this.showLoader = false;
    }
    else if(this.segment == 'PR'){
      let prEventsOficial = await this.processes.GetTasksList('Agenda Oficial PR', false).toPromise();
      let prEventsPessoal = await this.processes.GetTasksList('Agenda Pessoal PR', false).toPromise();
      let allEvents = prEventsOficial.concat(prEventsPessoal);
      console.log(allEvents);
      if(allEvents.length > 0){
        this.eventsPRList = this.sortService.sortArrayByDate(allEvents).reverse();
      }
      this.showLoader = false;
    }

  }

  async openApproveModal(eventSerialNumber, data) {

    let a =this.eventsMDGPRList.filter((elem)=>{
      //console.log(eventSerialNumber," ",elem.serialNumber)
      return eventSerialNumber == elem.serialNumber
    });

    this.approveEventDismiss.emit({
      "serialNumber": eventSerialNumber,
      "action": "Aprovar",
      "saveData": data
    });
  }

  refreshing() {
    this.LoadToApproveEvents();
  }

  doRefresh(event) {
    this.LoadToApproveEvents();
    console.log('refresh');
    setTimeout(() => {
      event.target.complete();
    }, 2000);
  }

  close() {
    console.log('close')
    this.cloneAllmobileComponent.emit();
  }


}
