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';
import {NavController} from '@ionic/angular';

@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, public nav: NavController) {
    }

    @ViewChild('slide') slide;

    imgs = ['1.jpg', '2.jpg', '3.jpg'];
    time = 0;
    ifFocus = false;
    searchText = '';
    isError = false;

    lunbo = {
        speed: 1000,
        allowTouchMove: true,
        effect: 'flip',
        autoplay: {
            delay: 2000,
        },
        loop: true,
        spaceBetween: 0,
        zoom: false
    };

    items = [];

    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) => {
            if (data && data.constructor === Array) {
                this.items = [];
                this.isError = false;
                data.map((value, index) => {
                    const itemInfo = {
                        name: value.fieldRegionName,
                        no: value.fieldRegionNo,
                        yewei: value.liquidLevel,
                        temp: value.temp,
                        img: this.getImgName(value.fieldRegionName),
                        url: value.fieldRegionGroup === '中转池自动控制组' ? 'east' : 'other',
                        time: new Date().getTime(),
                        state: value.state
                    };
                    this.items.push(itemInfo);
                });
            } else {
                this.api.presentMsgToast('获取数据失败');
                this.isError = true;
            }
            this.load.offLoad();
        }]);
    }

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

    toControl(url, name, no) {
        const query = '?name=' + name + '&no=' + no;
        this.tf.transfer({
            url: '/' + url,
            query: query,
            hash: ''
        });
    }

    toIndex() {
        if (this.ifFocus) {
            this.ifFocus = false;
        } else {
            this.tf.transfer({url: '/index'});
        }
    }

    pullToRefresh(event) {
        if (event === 'down') {
            this.getData(1);
        }
    }

    getImgName(name) {
        let ret = 'fajiao.jpg';

        switch (name) {
            case '东中转池':
                ret = 'e.jpg';
                break;
            case '西中转池' :
                ret = 'w.jpg';
                break;
            case '发酵池' :
                ret = 'fajiao.jpg';
                break;
            case '分离池' :
                ret = 'fenli.jpg';
                break;
            case '匀浆池' :
                ret = 'yunjiang.jpg';
                break;
            case '出料池' :
                ret = 'chuliao.jpg';
                break;
            case '沉沙池' :
                ret = 'chensha.jpg';
                break;
            default:
                break;
        }

        return ret;
    }

    toggleFocus(e) {
        this.ifFocus = e;
    }
}
