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

@Component({
    selector: 'v-east',
    templateUrl: './east.page.html',
    styleUrls: ['./east.page.scss'],
})
export class EastPage implements OnInit {
    value = [false, false, false, false, false, false, false];
    yewei = 0;
    temp = '-';
    proc = 0;
    duration = 23;
    nanClicked = false;
    beiClicked = false;
    nanLoading = false;
    beiLoading = false;

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

    ngOnInit() {
    }

    // ionic的生命周期函数
    ionViewWillEnter () {
        this.nanClicked = false;
        this.beiClicked = false;
        this.nanLoading = false;
        this.beiLoading = false;
        this.getData();
    }

    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();
                this.api.presentMsgToast(data.message);
            } else {
                this.api.presentMsgToast(data.message);
            }
            this.nanLoading = false;
            this.beiLoading = false;
        }]);
    }

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

    getData() {
        // 东中转池 19041001 0号通道为温度
        this.api.data.getRealData([{sn: '19041001', type: 'WTDR14P'}, (data) => {
            if (data && data.constructor === Array) {
                if (data.length > 0) {
                    this.temp = Number(data[0].value).toFixed(2);
                }
            } else {
                this.api.presentMsgToast('获取数据失败');
            }
        }]);
        // 东中转池 19041003 0号通道为液位
        this.api.data.getRealData([{sn: '19041003', type: 'WTDR18X'}, (data) => {
            if (data && data.constructor === Array) {
                if (data.length > 0) {
                    if (data[0].value) {
                        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([{sn: '19041004', type: 'WTDR66C'}, (data) => {
            if (data && data.constructor === Array) {
                if (data.length > 0) {
                    if (data[0]) {
                        this.value[0] = !!(data[0].value === '1.0');
                    }
                    if (data[2]) {
                        this.value[1] = !!(data[2].value === '1.0');
                    }
                }
                this.value = [...this.value];
            } else {
                this.api.presentMsgToast('获取数据失败');
            }
        }]);
        // 东中转池 19041005 0,1号通道为1号搅拌池 2,3号通道为2号搅拌池
        this.api.data.getRealData([{sn: '19041005', 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('获取数据失败');
            }
        }]);
    }
}
