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 = [
        {time: 1506743279334, con: '11111111111111111111111', name: '池子名字'},
        {time: 1506743273434, con: '22222222222222222222222', name: '池子名字'},
        {time: 1506743212334, con: '33333333333333333333333', name: '池子名字'},
        {time: 1506743314334, con: '44444444444444444444444', name: '池子名字'}
    ];

    ngOnInit() {
        this.getData();
    }

    getData() {
        this.load.toLoad('加载中...', false);
        setTimeout(() => {
            this.getAlarmOneDay();
            this.load.offLoad();
        }, 2000);
    }

    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) {
                console.log(data.length);
                if(data.length > 0) {
                    this.itemExit = true;
                } else {
                    this.itemExit = false;
                }
                data.map((value, index) => {
                    let it = {time: 1506743279334, con: '11111111111111111111111', name: '池子名字'};
                    it.con = value.description;
                    it.time = value.alarmTime;
                    it.name = value.fieldRegionName;

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

    getAlarmOneDay() {
        this.req.endTime = new Date().getTime();
        this.req.startTime = new Date().getTime() - 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) {
                console.log(data.length);
                if(data.length > 0) {
                    this.itemExit = true;
                } else {
                    this.itemExit = false;
                }

                data.map((value, index) => {
                    let it = {time: 1506743279334, con: '11111111111111111111111', name: '池子名字'};
                    it.con = value.description;
                    it.time = value.alarmTime;
                    it.name = value.fieldRegionName;

                    this.data.push(it);
                });
            } else {
                this.itemExit = false;
            }
        }]);
    }
    choose(e) {
        if (e.detail.value === 'day') {
            this.getAlarmOneDay();
            console.log(e.detail.value);
        } else {
            this.getAlarmOneWeek();
            console.log(e.detail.value);
        }
    }

    onLeftClick() {
        this.tf.transfer({url: '/index'});
    }

}
