import {Component, OnInit} from '@angular/core';
import {Transfer} from '../service/transfer';
import {Api} from '../service/api';
import {Load} from '../service/load';

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

    constructor(private tf: Transfer, private api: Api, private load: Load) {
    }

    req = {startTime: 0, endTime: 0};

    itemExit = false;

    data = [];

    ngOnInit() {
        this.getData();
    }

    getData() {
        this.load.toLoad('加载中...', false);
        this.getAlarmOneDay();
    }

    getAlarmOneWeek() {
        this.req.endTime = new Date().getTime();
        this.req.startTime = new Date().getTime() - 7 * 24 * 3600 * 1000;

        const req = {
            endTime: this.req.endTime,
            startTime: this.req.startTime
        };
        this.api.alarm.getAlarmByTerm([req, (data) => {
            this.data = [];
            if (data && data.constructor === Array) {
                if (data.length > 0) {
                    this.itemExit = true;
                } else {
                    this.itemExit = false;
                }
                data.map((value, index) => {
                    const it = {
                        con: value.description,
                        time: value.alarmTime,
                        name: value.fieldRegionName,
                    };

                    this.data.push(it);
                });
            } else {
                this.itemExit = false;
            }
        }]);
    }

    getAlarmOneDay() {
        this.req.endTime = new Date().getTime();
        this.req.startTime = Math.floor(new Date().getTime() / (24 * 3600 * 1000)) * 24 * 3600 * 1000 - 8 * 3600 * 1000;

        const req = {
            endTime: this.req.endTime,
            startTime: this.req.startTime
        };
        this.api.alarm.getAlarmByTerm([req, (data) => {
            this.data = [];
            if (data && data.constructor === Array) {
                if (data.length > 0) {
                    this.itemExit = true;
                } else {
                    this.itemExit = false;
                }
                data.map((value, index) => {
                    const it = {
                        con: value.description,
                        time: value.alarmTime,
                        name: value.fieldRegionName,
                    };
                    this.data.push(it);
                });
            } else {
                this.itemExit = false;
            }
            this.load.offLoad();
        }]);
    }

    choose(e) {
        if (e.selectedIndex === 0) {
            this.getAlarmOneDay();
        } else {
            this.getAlarmOneWeek();
        }
    }

}
