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;

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

    data = [];

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

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

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