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

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

    constructor(private tf: Transfer, private ac: ActivatedRoute,
                private api: Api, private load: Load, public nav: NavController) {
    }
    @ViewChild('slide') slide;
    searchText = '';
    isError = false;

    ifFocus = false;
    lunbo = {
        speed: 1000,
        allowTouchMove: true,
        effect: 'flip',
        autoplay: {
            delay: 2000,
        },
        loop: true,
        spaceBetween: 0,
        zoom: false,
        height: 150,
    };
    // 热应激良
    thiGood = 0;
    // 热应激差
    thiBad = 0;
    // 故障总数
    alarmTotal = 0;

    farmMsg = {
        temp: 28.6,
        humi: 32.1,
        thi: '中',
        hasCow: true,
    };

    items = [
        {
            name: '1号牛舍',
            temp: '23.5',
            humi: '82',
            heatStressLevel: '轻',
            online: true,
            operationModel: true,  // true 平控  false 现控
            isWisdomModel: 1,  // 0 手控  1 自控  2 智控
            alarm: true,
            ts: '2019-08-05 23:44',
        },
        {
            name: '2号牛舍',
            temp: '23.5',
            humi: '82',
            heatStressLevel: '轻',
            online: true,
            operationModel: true,  // true 平控  false 现控
            isWisdomModel: 1,  // 0 手控  1 自控  2 智控
            alarm: true,
            ts: '2019-08-05 23:44',
        },
        {
            name: '3号牛舍',
            temp: '23.5',
            humi: '82',
            heatStressLevel: '轻',
            online: true,
            operationModel: true,  // true 平控  false 现控
            isWisdomModel: 1,  // 0 手控  1 自控  2 智控
            alarm: true,
            time: '2019-08-05 23:44',
        },
        {
            name: '4号牛舍',
            temp: '23.5',
            humi: '82',
            thi: '轻',
            time: '2019-08-05 23:44',
            online: true,
            operationModel: true,  // true 平控  false 现控
            isWisdomModel: 1,  // 0 手控  1 自控  2 智控
            ctrl: 0,
            alarm: true,
        },
        {
            name: '5号牛舍',
            temp: '23.5',
            humi: '82',
            thi: '轻',
            time: '2019-08-05 23:44',
            online: true,
            operationModel: true,  // true 平控  false 现控
            isWisdomModel: 1,  // 0 手控  1 自控  2 智控
            ctrl: 0,
            alarm: true,
        },
    ];

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

    dummyData = {
        name: '1好',
        temp: '',
        humi: '',
        thi: '',
        time: '',
        online: false,
        ctrl: 0,
        alarm: true,
    };

    ngOnInit() {
        this.init();
        this.ionViewWillEnter();
    }

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

    getData(time) {
        if (time) {
            this.load.toLoad('加载中...', false);
        }
        this.api.app.getAllRegionData(['', (data) => {
            // 热应激良 热应激差 故障总数
            data.map((value, index) => {
                this.thiGood = value.thiGood;
                this.thiBad = value.thiBad;
                this.alarmTotal = value.alarmTotal;
            });

            // 牛舍列表
            if (data && data.constructor === Array) {
                this.items = [];
                this.isError = false;
                data.map((value, index) => {
                    const itemInfo = {
                        name: value.fieldRegionName,
                        temp: value.temp,
                        humi: value.humi,
                        heatStressLevel: value.heatStressLevel,
                        online: value.online,
                        operationModel: value.operationModel,
                        isWisdomModel: value.isWisdomModel,
                        alarm: value.alarm,
                        ts: value.ts
                    };
                    this.items.push(itemInfo);
                });
            } else {
                this.api.presentMsgToast('获取数据失败');
                this.isError = true;
            }
            this.load.offLoad();
        }]);
    }
    //
    // toControl(one){
    //     const query = '?name=' + one.name + '&no=' + one.no + '&group=' + one.group;
    //     // this.tf.transfer({
    //     //     url: '/' + one.url,
    //     //     query: query,
    //     //     hash: ''
    //     // });
    //     this.nav.navigateForward(one.url + query);
    // }

    toControl(one) {
        // const query = '/env-detail?name=' + one.name + '&no=' + one.no + '&group=' + one.group;
        // this.tf.transfer({
        //     url: '/' + one.url,
        //     query: query,
        //     hash: ''
        // });
        // console.log(query);
        const query = '/env-detail?name=1213';
        this.nav.navigateForward(query);
        // this.nav.navigateRoot(one.url + query);
    }

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

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