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

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

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

    // 牧场区域编号
    fieldRegionNo = '';
    // 牧场设备类型
    fieldDeviceType = '';
    // 模式状态
    controlModel = 0;
    // 供液泵状态
    bump = {online: true, press: 0};
    // 时间参数
    controlConfig = {
        name: '',
        interval: 0, // 间隔时间
        a: '', // A区喷淋时间
        t: '',
        b: '', // A区喷淋时间
    };
    // 各设备信息
    param = [];

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

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

    ngOnInit() {
        this.route.paramMap.subscribe(params => {this.fieldRegionNo = params.get('fieldRegionNo'); });
        this.getData(0);
    }

    getData(time) {
        if (time) {
            this.load.toLoad('加载中...', false);
        }
        this.api.cowshedApp.getPenLinData([{'fieldRegionNo' : this.fieldRegionNo}, (data) => {
            if (data) {
                this.fieldRegionNo = data.fieldRegionNo;
                this.fieldDeviceType = data.fieldDeviceType;
                this.controlModel = data.controlModel;
                this.bump = data.bump;
                this.controlConfig = data.controlConfig;
                this.param = data.param;
            }
        }]);
    }

    // 单个设备控制
    deviceCheck(btn) {
        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;
        }]);
    }

}
