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

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

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

    @ViewChild('slide') slide;

    imgs = ['1.jpg', '2.jpg', '3.jpg'];

    lunbo = {
        speed: 1000,
        allowTouchMove: true,
        effect: 'flip',
        autoplay: {
            delay: 2000,
        },
        loop: true,
        spaceBetween: 0
    };
    alarms = [1, 2, 3, 4, 5];

    data = [];

    items = [{
        name: '东中转池',
        yewei: '',
        nan: false,
        bei: false,
        isNB: 1,
        url: 'east'
    }, {
        name: '西中转池',
        yewei: '',
        nan: false,
        bei: false,
        isNB: 1,
        url: 'west'
    }];
    state = {
        refreshState: {
            currentState: 'deactivate',
            drag: false
        },
        direction: '',
        endReachedRefresh: false,
        data: [],
        directionName: 'up'
    };

    ngOnInit(): void {
        this.init();
        this.ac.queryParams.subscribe((e) => {
            this.ionViewWillEnter();
        });
    }

    // ionic的生命周期函数
    ionViewWillEnter () {
        this.slideDidChange();
        this.getData(1);
    }

    ngOnDestroy(): void {
    }

    init() {
        this.getData(1);
    }

    getData(time) {
        if (time) {
            this.load.toLoad('加载中...', false);
        }
        this.api.app.getAll(['', (data) => {
            console.log(data);
            if (data.constructor === Array) {
                this.data = data;
            } else {
                this.api.presentMsgToast('获取数据失败');
            }
            this.load.offLoad();
        }]);
    }

    slideDidChange() {
        this.slide.startAutoplay();
    }

    toControl(url) {
        this.tf.transfer({
            url: '/' + url,
            query: '',
            hash: ''
        });
    }

    pullToRefresh(event) {
        if (event === 'endReachedRefresh') {
            console.log(11111111111111);
        } else {
            console.log(22222222222222);
        }
    }
}
