import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validator, Validators} from '@angular/forms';
import {OverAllService} from '../../overAll/overAll.service';
import {BasicKeyComponent} from '../basic-key/basic-key.component';
import {NewTypeComponent} from '../new-type/new-type.component';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
  selector: 'smart-discovery',
  templateUrl: './discovery.component.html',
  styles: []
})
export class DiscoveryComponent implements OnInit {
    @ViewChild('basicKey') basicKey: BasicKeyComponent;
    @ViewChild('newType') newType: NewTypeComponent;
    @Output() done = new EventEmitter<any>();

    title;
    isVisiable = false;
    isOkLoading = false;
    nzSize = 'large';
    hostId;   //主机ID
    itemId;  //监控项id
    valueMapList = [];
    validateForm: FormGroup;
    checkList;  //监测点分类
    interval;    //间隔时长

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

    ngOnInit() {
        this.initForm();
    }

    initForm() {
        this.validateForm = this.fb.group({
            hostid: [this.hostId],
            name: [null, [Validators.required]],
            type: [null, [Validators.required]],
            key_: [null, [Validators.required]],
            snmp_oid: ['interfaces.ifTable.ifEntry.ifInOctets.1'],
            snmp_community: ['public'],
            port: [this.hostId],
            delay: [null, [Validators.required]],
            description: [null],
            lifetime: ['3600'],
            status: [null],   //0启用 1禁用
        });
    }

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

    //编辑
    showEditModal(hostId, discoveryid,title) {
        this.title = title;
        this.hostId = hostId;
        this.isVisiable = true;
        this.itemId = discoveryid;
        this.overAllSer.findLldrule(discoveryid).subscribe(
            (res) => {
                const data = res.data[0];
                data.type += '';
                data.value_type += '';
                data.status =  data.status == 0 ? true : false;
                this.validateForm.patchValue(data);
            }
        );
    }

    //添加
    handleCheckOk() {
        for (let i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if (this.validateForm.invalid) {
            this.message.error('请输入必填信息');
            return false;
        }
        let d =  this.validateForm.value.status == true ? 0 : 1;
        this.validateForm.patchValue({
            status:d,
            hostid:this.hostId
        });
        this.isOkLoading = true;
        if (this.title == '添加自动发现') {
            this.create();
        }
        if (this.title == '编辑自动发现') {
            this.update();
        }
    }

    create() {
        this.overAllSer.createLldrule(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('创建自动发现成功');
                    this.done.emit();
                    this.isVisiable = false;
                }
                this.isOkLoading = false;
            },
            (err) => {
                this.message.error('系统错误');
            }
        );
    }

    //修改监控项
    update() {
        this.validateForm.addControl('itemid', new FormControl(this.itemId));
        this.overAllSer.updateLldrule(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('修改自动发现成功');
                    this.done.emit();
                    this.isVisiable = false;
                }
                this.isOkLoading = false;
            },
            (err) => {
                this.message.error('系统错误');
            }
        );
    }

    handleCheckCancel(): void {
        this.isVisiable = false;
        this.isOkLoading = false;
        this.initForm();
    }

}
