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

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

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

    item = {};

    fieldRegionNo = '';

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

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

    getData(time) {
        if (time) {
            this.load.toLoad('加载中...', false);
        }
        this.api.cowshedApp.getRealDataByFieldRegionNo([{'fieldRegionNo' : this.fieldRegionNo}, (data) => {

            if (data) {
                this.item = data;
                // TODO 测试用准备数据
                this.item = {
                    // 3-重度热应激 在线 平控 非智控(自控) 有报警
                    fieldRegionName: '测试用4号牛舍',
                    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',
                };
            }
        }]);
    }

    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 = '在舍';
            default:
                break;
        }
        return stateName;
    }
}
