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;   //监控项对象

    triggerConditionId;
    triggerFaultConditionId;

    triggerObj = {
        condition: null,
        faultCondition: null,
        itemName: ''
    };

    conditionExpression;  //严重
    faultConditionExpression;  //告警

    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.isOkLoading = false;
        this.conditionExpression = '';
        this.faultConditionExpression = '';
        this.triggerObj = {
            condition: null,
            faultCondition: null,
            itemName: ''
        };
    }

    //新增
    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, item, itemId, hostId, hostName) {
        this.title = title;
        this.itemId = itemId;
        this.isVisiable = true;
        this.hostName = hostName;
        item.triggers.forEach((value) => {
            if (value.priority == 1) {     //告警
                this.triggerObj.condition = value;
                this.conditionExpression = value.expression;
                this.triggerConditionId = value.id;
            }
            if (value.priority == 2) {     //严重
                this.triggerObj.faultCondition = value;
                this.faultConditionExpression = value.expression;
                this.triggerFaultConditionId = value.id;
            }
        })
    }

    //2.修改阈值
    saveTrigger() {
        if (!this.conditionExpression && !this.faultConditionExpression) {
            this.message.error('请输入阈值');
            return false;
        }

        if (!this.triggerObj.condition && this.conditionExpression) { //告警触发器创建
            const res = {
                itemName: this.itemObj.name,
                condition: this.conditionExpression,
                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;
                }
            );
        }
        if (this.triggerObj.condition) { //告警触发器更新
            this.triggerObj.condition.expression = this.conditionExpression;
            this.overAllSer.updateTrigger(this.triggerObj.condition).subscribe(res => {
                if (res.errCode == 10000) {
                    this.message.success('更新告警阈值成功');
                    this.initForm();
                    this.done.emit();
                    this.isVisiable = false;
                }
                this.isOkLoading = false;
            });
        }
        if (!this.triggerObj.faultCondition && this.faultConditionExpression) {  //严重触发器创建
            const res = {
                itemName: this.itemObj.name,
                faultCondition: this.faultConditionExpression,
                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;
                }
            );
        }
        if (this.triggerObj.faultCondition) {  //严重触发器更新
            this.triggerObj.faultCondition.expression = this.faultConditionExpression;
            this.overAllSer.updateTrigger(this.triggerObj.faultCondition).subscribe(res => {
                if (res.errCode == 10000) {
                    this.message.success('更新严重阈值成功');
                    this.initForm();
                    this.done.emit();
                    this.isVisiable = false;
                }
                this.isOkLoading = false;
            });
        }
    }

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

}
