import { Component, Input, OnInit } 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 { NavigationStart, Router } from '@angular/router';
import { LoginUserRespose } from 'src/app/models/user.model';
import { AuthService } from 'src/app/services/auth.service';
import { EventoAprovacaoStore } from 'src/app/store/eventoaprovacao-store.service';
import { SortService } from 'src/app/services/functions/sort.service';

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

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

  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;
    this.LoadToApproveEvents();

    this.router.events.forEach((event) => {
      if (event instanceof NavigationStart &&
        '/home/gabinete-digital?eventos=true'.startsWith(event.url)) {

          if(window.location.pathname.split('/').length >= 4 && window.location.pathname.startsWith('/home/gabinete-digital')) {
            this.doRefresh()
          } else {
            this.LoadToApproveEvents()
          }
      }
    });

  }

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

  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();
      this.eventsMDGPRList = mdEventsOficial.concat(mdEventsPessoal);
      console.log(this.sortService.sortArrayByDate(this.eventsMDGPRList));

      this.eventaprovacaostore.resetmd(this.sortService.sortDate(this.eventsMDGPRList, 'CreateDate'));
    }
    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();
      this.eventsPRList = prEventsOficial.concat(prEventsPessoal);
      
      this.eventaprovacaostore.resetpr(this.sortService.sortDate(this.eventsPRList, 'CreateDate'));
    }
    this.showLoader = false;
  }


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

  goToEventDetail(event){
    this.router.navigate(['/home/gabinete-digital/event-list/approve-event',event.serialNumber, 'gabinete-digital'])
  }

  /* async openApproveModal(eventToAprove) {
    let classs;
    if( window.innerWidth <= 800){
      classs = 'modal modal-desktop'
    } else  {
      classs = 'modal modal-desktop showAsideOptions'
    }

    let InstanceId;

    if(!eventToAprove.workflowInstanceDataFields.hasOwnProperty('InstanceId')){
      InstanceId = ''
    } else {
      InstanceId =eventToAprove.workflowInstanceDataFields.InstanceId
    }

    const modal = await this.modalController.create({
      component: ApproveEventPage,
      componentProps:{
        serialNumber: eventToAprove.serialNumber,
        InstanceId: InstanceId ,
        showAside: true,
      },
      cssClass: classs,
    });
    await modal.present();
    modal.onDidDismiss().then((res) => {
      console.log(res['data']);

      this.LoadToApproveEvents();
    });
  } */

  doRefresh() {
    console.log('Refresh Events');
    setTimeout(()=>{
      this.LoadToApproveEvents();
    }, 1000)

  }
  close(){
    this.modalController.dismiss(null);
  }


}
