import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
import { NavigationEnd, NavigationExtras, Router } from '@angular/router';
import { customTask, DailyWorkTask } from '../../../models/dailyworktask.model';
import { ProcessesService } from 'src/app/services/processes.service';
import { formatDate } from '@angular/common';
import { ModalController, NavParams, Platform } from '@ionic/angular';
import { AlertService } from 'src/app/services/alert.service';
import { DiplomaPage } from 'src/app/pages/gabinete-digital/diplomas/diploma/diploma.page';
import { CustomTaskPipe } from 'src/app/pipes/custom-task.pipe';
import { SortService } from 'src/app/services/functions/sort.service';
import { ThemeService } from 'src/app/services/theme.service'

@Component({
  selector: 'app-diplomas-assinar',
  templateUrl: './diplomas-assinar.page.html',
  styleUrls: ['./diplomas-assinar.page.scss'],
})
export class DiplomasAssinarPage implements OnInit {
 //profile:string;
 diplomasList:DailyWorkTask[] = [];
 showLoader: boolean;
 totalDocs:any;
 serialNumber:string;

 @Input() profile:string;
 @Input() segment:string;

 customTaskPipe = new CustomTaskPipe()
 skeletonLoader = true

 constructor(
   private processes:ProcessesService,
   private modalController: ModalController,
   private alertService: AlertService,
   private router: Router,
   private sortService: SortService,
  public ThemeService: ThemeService) {
   }

  ngOnInit() {
    const location = window.location
    const pathname = location.pathname + location.search

    this.LoadList()

    this.router.events.forEach((event) => {
      if (event instanceof NavigationEnd && event.url.startsWith(pathname)) {
        if(window.location.pathname.split('/').length >= 4 && window.location.pathname.startsWith('/home/gabinete-digital')) {
          this.refreshing()
        } else {
          this.LoadList()
        }
      }
    });
  }



  goToDiploma(serialNumber:any){
    this.router.navigate(['/home/gabinete-digital/diplomas-assinar',serialNumber,'gabinete-digital']);
  }

 async refreshing() {
    setTimeout(() => {
      this.LoadList();
    }, 1000);
  }

  doRefresh(event) {

    setTimeout(() => {
      this.LoadList();
    }, 1000);
  }

  async LoadList(){

    this.skeletonLoader = true

    let diplomas = await this.processes.GetTasksList("Despacho do Presidente da República", false).toPromise();
    this.diplomasList = [];

    let diplomasAssinar = diplomas.filter(data => data.activityInstanceName == "Assinar Diploma");
    diplomasAssinar.forEach(element => {
      let task: customTask = this.customTaskPipe.transform(element)
      this.diplomasList.push(task);
    });

    this.diplomasList = this.sortService.sortDate(this.diplomasList, 'CreateDate')


    this.skeletonLoader = false
    this.showLoader = false;

  }

  goBack() {
    this.router.navigate(['/home/gabinete-digital']);
    // window.history.back()
  }

}
