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

@Component({
  selector: 'v-west',
  templateUrl: './west.component.html',
  styleUrls: ['./west.component.scss'],
})
export class WestPage implements OnInit {
  value = [false, false, false, false, false, false, false];
  yewei = 0;
  temp: any = '';
  proc = 0;
  duration = 15;
  nanClicked = false;
  beiClicked = false;
  nanLoading = false;
  beiLoading = false;
  _inv;

  constructor(private tf: Transfer, private api: Api, private load: Load) {
  }

  ngOnInit() {
  }

  // ionic的生命周期函数
  ionViewWillEnter () {
    this.cancel();
    this.getData(1);
  }

  ionViewWillLeave() {
    // 清掉计数器
    clearTimeout(this._inv);
  }

  toggle(index) {
    if (!(this.beiLoading || this.nanLoading || this.value[0] || this.value[1])) {
      if (index === 0) {
        this.nanClicked = true;
        this.nanLoading = true;
      } else if (index === 1) {
        this.beiClicked = true;
        this.beiLoading = true;
      }
    }
  }

  cancel() {
    this.nanClicked = false;
    this.beiClicked = false;
    this.nanLoading = false;
    this.beiLoading = false;
  }

  confirm(index) {
    let req;
    if (index) {
      req = [{
        data: {
          d2: 1,
          duration: this.duration,
          startTime: +new Date(),
          ts: +new Date()
        },
        sn: '19041004',
        type: 'WTDR66C'
      }];
    } else {
      req = [{
        data: {
          d0: 1,
          duration: this.duration,
          startTime: +new Date(),
          ts: +new Date()
        },
        sn: '19041004',
        type: 'WTDR66C'
      }];
    }
    this.nanClicked = false;
    this.beiClicked = false;
    this.api.presentMsgToast('已成功发送指令,请耐心等待');
    this.api.data.updateControl([req, (data) => {
      if (data && data.code === 1) {
        this.getData(1);
        this.api.presentMsgToast(data.message);
      } else {
        this.api.presentMsgToast(data.message);
      }
      this.nanLoading = false;
      this.beiLoading = false;
    }]);
  }

  onLeftClick() {
    this.tf.transfer({url: '/home'});
  }

  getData(time) {
    if (time) {this.load.toLoad('加载中...', false); }
    // 西中转池 19041006 0号通道为液位
    this.api.data.getRealData([{sn: '19041006', type: 'WTDR18X'}, (data) => {
      if (data && data.constructor === Array) {
        if (data[0].value) {
          if (data.length > 0) {
            data[0].value = Number(data[0].value) > 0 ? data[0].value : '0';
            data[0].value = Number(data[0].value) <= 6.5 ? data[0].value : '6.5';
            this.yewei = 6.5 - Number(data[0].value);
            this.proc = 100 - (Number(data[0].value) * 100 / 6.5);
          }
        }
      } else {
        this.api.presentMsgToast('获取数据失败');
      }
    }]);
    // 西中转池 19041004 0,1号通道为南水 2,3号通道为北水
    // this.api.data.getRealData([{deviceNo: '92489787927302144'}, (data) => {
    //   if (data && data.constructor === Array) {
    //     if (data[0] && data[1]) {
    //       this.value[0] = !!(data[0].value === '1.0' && data[1].value === '1.0');
    //       this.color[0] = (data[0].value === '1.0' && data[1].value === '1.0') ? '#28c9bd' : this.color[0];
    //     }
    //     if (data[2] && data[3]) {
    //       this.value[1] = !!(data[2].value === '1.0' && data[3].value === '1.0');
    //       this.color[1] = (data[2].value === '1.0' && data[3].value === '1.0') ? '#28c9bd' : this.color[1];
    //     }
    //     this.value = [...this.value];
    //   } else {
    //     this.api.presentMsgToast('获取数据失败');
    //   }
    // }]);
    // 西中转池 19041008 0,1号通道为1号搅拌池 2,3号通道为2号搅拌池
    this.api.data.getRealData([{sn: '19041008', type: 'WTDR66C'}, (data) => {
      if (data && data.constructor === Array) {
        if (data.length > 0) {
          if (data[0]) {
            this.value[2] = !!(data[0].value === '1.0');
          }
          if (data[2]) {
            this.value[3] = !!(data[2].value === '1.0');
          }
        }
        this.value = [...this.value];
      } else {
        this.api.presentMsgToast('获取数据失败');
      }
      if (time) {this.load.offLoad()}
      this._inv = setTimeout(() => {
        this.getData(0);
      }, 10000);
    }]);
  }

}
