import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {Api} from '../../service/api';
import {AlertController} from '@ionic/angular';

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

  fieldRegionNo = '';
  fieldRegionName = '';
  fieldRegionGroup = '';

  mSelAlarm;
  showAlarmList = [];
  alarmList = [];
  troubleList = [];

    // 工作模式
    workModeSetting = {  'workMode' : '风机',
        'DeviceNo' : '',
        'workModeOld' : '1'};

    reqDummy = {
        "fieldRegionNo":"FRN110268534764224512",
        "fieldRegionName":"1号牛舍",
        "startTime":1565165000000,
        "stopTime":156516501230000,
        "devErr":36,
        "devErrTotal":55,
        "trouble":[{
            "troubleLogNo":"123415646196875456",
            "fieldDeviceName":"一号风机组",
            "fieldDeviceType":"风机",
            "description":"一号风机组故障",
            "troubleTime":1565165000000,
            "disposeState":0,
            "isReal":0
        }]
    }

    commTmr: any;

  constructor(private ac: ActivatedRoute, private api: Api, private alterContoller: AlertController) { }

  ngOnInit() {
      this.ac.queryParams.subscribe((data) => {
          this.fieldRegionNo = data.no;
          this.fieldRegionName = data.name;
      });

      this.mSelAlarm = 'trouble';

      this.getTroubletList();
      this.getAlarmList();

      this.commTmr = window.setInterval(() => {
          console.log(1);
          if(this.mSelAlarm  === 'trouble'){
              this.showAlarmList = this.troubleList;
          } else {
              this.showAlarmList = this.alarmList;
          }

      }, 1000) ;
  }

  getTroubletList(){
    const req = {'fieldRegionNo': this.fieldRegionNo,
                  'startTime':new Date().getTime(),
                  'stopTime' : new Date().getTime() - 7 * 24 * 3600 * 1000};
    this.api.troubleLog.getByTerm([req,(data)=> {
      console.log(data);
      data = this.reqDummy;

      if(data.trouble && data.trouble.constructor === Array){
          this.troubleList = [];
          data.trouble.map((value, index) => {
              let val = value;
              val.troubleTime = this.api.formatTime('yyyy-MM-dd hh:mm:ss', new Date(value.troubleTime))
              this.troubleList.push(val);
          })
      }
    }]);
  }

  getAlarmList(){
      const req = {'fieldRegionNo': this.fieldRegionNo,
          'startTime':new Date().getTime(),
          'stopTime' : new Date().getTime() - 7 * 24 * 3600 * 1000};
      this.api.alarm.getCowshedAlarm([req,(data)=> {
          console.log(data);
          data = this.reqDummy;

          if(data.trouble && data.trouble.constructor === Array){
              this.alarmList = [];
              data.trouble.map((value, index) => {
                  let val = value;
                  val.troubleTime = this.api.formatTime('yyyy-MM-dd hh:mm:ss', new Date(value.alarmTime))
                  this.alarmList.push(val);
              })
          }
      }]);
  }

    ionViewWillLeave() {
        clearInterval(this.commTmr);
    }

    segmentChanged(ev: any) {
        console.log('Segment changed', ev);
        if(ev === 'alarm') {
            this.showAlarmList = this.alarmList;
            this.mSelAlarm = 'alarm';
        } else {
            this.showAlarmList = this.troubleList;
            this.mSelAlarm = 'trouble';
        }
    }

    // 确认修复
    async onRecvClick(alarm){
        console.log(alarm);

        const alert = await this.alterContoller.create({
            header: '请注意',
            message: '请确认该故障是否为真实故障?',
            mode: 'ios',
            buttons: [
                {
                    text: '取消',
                    role: 'cancel',
                    cssClass: 'secondary',
                    //cssClass: 'width: 30%',
                }, {
                    text: '是',
                    //cssClass: 'width: 40%',
                    handler: () => {
                        this.processingFailure(1, alarm);
                    }
                }, {
                    text: '不是',
                    //cssClass: 'width: 30%',
                    handler: () => {
                        this.processingFailure(0,alarm);
                    }
                }
            ]
        });

        await alert.present();
    }

    processingFailure(real, alarm) {
        let req = {
            "troubleLogNo": this.mSelAlarm === 'alarm'?  alarm.alarmLogNo : alarm.troubleLogNo,
            "disposeState": 1,
            "isReal": real === 1 ? 1 : 0
        };

        this.api.troubleLog.processingFailure([req, (data) => {
            ;
        }]);
    }
}
