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

@Component({
  selector: 'v-env-fan',
  templateUrl: './env-fan.page.html',
  styleUrls: ['./env-fan.page.scss'],
})
export class EnvFanPage implements OnInit {
    /* 风机 */
    // 牧场区域编号
    fieldRegionNo = '';
    // 牧场设备类型
    fieldDeviceType = '';
    // 模式状态
    controlModel = 0;

    // 各设备信息
    devices = [];
    // 一鍵控制按钮
    batchSwitch = {op: undefined, type: undefined, load: undefined, disabled: undefined};

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

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

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

    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.fieldRegionNo = data.fieldRegionNo;
                this.fieldDeviceType = data.fieldDeviceType;
                this.controlModel = data.controlModel;
                this.devices = data.param;

                // --------------------------------------------------------------------
                // TODO 测试用准备数据
                // this.fieldRegionNo = 'FRN136641006052720640';
                this.fieldDeviceType = '风机';
                this.controlModel = 0;
                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}];
                // --------------------------------------------------------------------

                // [手控(0)]的状态下:活性(false)  以外:非活性(true)
                const allDisabled = this.controlModel === 0 ? false : true;
                this.batchSwitch.disabled = allDisabled;
                this.devices.forEach((device, i) => {
                    device.disabled = allDisabled;
                });

                // 设置[一键控制]按钮(默认: 开启)
                // 当任意子按钮[关闭]的状态下: 关闭  以外:开启
                this.batchSwitch.op = 1;
                this.devices.map((device) => {
                    if (device.state === 0) {
                        this.batchSwitch.op = 0;
                    }
                });
            }
        }]);

    }
    // 一键控制
    allCheck(btn) {
        btn.load = true;
        const req = {
            fieldRegionNo: this.fieldRegionNo,
            op: btn.op ? 0 : 1,
            type: this.fieldDeviceType,
            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;
            btn.load = false;
        }]);

        // 子按钮状态
        this.devices.forEach((device, i) => {
            device.state = btn.op ? 0 : 1;
        });
    }

    // 单个设备控制
    deviceCheck(btn) {
        btn.loading = true;
        const req = {
            fieldDeviceNo: btn.fieldDeviceNo,
            op: btn.state ? 0 : 1,
            ts: parseInt((new Date().getTime() / 1000).toString(), 10)
        };
        this.comm.remain = 20;
        this.api.control.controlSwitch([req, (data) => {
            if (data && data.code === 1) {
                this.getData(1);
                this.comm.remain = 0;
                this.api.presentMsgToast(data.message);
            } else {
                this.comm.remain = 0;
                this.api.presentMsgToast(data.message);
            }
            btn.loading = false;
        }]);
    }
}
