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';
import {Network} from '@ionic-native/network/ngx';


@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 network: Network,
                private api: Api, private load: Load, public nav: NavController) {
    }
    @ViewChild('slide') slide;

    isError = false;
    ifFocus = false;
    searchText = '';

    // 发送指令延时判断
    comm = {
        remain: 20,
        isLoading: true
    };

    imgs = ['1.jpg', '2.jpg', '3.jpg'];
    lunbo = {
        speed: 1000,
        allowTouchMove: true,
        effect: 'flip',
        autoplay: {
            delay: 2000,
        },
        loop: true,
        spaceBetween: 0,
        zoom: false,
        height: 150,
    };
    // 热应激良
    thiGood = 0;
    // 热应激差
    thiBad = 0;
    // 故障总数
    alarmTotal = 0;
    // 牛舍列表
    items = [];
    displayItems = [];

    getItems(e) {
        this.items = this.displayItems;
        this.comm.isLoading = false;
        this.comm.remain = 1000;
        this.searchText = e.target.value;
        const val = e.target.value;
        if (val && val.trim() !== '') {
            this.items = this.items.filter((item) => {
                const name = item.fieldRegionName;
                return (name.toLowerCase().indexOf(this.searchText.toLowerCase()) > -1);
            });
        }
    }

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

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

    getData(time) {
        if (this.isNetConnected() === true) {
            if (time) {
                this.load.toLoad('加载中...', false);
            }
            this.api.cowshedApp.getAllRegionData(['', (data) => {
                // 热应激良 热应激差 故障总数
                if (data) {
                    this.thiGood = data.thiGood;
                    this.thiBad = data.thiBad;
                    this.alarmTotal = data.alarmTotal;
                    // 牛舍列表
                    if (data.resultIndexDataListForAppVos && data.resultIndexDataListForAppVos.constructor === Array) {
                        this.isError = false;
                        data.resultIndexDataListForAppVos.map((value, index) => {
                            const itemInfo = {
                                fieldRegionNo: value.fieldRegionNo,
                                fieldRegionName: value.fieldRegionName,
                                temp: value.temp,
                                humi: value.humi,
                                heatStressLevel: value.heatStressLevel,
                                heatStressLevelName: this.getHeatStressLevelName(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.displayItems = this.items;

                    // --------------------------------------------------------------------
                    // TODO 测试用准备数据
                    this.thiGood = 1;
                    this.thiBad = 12;
                    this.alarmTotal = 5;
                    this.items = [
                        {
                            // 0-无热应激 离线 无报警
                            fieldRegionNo: 'FRN136641006052720640',
                            fieldRegionName: '1号牛舍',
                            temp: '23.5',
                            humi: '82',
                            heatStressLevel: '0',
                            heatStressLevelName: this.getHeatStressLevelName('0'),
                            online: 0, // 0-离线，1-在线
                            alarm: 0,
                            ts: '2019-08-05 23:44',
                        },
                        {
                            // 1-轻度热应激 在线 现控 无报警
                            fieldRegionNo: 'FN108050436279648256',
                            fieldRegionName: '2号牛舍',
                            temp: '23.5',
                            humi: '82',
                            heatStressLevel: '1',
                            heatStressLevelName: this.getHeatStressLevelName('1'),
                            online: 1, // 0-离线，1-在线
                            operationModel: 0,  // 0-现控，1-平控
                            alarm: 0,
                            ts: '2019-08-05 23:44',
                        },
                        {
                            // 2-中度热应激 在线 平控 非智控(手控) 有报警
                            fieldRegionNo: 'FN108050436279648256',
                            fieldRegionName: '3号牛舍',
                            temp: '23.5',
                            humi: '82',
                            heatStressLevel: '2',
                            heatStressLevelName: this.getHeatStressLevelName('2'),
                            online: 1, // 0-离线，1-在线
                            operationModel: 1,  // 0-现控，1-平控
                            isWisdomModel: 0,  // 0-非智控(手控)，1-智控
                            alarm: 1,
                            ts: '2019-08-05 23:44',
                        },
                        {
                            // 3-重度热应激 在线 平控 非智控(自控) 有报警
                            fieldRegionNo: 'FN108050436279648256',
                            fieldRegionName: '4号牛舍',
                            temp: '23.5',
                            humi: '82',
                            heatStressLevel: '3',
                            heatStressLevelName: this.getHeatStressLevelName('3'),
                            online: 1, // 0-离线，1-在线
                            operationModel: 1,  // 0-现控，1-平控
                            isWisdomModel: 0,  // 0-非智控(自控)，1-智控
                            alarm: 1,
                            ts: '2019-08-05 23:44',
                        },
                        {
                            // 4-II重度热应激 在线 平控 智控 有报警
                            fieldRegionNo: 'FN108050436279648256',
                            fieldRegionName: '测试用5号牛舍',
                            temp: '23.5',
                            humi: '82',
                            heatStressLevel: '4',
                            heatStressLevelName: this.getHeatStressLevelName('4'),
                            online: 1, // 0-离线，1-在线
                            operationModel: 1,  // 0-现控，1-平控
                            isWisdomModel: 1,  // 0-非智控(自控)，1-智控
                            alarm: 1,
                            ts: '2019-08-05 23:44',
                        },
                    ];
                    this.displayItems = this.items;
                    // --------------------------------------------------------------------
                }
                this.load.offLoad();
            }]);
        }
    }

    toControl(one) {
        const query = '/env-detail?no=' + one.fieldRegionNo;
        this.tf.transfer({
            url: '/' + one.url,
            query: query,
            hash: ''
        });
        this.nav.navigateForward(query);

        /*const query = '/env-detail?no=' + one.fieldRegionNo;
        // this.tf.transfer({
        //     url: '/' + one.url,
        //     query: query,
        //     hash: ''
        // });
        console.log(one.url + query);
        this.nav.navigateForward(one.url + query);*/
    }

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

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

    getHeatStressLevelName(codeId) {
        let levelName = '';
        switch (codeId) {
            case '0':
                levelName = '无';
                break;
            case '1' :
                levelName = '轻';
                break;
            case '2' :
                levelName = '中';
                break;
            case '3' :
                levelName = '重';
                break;
            case '4' :
                levelName = 'II重';
                break;
            default:
                break;
        }
        return levelName;
    }

    doRefresh(event) {
        setTimeout(() => {
            this.getData(0);
            event.target.complete();
        }, 2000);
    }

    isNetConnected() {
        if (this.network.type === 'none') {
            this.api.presentMsgToast('网络连接失败,请确认连接状况');
            return false;
        }
        return true;
    }
}
