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

@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
    };

    commTmr: any;

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

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

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

    ngOnInit() {
        this.comm.remain = 0;
        this.comm.isLoading = false;
        this.commTmr = window.setInterval(() => {

            if (this.comm.remain > 0) {
                this.comm.remain = this.comm.remain - 1;
            }

            if ( this.comm.remain < 1 ) {
                this.comm.isLoading = false;
            } else {
                this.comm.isLoading = true;
            }
        }, 1000) ;
        this.init();
    }

    // ionViewWillEnter() {
    //     console.log('ionViewWillEnter_shower');
    //     this.init();
    // }
    ionViewWillLeave() {
        clearInterval(this.commTmr);
    }

    init() {
        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;
                this.param.forEach(device => {
                    device.op = device.cycle === 1 ? true : false;
                });
                this.initButtons();
            }
            this.load.offLoad();
        }]);
    }

    initButtons() {
        this.param.forEach((device) => {
            // 远控的场合  小按钮活性(disabled = false)
            device.disabled = !(this.controlModel === Constants.CONTROLL_REMOTR);
        });
    }

    // 单个设备控制
    deviceCheck(btn) {
        const req = {
            fieldDeviceNo: btn.fieldDeviceNo,
            fieldDeviceType: this.fieldDeviceType,
            op: btn.op ? 0 : 1,
            ts: parseInt((new Date().getTime() / 1000).toString(), 10)
        };
        this.comm.remain = 20;
        this.api.control.controlCowedSwitch([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);
            }
            // this.getData(0);
            // this.comm.remain = 0;
            // this.api.presentMsgToast(data.message);
            btn.loading = false;
        }]);
    }
}
