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

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

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

    item: any = {};

    fieldRegionNo = '';
    loading = false;


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

    // fan = '/env-detail/fan';
    showerLink = '';
    lightLink = '';
    waterchannelLink = '';

    ngOnInit() {
        this.init();
    }

    init() {
        this.comm.isLoading = false;
        this.comm.remain = 0;
        this.ac.queryParams.subscribe((data) => {
            this.fieldRegionNo = data.no;
        });
        this.getData(0);
    }

    initLayout() {
        this.showerLink = '/env-detail/shower/' + this.fieldRegionNo;
        this.lightLink = '/env-detail/light/' + this.fieldRegionNo;
        this.waterchannelLink = '/env-detail/waterchannel/' + this.fieldRegionNo;
        if (this.item && this.item.heatStressLevel && this.item.heatStressLevel !== '') {
            this.item.heatStressColor = this.item.heatStressLevel === '0' ? '#71fa1d' :
                this.item.heatStressLevel === '1' ? '#71fa1d' :
                    this.item.heatStressLevel === '2' ? '#FAB139' :
                        this.item.heatStressLevel === '3' ? '#ff6a1f' : '#b22222';
        }
    }

    getData(time) {
        if (time) {
            this.load.toLoad('加载中...', false);
        }
        // this.api.cowshedApp.getRealDataByFieldRegionNo([{'fieldRegionNo' : this.fieldRegionNo}, (data) => {
        //     if (data) {
        //         this.item = data;
        //         this.initLayout();
        //     }
        // }]);
        // ----------------------------------------------
        // TODO 测试用准备数据
        this.item = {
            // 3-重度热应激 在线 平控 非智控(自控) 有报警
            fieldRegionNo: 'FRN136641006052720640',
            fieldRegionName: '1号牛舍',
            temp: '23.5',
            humi: '82.8',
            heatStressLevel: '3',
            heatStressLevelName: this.getHeatStressLevelName('3'),
            online: 1, // 0-离线，1-在线
            operationModel: 1,  // 0-现控，1-平控
            isWisdomModel: 0,  // 0-非智控(自控)，1-智控
            alarm: 1,
            inHouse : this.getInHouseState(1), // 0-离舍，1-在舍
            ts: '2019-08-05 23:44',
        };
        this.initLayout();
        // ----------------------------------------------
    }

    getHeatStressLevelName(id) {
        let levelName = '';
        switch (id) {
            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;
    }

    getInHouseState(id) {
        let stateName = '';
        switch (id) {
            case 0:
                stateName = '离舍 ';
                break;
            case 1:
                stateName = '在舍';
                break;
            default:
                break;
        }
        return stateName;
    }

    toHistory(item) {
        const query = 'env-history?no=' + item.fieldRegionNo;
        this.nav.navigateForward(query);
    }

    setInHouse() {
        this.loading = true;
        // TODO 在离舍开关控制
        this.loading = false;
    }

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