import {Component, ComponentFactoryResolver, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, ActivationStart, Router, RouterOutlet} from '@angular/router';
import {Api} from '../../service/api';
import {EventService} from '../../service/event';

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

    @ViewChild(RouterOutlet) outlet: RouterOutlet;

  constructor(private router: Router, private api: Api, public eventService: EventService) {

  }

  totalAlert;

  ngOnInit() {
      // this.router.events.subscribe(e => {
      //     if (e instanceof ActivationStart && e.snapshot.outlet === 'tabs')
      //         console.log('DDEDEDEEEEEEEEEDDDDDDDDDDDDDDDDD');
      //         this.outlet.deactivate();
      // });
      this.totalAlert = 0;

      this.init();

      // 监听[告警数]变化
      this.eventService.event.on('refreshAlarmInfo', () => {
        console.log('refreshAlarmInfo');
        this.init();
      });

  }

  init() {
    this.api.alert.getAll([{fieldRegionType: '牛舍'}, (data) => {
      if (data.total > 0) {
        this.totalAlert = 0;
        const rows = data.rows;
        if (rows.constructor === Array) {
          rows.map((value, index) => {
            this.totalAlert = this.totalAlert + value.devErr + value.alarm;
          });
        }
      }
    }]);
  }

}
