import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {OverAllService} from '../../overAll/overAll.service';
import {pageSize} from '../../app.constants';

@Component({
  selector: 'smart-select-trigger',
  templateUrl: './select-trigger.component.html',
  styles: []
})
export class SelectTriggerComponent implements OnInit {
    @Output() done = new EventEmitter<string>();
    isVisiable = false;
    isLoading = false;
    dataSet;

    name;

    groupList;   // 分组列表
    hostList;   //主机列表

    obj = {
        groupid:null,
        name:null,
        hostid:null,
    }

    constructor(private overAllSer: OverAllService) {
    }

    ngOnInit() {
        this.getGroup();
    }

    //获取分组
    getGroup() {
        this.overAllSer.findGroup().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.groupList = res.data;
                    this.obj.groupid = this.groupList[0].groupid + '';
                    this.getHost();
                }
            }
        );
    }

    //获取主机列表
    getHost() {
        const data = {
            'groupids': [this.obj.groupid],
            'hostExtend': {
                'superiorHostid': null
            }
        };
        this.overAllSer.findDetail(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.hostList = res.data;
                    this.obj.hostid = this.hostList[0].hostid + '';
                    this.getwebList();
                }
            }
        );
    }

    showModal() {
        this.isVisiable = true;
    }

    //阈值列表
    getwebList() {
        this.isLoading = true;
        const data = {
            queryName: this.obj.name,
            hostid: this.obj.hostid
        };
        this.overAllSer.findTri(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.dataSet = res.data;
                }
                this.isLoading = false;
            }
        );
    }

    set(item) {
        this.done.emit(item);
        this.isVisiable = false;
    }

    handleCancel() {
        this.isLoading = false;
        this.isVisiable = false;
    }

    handleOk() {
        this.isVisiable = false;
    }

}
