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-light',
  templateUrl: './env-light.page.html',
  styleUrls: ['./env-light.page.scss'],
})
export class EnvLightPage implements OnInit {

    /* 照明 */
    // 牧场区域编号
    fieldRegionNo = '';
    // 牧场设备类型
    fieldDeviceType = '';
    // 模式状态
    controlModel = 0;
    // 开始日期
    startDate = '';
    // 结束日期
    stopDate = '';
    // 开启时间
    on = '';
    // 关闭时间
    off = '';

    // 各设备信息
    devices = [];
    // 一鍵控制信息
    batchSwitch = {op: 0, type: '照明'};

    controlModelOptions = [
        {modelId: 0, modelValue: '手控模式'},
        {modelId: 1, modelValue: '自控模式'},
        {modelId: 2, modelValue: '智慧控制'}];

    // workModeSetting = {  'workMode' : '1',
    //     'DeviceNo' : '',
    //     'workModeOld' : '1'};

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

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

    lightMode = {
      name: 'July',
      openTime: '19:00',
      closeTime: '05:00'};

    // 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);
  }

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

    getData(time) {
        if (time) {
            this.load.toLoad('加载中...', false);
        }
        this.api.cowshedApp.getZhaoMingData([{'fieldRegionNo' : this.fieldRegionNo}, (data) => {
            if (data) {
                this.fieldRegionNo = data.fieldRegionNo;
                this.fieldDeviceType = data.fieldDeviceType;
                this.controlModel = data.controlModel;
                this.startDate = data.startDate;
                this.stopDate = data.stopDate;
                this.on = data.on;
                this.off = data.off;
                this.devices = data.param;

                // TODO 测试用准备数据
                this.fieldRegionNo = 'FRN136641006052720640';
                this.fieldDeviceType = '照明';
                this.controlModel = 2;
                this.on = '19:00';
                this.off = '05:00';
                this.devices = [{fieldDeviceNo: '136641005998194688', fieldDeviceName: 'A号照明组', state: 1},
                    {fieldDeviceNo: '136641005998194688', fieldDeviceName: 'B号照明组', state: 0},
                    {fieldDeviceNo: '136641005998194688', fieldDeviceName: 'C号照明组', state: 1},
                    {fieldDeviceNo: '136641005998194688', fieldDeviceName: 'D号照明组', state: 0},
                    {fieldDeviceNo: '136641005998194688', fieldDeviceName: 'E号照明组', state: 1},
                    {fieldDeviceNo: '136641005998194688', fieldDeviceName: 'F号照明组', state: 1}];

                // 获取一键控制信息
                this.allCheck(this.batchSwitch);
            }
        }]);

    }

    allCheck(batchSwitch) {
        batchSwitch.type = this.fieldDeviceType;

        const req = {
            fieldRegionNo: this.fieldRegionNo,
            op: batchSwitch.op, // op: batchSwitch.op ? 0 : 1,
            fieldDeviceType: batchSwitch.type,
            ts: parseInt((new Date().getTime() / 1000).toString(), 10)
        };

        this.comm.remain = 120;
        this.api.control.controlBatchSwitch([req, (data) => {
            if (data.code === 1) {
                this.batchSwitch.op = data.op;
                this.api.presentMsgToast(data.message);
            } else {
                this.api.presentMsgToast(data.message);
            }
            this.comm.remain = 0;
            // batchSwitch.state = false;
        }]);
        // TODO 测试用准备数据
        this.batchSwitch.op = 0;
    }

}
