import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { NavigationStart, Router } from '@angular/router';
import { customTask, DailyWorkTask } from '../../../models/dailyworktask.model';
import { ProcessesService } from 'src/app/services/processes.service';
import { ModalController } from '@ionic/angular';
import { AlertService } from 'src/app/services/alert.service';
import { LoginUserRespose } from 'src/app/models/user.model';
import { AuthService } from 'src/app/services/auth.service';
import { CustomTaskPipe } from 'src/app/pipes/custom-task.pipe';
import { ExpedienteGdStore } from 'src/app/store/expedientegd-store.service';
import { ExpedienteTaskPipe } from 'src/app/pipes/expediente-task.pipe';
import { ThemeService } from 'src/app/services/theme.service'
import { SortService } from 'src/app/services/functions/sort.service';


@Component({
  selector: 'app-expedientes-pr',
  templateUrl: './expedientes-pr.page.html',
  styleUrls: ['./expedientes-pr.page.scss'],
})
export class ExpedientesPrPage implements OnInit {

  taskslist = [];
  serialNumber:string;
  skeletonLoader = true;
  expedienteGdStore = ExpedienteGdStore;
  customTaskPipe = new CustomTaskPipe()
  expedienteTaskPipe = new ExpedienteTaskPipe()

  loggeduser: LoginUserRespose;
  @Output() openExpedientDetail:EventEmitter<any> = new EventEmitter<any>();

  constructor(
    private processes:ProcessesService,
    private modalController: ModalController,
    private alertService: AlertService,
    private authService: AuthService,
    private router: Router,
    public ThemeService: ThemeService,
    private sortService: SortService,
    ) {
      this.loggeduser = authService.ValidatedUser;
    }

  ngOnInit() {

    this.LoadList()

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

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

  }

  openExpedientDetailPage(data){
    console.log(data);
    this.openExpedientDetail.emit(data);
  }

  LoadList() {
    this.skeletonLoader = true;
    this.processes.GetTaskListExpediente(false).subscribe(result => {

      this.taskslist = [];
      let res = result.reverse().filter(data => data.workflowInstanceDataFields.Status == "Active");
      res.forEach(element => {
        let task = this.expedienteTaskPipe.transform(element);
        this.taskslist.push(task);
      });
      
      this.taskslist = this.sortService.sortDate(this.taskslist, 'CreateDate')

      this.expedienteGdStore.reset(this.taskslist);
      this.skeletonLoader = false;

    });
  }

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

  doRefresh(event) {
    this.LoadList();
    setTimeout(() => {
      event.target.complete();
    }, 2000);
  }

  goToExpediente(serialNumber:any){
    /* this.router.navigate(['/home/gabinete-digital/expedientes-pr',serialNumber,'gabinete-digital']); */
    this.router.navigate(['/home/gabinete-digital/expediente', serialNumber, 'gabinete-digital']);
  }

}
