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 = {};
    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.detailInfo = {};
            const req = {
                fieldRegionNo: this.fieldRegionNo,
                startTime: this.startTime,
                stopTime: this.stopTime,
                disposeState: '',
                fieldDeviceType: ''
            };
            if (this.disposeState) {
                req.disposeState = this.disposeState;
            }
            if (this.fieldDeviceType) {
                req.fieldDeviceType = this.fieldDeviceType;
            }
            this.api.troubleLog.getByTerm([req, (data) => {
                // ---------------------------------------------------------
                // TODO 测试用数据
                data = {
                    fieldRegionNo: 'FRN110268534764224512',
                    fieldRegionName: '1号牛舍',
                    startTime: 1565165000000,
                    stopTime: 156516501230000,
                    devErr: 11,
                    devErrTotal: 33,
                    trouble: [{
                        troubleLogNo: '123415646196875456',
                        fieldDeviceName: '一号风机组',
                        fieldDeviceType: '风机',
                        description: '一号风机组故障',
                        troubleTime: 1565165000000,
                        data: {liquidPump: 25, press: 50},
                        disposeState: 0
                    }, {
                        troubleLogNo: '123415646196875456',
                        fieldDeviceName: '一号风机组',
                        fieldDeviceType: '风机',
                        description: '一号风机组故障',
                        troubleTime: 1565165000000,
                        data: {liquidPump: 25, press: 50},
                        disposeState: 0
                    }, {
                        troubleLogNo: '123415646196875456',
                        fieldDeviceName: '一号风机组',
                        fieldDeviceType: '风机',
                        description: '一号风机组故障',
                        troubleTime: 1565165000000,
                        data: {liquidPump: 25, press: 50},
                        disposeState: 0
                    }, {
                        troubleLogNo: '123415646196875456',
                        fieldDeviceName: '一号风机组',
                        fieldDeviceType: '风机',
                        description: '一号风机组故障',
                        troubleTime: 1565165000000,
                        data: {liquidPump: 25, press: 50},
                        disposeState: 0
                    }, {
                        troubleLogNo: '123415646196875456',
                        fieldDeviceName: '一号风机组',
                        fieldDeviceType: '风机',
                        description: '一号风机组故障',
                        troubleTime: 1565165000000,
                        data: {liquidPump: 25, press: 50},
                        disposeState: 0
                    }]
                };
                // --------------------------------------------------------

                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;
                }
            }]);
        }
    }

    getAlarmList() {
        if (this.displayArea === 'alarm') {
            this.detailInfo = {};
            const req = {
                fieldRegionNo: this.fieldRegionNo,
                startTime: this.startTime,
                stopTime: this.stopTime,
                disposeState: '',
                fieldDeviceType: ''
            };
            if (this.disposeState) {
                req.disposeState = this.disposeState;
            }
            if (this.fieldDeviceType) {
                req.fieldDeviceType = this.fieldDeviceType;
            }
            this.api.alarm.getCowshedAlarm([req, (data) => {
                // ---------------------------------------------------------
                // TODO 测试用数据
                data = {
                    fieldRegionNo: 'FRN110268534764224512',
                    fieldRegionName: '1号牛舍',
                    startTime: 1565165000000,
                    stopTime: 156516501230000,
                    alarm: 22,
                    alarmTotal: 44,
                    trouble: [{
                        alarmLogNo: '123415646196875456',
                        fieldDeviceName: '一号风机组',
                        fieldDeviceType: '风机',
                        description: '温度偏高',
                        data: {temp: 25, humid: 50, heatStressLevel: '轻度', liquidPump: 25, press: 50, NH3: 50},
                        alarmTime: 1565165000000,
                        disposeState: 0
                    }, {
                        alarmLogNo: '123415646196875456',
                        fieldDeviceName: '一号风机组',
                        fieldDeviceType: '风机',
                        description: '温度偏高',
                        data: {temp: 25, humid: 50, heatStressLevel: '轻度', liquidPump: 25, press: 50, NH3: 50},
                        alarmTime: 1565165000000,
                        disposeState: 0
                    }, {
                        alarmLogNo: '123415646196875456',
                        fieldDeviceName: '一号风机组',
                        fieldDeviceType: '风机',
                        description: '温度偏高',
                        data: {temp: 25, humid: 50, heatStressLevel: '轻度', liquidPump: 25, press: 50, NH3: 50},
                        alarmTime: 1565165000000,
                        disposeState: 0
                    }, {
                        alarmLogNo: '123415646196875456',
                        fieldDeviceName: '一号风机组',
                        fieldDeviceType: '风机',
                        description: '温度偏高',
                        data: {temp: 25, humid: 50, heatStressLevel: '轻度', liquidPump: 25, press: 50, NH3: 50},
                        alarmTime: 1565165000000,
                        disposeState: 0
                    }, {
                        alarmLogNo: '123415646196875456',
                        fieldDeviceName: '一号风机组',
                        fieldDeviceType: '风机',
                        description: '温度偏高',
                        data: {temp: 25, humid: 50, heatStressLevel: '轻度', liquidPump: 25, press: 50, NH3: 50},
                        alarmTime: 1565165000000,
                        disposeState: 0
                    }]
                };
                // ---------------------------------------------------------

                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;
                }
            }]);
        }
    }

    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;
    }

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