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

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

    displayArea = 'trouble';
    isTroubleDis = false;
    isAlarmDis = false;
    fieldRegionNo = '';
    fieldRegionName = '';
    fieldRegionGroup = '';

    detailInfo: any = {};
    detailInfoLength = 0;
    disposeState = '';
    fieldDeviceType = '';
    startTime = null;
    stopTime = null;
    curInterval = 12;

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

    // commTmr: any;

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

    ngOnInit() {
        this.init();
        // this.commTmr = window.setInterval(() => {
        //     console.log(1);
        //     if (this.displayArea  === 'trouble') {
        //         this.showAlarmList = this.troubleList;
        //     } else {
        //         this.showAlarmList = this.alarmList;
        //     }
        //
        // }, 1000) ;
    }

    init() {
        this.ac.queryParams.subscribe((data) => {
            this.fieldRegionNo = data.no;
            this.displayArea = data.displayArea;
            this.curInterval = data.interval;

            this.setDateInterval(this.curInterval);
            this.showArea(data.displayArea);
            this.getTroubleList();
            this.getAlarmList();
        });
    }

    setDateInterval(interval) {
        this.startTime = this.datePipe.transform(this.getStartTime(interval), 'yyyy-MM');
        this.stopTime = this.datePipe.transform(new Date(), 'yyyy-MM');
    }

    showArea(displayArea) {
        this.isTroubleDis = displayArea === 'trouble' ? true : false;
        this.isAlarmDis = displayArea === 'alarm' ? true : false;
    }

    getTroubleList() {
        if (this.displayArea === 'trouble') {
            this.isTroubleDis = true;
            this.detailInfo = {};
            const req = {
                fieldRegionNo: this.fieldRegionNo,
                startTime: this.parseTime(this.startTime, 'start'),
                stopTime: this.parseTime(this.stopTime, 'stop'),
                disposeState: '',
                fieldDeviceType: ''
            };
            if (this.disposeState) {
                req.disposeState = this.disposeState;
            }
            if (this.fieldDeviceType) {
                req.fieldDeviceType = this.fieldDeviceType;
            }
            this.api.troubleLog.getByTerm([req, (data) => {
                this.detailInfo = data;
                if (data.trouble && data.trouble.constructor === Array) {
                    data.trouble.forEach((value) => {
                        value.troubleTime = this.api.formatTime('yyyy-MM-dd hh:mm:ss', new Date(value.troubleTime));
                    });
                    this.detailInfo.trouble = data.trouble;
                    this.detailInfoLength = data.trouble.length();
                }
            }]);
        }
    }

    getAlarmList() {
        if (this.displayArea === 'alarm') {
            this.isAlarmDis = true;
            this.detailInfo = {};
            const req = {
                fieldRegionNo: this.fieldRegionNo,
                startTime: this.parseTime(this.startTime, 'start'),
                stopTime: this.parseTime(this.stopTime, 'stop'),
                disposeState: '',
                fieldDeviceType: ''
            };
            if (this.disposeState) {
                req.disposeState = this.disposeState;
            }
            if (this.fieldDeviceType) {
                req.fieldDeviceType = this.fieldDeviceType;
            }
            this.api.alarm.getCowshedAlarm([req, (data) => {
                this.detailInfo = data;
                if (data.trouble && data.trouble.constructor === Array) {
                    data.trouble.forEach((value) => {
                        value.troubleTime = this.api.formatTime('yyyy-MM-dd hh:mm:ss', new Date(value.alarmTime));
                    });
                    this.detailInfo.trouble = data.trouble;
                    this.detailInfoLength = data.trouble.length();
                }
            }]);
        }
    }

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

    segmentChanged(ev: any) {
        if (ev.target.value === 'alarm') {
            this.displayArea = 'alarm';
            this.getAlarmList();
        } else {
            this.displayArea = 'trouble';
            this.getTroubleList();
        }
    }

    // 确认修复
    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) {
        const req = {
            troubleLogNo: this.displayArea === 'alarm' ? alarm.alarmLogNo : alarm.troubleLogNo,
            disposeState: 1,
            isReal: real === 1 ? 1 : 0
        };

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

    getStartTime(interval) {
        const now = new Date;
        const year = now.getFullYear();
        const month = now.getMonth() - interval;
        const day = now.getDate();
        const time = new Date(year, month, day, 0, 0, 0);
        return time;
    }

    parseTime(time, flg) {
        const year = time.slice(0, 4);
        const month = parseInt(time.slice(5, 7), 10);
        let updateTime = 0;
        if (flg === 'start') {
            updateTime = new Date( year, month - 1, 1, 0, 0, 0).valueOf();
        } else {
            updateTime = new Date( year, month, 0, 23, 59, 59).valueOf();
        }
        return updateTime;
    }

    reflush() {
        this.getTroubleList();
        this.getAlarmList();
    }
}
