import {Component, OnInit} from '@angular/core';
import {OverAllService} from '../../../overAll/overAll.service';
import {NzMessageService, NzTreeNode, NzFormatEmitEvent} from 'ng-zorro-antd';
import {CommonService} from '../../../shared/common/common.service';

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

    isVisible = false;
    dataSet;
    nodes;

    constructor(private overAllSer:OverAllService,private message:NzMessageService,
                private commonSer:CommonService) {
    }

    ngOnInit() {
        this.getGroup();
    }

    getGroup(){
        this.overAllSer.findGroup().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    const dataSet = res.data;
                    dataSet.forEach(res=>{
                        res.title  = res.name;
                        res.key = res.groupid;
                    });
                    this.toNode(dataSet);
                } else {
                    this.message.info(res.errMsg);
                }
            }
        );
    }

    toNode(data){
        this.nodes = data.map(res=>{
            return new NzTreeNode(res);
        })
    }

    //下级
    mouseAction(name: string, event: NzFormatEmitEvent): void {
        // if (name === 'expand') {
            setTimeout(_ => {
                // if (e.node.getChildren().length === 0 && e.node.isExpanded) {
                //     e.node.addChildren([
                //         {
                //             title: 'childAdd-1',
                //             key  : '10031-' + (new Date()).getTime()
                //         },
                //         {
                //             title : 'childAdd-2',
                //             key   : '10032-' + (new Date()).getTime(),
                //             isLeaf: true
                //         } ]);
                // }
            }, 1000);
        // }
    }

    showModal() {
        this.isVisible = true;
    }

    handleOk() {
        this.isVisible = false;
    }

    handleCancel() {
        this.isVisible = false;
    }

}
