import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {OverAllService} from '../../overAll/overAll.service';
import {FormBuilder} from '@angular/forms';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
    selector: 'smart-threshold',
    templateUrl: './threshold.component.html',
    styles: []
})
export class ThresholdComponent implements OnInit {
    @Output() done = new EventEmitter<any>();

    isVisiable = false;
    isOkLoading = false;
    title;
    itemId;   //监控项ID
    hostName;   //主机name
    itemObj;   //监控项对象
    conditionType = 'high';
    faultConditionList = [];   //故障阈值
    conditionList = [];    //危险阈值

    triggerConditionId;
    triggerFaultConditionId;

    triggerObj = {
        condition: '',
        faultCondition: '',
        itemName: ''
    };
    demo = `示例1.{监测点id}>10 or {监测点id}=1  示例2. {监测点id}<10 and {监测点id}>3`;

    constructor(private overAllSer: OverAllService, private fb: FormBuilder,
                private message: NzMessageService) {
    }

    ngOnInit() {
    }

    initForm() {
        this.faultConditionList = [];   //危险阀值list
        this.conditionList = [];    //故障list
        this.triggerObj = {
            condition: '',
            faultCondition: '',
            itemName: ''
        };
    }

    //危险add
    addFault() {
        const demo = {
            and: ' and ',
            equal: '=',
            value: '',
        };
        this.faultConditionList.push(demo);
    }

    //危险add
    addcondition() {
        const demo = {
            and: ' and ',
            equal: '=',
            value: '',
        };
        this.conditionList.push(demo);
    }

    //故障delete
    deleteFault(index) {
        this.faultConditionList.splice(index, 1);
    }

    //危险delete
    deleteCondition(index) {
        this.conditionList.splice(index, 1);
    }

    //新增
    showAddModal(item, title, itemId, hostName) {
        this.title = title;
        this.itemId = itemId;
        this.isVisiable = true;
        this.hostName = hostName;
        this.itemObj = item;
        this.overAllSer.findItemDetail(this.itemId).subscribe(
            (res) => {
                this.itemObj = res.data;
            }
        );
    }

    //编辑
    showEditModal(title, itemId, hostId, hostName) {
        this.title = title;
        this.itemId = itemId;
        this.isVisiable = true;
        this.hostName = hostName;
        this.overAllSer.findItemDetail(this.itemId).subscribe(
            (res) => {
                this.itemObj = res.data;
                this.itemObj.triggers.forEach((value) => {
                    if (value.priority == 2 || value.priority == 3) {     //危险
                        this.triggerObj.condition = value.expression;
                        this.triggerConditionId = value.id;
                    }
                    if (value.priority == 4 || value.priority == 5) {     //故障
                        this.triggerObj.faultCondition = value.expression;
                        this.triggerFaultConditionId = value.id;
                    }
                });
            }
        );
    }

    //阈值--start
    saveTrigger() {
        const data = {
            condition: '',
            faultCondition: ''
        };
        if (this.conditionType == 'commonly') {   //常规
            this.conditionList.forEach(res => {
                if (!res.and) {
                    res.and = '';
                }
                data.condition += res.and + '{' + this.hostName + ':' + this.itemObj.key + '.last()}' + res.equal + '' + res.value;
            });
            this.faultConditionList.forEach(res => {
                if (!res.and) {
                    res.and = '';
                }
                data.faultCondition += res.and + '{' + this.hostName + ':' + this.itemObj.key + '.last()}' + res.equal + '' + res.value;
            });
        } else {           //高级
            data.condition = this.triggerObj.condition;
            data.faultCondition = this.triggerObj.faultCondition;
        }
        if (!this.checkFun() && !this.triggerObj.condition && !this.triggerObj.faultCondition) {
            this.message.error('请输入阈值');
            return false;
        }

        this.isOkLoading = true;

        if (this.title == '添加阈值') {
            // if (this.conditionList.length == 0 && this.faultConditionList.length == 0) {
            //     this.isVisiable = false;
            //     this.initForm();
            //     return false;
            // }else{
            this.createTrigger(data);
            // }
        }
        if (this.title == '编辑阈值') {
            this.updateTrigger(data);
        }
    }

    //校验是否填值
    checkFun() {
        let error = true;
        this.conditionList.map(e => {
            if (e.value.length > 0) {
            } else {
                error = false;
            }
        });
        this.faultConditionList.map(e => {
            if (e.value.length > 0) {
            } else {
                error = false;
            }
        });

        return error;
    }

    //1.创建阈值
    createTrigger(data) {
        const res = {
            itemName: this.itemObj.name,
            faultCondition: data.faultCondition,
            condition: data.condition,
            itemid: this.itemId
        };
        this.overAllSer.createTrigger(res).subscribe(
            (response) => {
                if (response.errCode == 10000) {
                    this.message.success('创建阈值成功');
                    this.initForm();
                    this.done.emit();
                    this.isVisiable = false;
                } else {
                    this.message.error(response.errMsg);
                }
                this.isOkLoading = false;
            }
        );
    }

    //2.修改阈值
    updateTrigger(data) {
        const condition = {
            id: this.triggerConditionId,
            itemid: this.itemId,
            expression: data.condition,
        };
        const faultCondition = {
            id: this.triggerFaultConditionId,
            itemid: this.itemId,
            expression: data.faultCondition,
        };
        if (this.triggerConditionId) {
            this.overAllSer.updateTrigger(condition).subscribe(res => {
                if (res.errCode == 10000) {
                    this.initForm();
                    this.done.emit();
                    this.isOkLoading = false;
                    this.isVisiable = false;
                }
            });
        }
        if (this.triggerFaultConditionId) {
            this.overAllSer.updateTrigger(faultCondition).subscribe(res => {
                if (res.errCode == 10000) {
                    this.initForm();
                    this.done.emit();
                    this.isVisiable = false;
                }
                this.isOkLoading = false;
            });
        }
    }

    //取消
    handleCheckCancel() {
        this.isVisiable = false;
        this.initForm();
    }

}
