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

@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;
    loading2 = false;
    loading3 = false;
    _inv;

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

    ngOnInit() {
    }

    // ionic的生命周期函数
    ionViewWillEnter () {
        this.nanClicked = false;
        this.beiClicked = false;
        this.nanLoading = false;
        this.beiLoading = false;
        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;
            } else if (index === 2) {
                this.loading2 = true;
                const req = [{
                    data: {
                        d0: 1,
                        startTime: +new Date(),
                        ts: +new Date()
                    },
                    sn: '19041005',
                    type: 'WTDR66C'
                }];
                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.loading2 = false;
                    this.load.offLoad();
                }]);
            } else if (index === 3) {
                this.loading3 = true;
                const req = [{
                    data: {
                        d2: 1,
                        startTime: +new Date(),
                        ts: +new Date()
                    },
                    sn: '19041005',
                    type: 'WTDR66C'
                }];
                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.loading3 = false;
                    this.load.offLoad();
                }]);
            }
        }
    }

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

    confirm(index) {
        this.load.toLoad('发送指令中,等待指令返回...', true);
        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,
                    startTime: +new Date(),
                    ts: +new Date()
                },
                sn: '19041004',
                type: 'WTDR66C'
            }];
        }
        this.nanClicked = false;
        this.beiClicked = false;
        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;
            this.loading2 = false;
            this.loading3 = false;
            this.load.offLoad();
        }]);
    }

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

    getData(time) {
        if (time) {this.load.toLoad('加载中...', false); }
        // 东中转池 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('获取数据失败');
            }
            if (time) {this.load.offLoad(); }
            this._inv = setTimeout(() => {
                this.getData(0);
            }, 10000);
        }]);
        // 东中转池 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('获取数据失败');
            }
        }]);
    }
}
