import { Component, OnInit } from '@angular/core';
import { customTask } from '../../../models/dailyworktask.model';
import { NavigationStart, Router } from '@angular/router';
import { DespachoStore } from 'src/app/store/despacho-store.service';
import { DespachoService } from 'src/app/Rules/despacho.service';

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

  despachoStore = DespachoStore;

  constructor (
   private router: Router,
   private despachoRule: DespachoService
  ) {}

  ngOnInit() {

    this.LoadList();

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

  goToDespacho({ SerialNumber } : customTask) {
    this.router.navigate(['/home/gabinete-digital/despachos',SerialNumber,'gabinete-digital']);
  }

  async LoadList() {

    await this.despachoRule.getList({updateStore: true})
  }

  get skeletonLoader(): boolean {
    return this.despachoRule.LoaderService.loading
  }

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

}
