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

@Component({
  selector: 'smart-tri-modal',
  templateUrl: './tri-modal.component.html',
  styles: []
})
export class TriModalComponent 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: '',
            comments:''
        };
    }

    //新增
    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;
        const data = {
            triggerid:id
        }
        this.overAllSer.findTri(data).subscribe(
            (res) => {
                const data = res.data[0];
                this.triggerObj.expression = data.expression;
                this.triggerObj.description = data.description;
                this.triggerObj.comments = data.comments;
                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;
        }
        if (!this.triggerObj.description) {
            this.message.error('请输入名称');
            return false;
        }
        return true;
    }

    //1.创建阈值
    createTrigger() {
        this.overAllSer.createTri(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,
            comments: this.triggerObj.comments
        };
        this.overAllSer.updateTri(data).subscribe(res => {
            if (res.errCode == 10000) {
                this.message.success('修改阈值成功');
                this.initForm();
                this.done.emit();
                this.isVisiable = false;
            } else {
                this.message.error(res.errMsg);
            }
        });
    }

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

}
