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

@Component({
  selector: 'v-env-fan',
  templateUrl: './env-fan.page.html',
  styleUrls: ['./env-fan.page.scss'],
})
export class EnvFanPage implements OnInit {
    /* 风机 */
    // 模式状态
    controlModel = '';
    // 设备信息
    devices = [];


  // 工作模式
  workModeSetting = {  'workMode' : '1',
      'DeviceNo' : '',
      'workModeOld' : '1'};

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

    allBtns = [{v:1}];
    fieldRegionNo = '';
    fieldRegionName = '';

    workarea = {
        param: [
        {
            name: '1号风机组',
            runningState:true,  // 是否开启
        },
        {
            name: '2号风机组',
            runningState:false,
        }
        ]
    };

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

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

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

            if (data) {
                this.controlModel = data.controlModel;
                this.devices = data.param;
            }
        }]);
    }

    toggle(a) {
        // console.log("123");
    }

    allCheck(a) {
        a.d = true;
        const req = {
            fieldRegionNo: this.fieldRegionNo,
            op: a.v ? 0 : 1,
            type: a.t,
            ts: parseInt((new Date().getTime() / 1000).toString(), 10)
        };
        this.comm.remain = 120;
        this.api.control.controlBatchSwitch([req, (data) => {
            if (data.code === 1) {
                this.getData(1);
                this.api.presentMsgToast(data.message);
            } else {
                this.api.presentMsgToast(data.message);
            }
            this.comm.remain = 0;
            a.d = false;
        }]);
    }

}
