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

@Component({
    selector: 'smart-trigger-prototype',
    templateUrl: './trigger-prototype.component.html',
    styles: []
})
export class TriggerPrototypeComponent implements OnInit {

    @Output() done = new EventEmitter<any>();

    isVisiable = false;
    title;
    triggerId;  //阈值id
    itemId;   //监控项ID
    hostName;   //主机name
    itemObj;   //监控项对象

    triggerObj;

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

    ngOnInit() {
        this.initForm();
    }

    initForm() {
        this.triggerObj = {
            expression: '',
            priority: '2',
            description: ''
        };
    }

    //新增
    showAddModal(title, hostName) {
        this.title = title;
        this.isVisiable = true;
        this.hostName = hostName;
    }

    //编辑
    showEditModal(title, id, hostName) {
        this.title = title;
        this.triggerId = id;
        this.isVisiable = true;
        this.hostName = hostName;
        this.overAllSer.findTriggerById(id).subscribe(
            (res) => {
                const data = res.data[0];
                this.triggerObj.expression = data.expression;
                this.triggerObj.description = data.description;
                this.triggerObj.priority = data.priority + '';
            }
        );
    }

    //阈值--start
    saveTrigger() {
        if (!this.checkFun()) {
            this.message.error('请输入阈值');
            return false;
        }
        if (this.title == '新增阈值原型') {
            this.createTrigger();
        }
        if (this.title == '编辑阈值原型') {
            this.updateTrigger();
        }
    }

    //校验是否填值
    checkFun() {
        if (!this.triggerObj.expression) {
            this.message.error('请输入表达式');
            return false;
        }
        return true;
    }

    //1.创建阈值
    createTrigger() {
        this.overAllSer.createTriggerPrototype(this.triggerObj).subscribe(
            (response) => {
                if (response.errCode == 10000) {
                    this.message.success('创建阈值成功');
                    this.initForm();
                    this.done.emit();
                    this.isVisiable = false;
                } else {
                    this.message.error(response.errMsg);
                }
            }
        );
    }

    //2.修改阈值
    updateTrigger() {
        const data = {
            triggerid: this.triggerId,
            expression: this.triggerObj.expression,
            priority: this.triggerObj.priority,
            description: this.triggerObj.description
        };
        this.overAllSer.updateTriggerPrototype(data).subscribe(res => {
            if (res.errCode == 10000) {
                this.message.success('修改阈值成功');
                this.initForm();
                this.done.emit();
                this.isVisiable = false;
            }
        });
    }

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

}
