import {Component, OnInit} from '@angular/core';
import {Api} from '../../service/api';
import {NavController} from '@ionic/angular';
import {Load} from '../../service/load';
import {EventService} from '../../service/event';


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

    alarmItems = [];

    curFieldRegion = {
        factoryNo: '',
        factoryName: '',
        fieldRegionNo: '',
        fieldRegionName: '',
        devErr: 0,
        devErrTotal: 0,
        alarm: 0,
        alarmTotal: 0,
        total: 0
    };

    curInterval = '12';
    popoverElement = null;

    constructor(private api: Api, public nav: NavController, private load: Load, public eventService: EventService) {
    }

    ngOnInit() {
        this.init();

        this.eventService.event.on('refreshAlarmInfo', () => {
            this.init();
        });
    }

    // ionic的生命周期函数
    ionViewWillEnter() {
        console.log('env-alarm: ionViewWillEnter');
        // this.init();
    }

    ionViewDidEnter() {
        console.log('env-alarm: ionViewDidEnter');
        // this.init();
    }

    init() {
        this.curInterval = '12';
        this.api.alert.getAll([{fieldRegionType: '牛舍'}, (data) => {
            if (data.total > 0) {
                if (data.rows && data.rows.constructor === Array) {
                    this.alarmItems = data.rows;
                    this.alarmItems.forEach(item => (item.interval = this.curInterval));
                }
            }
        }]);
    }

    toControl(one, displayArea) {
        const query = '/env-alarm-detail?no=' + one.fieldRegionNo + '&fieldRegionName=' + one.fieldRegionName
            + '&displayArea=' + displayArea + '&interval=' + one.interval;
        this.nav.navigateForward(query);
    }

    dismissPopover(one) { // interval, fieldRegionInfo
        this.curFieldRegion = one;
        this.load.toLoad('加载中...', false);
        const condition = {
            fieldRegionNo: this.curFieldRegion.fieldRegionNo,
            startTime: this.getStartTime(one.interval).valueOf(),
            stopTime: new Date().valueOf()
        };

        this.api.alert.getByTerm([condition, (data) => {
            if (data && data.fieldRegionNo) {
                this.curFieldRegion = data;
                this.alarmItems.forEach(item => {
                    if (item.fieldRegionNo === this.curFieldRegion.fieldRegionNo) {
                        item = this.curFieldRegion;
                    }
                });
            }
            this.load.offLoad();
        }]);
        // if (this.popoverElement) {
        //     this.popoverElement.dismiss().then(() => { this.popoverElement = null; });
        // }
        // this.displayInterval(interval);
    }

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

    // displayInterval(interval) {
    //     let displayValue = '';
    //     switch (interval) {
    //         case 12:
    //             displayValue = '近一年';
    //             break;
    //         case 6:
    //             displayValue = '近六个月';
    //             break;
    //         case 3:
    //             displayValue = '近三个月';
    //             break;
    //         case 1:
    //             displayValue = '近一个月';
    //             break;
    //         default:
    //             displayValue = '近一年';
    //             break;
    //     }
    //     document.getElementById(this.curFieldRegion.fieldRegionNo).innerText = displayValue;
    // }

    doRefresh(event) {
        setTimeout(() => {
            this.init();
            event.target.complete();
            // 通知刷新告警barge
            this.eventService.event.emit('refreshAlarmInfo');

        }, 1500);
    }
}
