import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {TopologyService} from '../../topology.service';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
    selector: 'smart-topology',
    templateUrl: './topology.component.html',
    styles: []
})
export class TopologyComponent implements OnInit {
    @Output() done = new EventEmitter();

    isVisible = false;
    title: string;
    validateForm:FormGroup;
    topoId;

    pageNum;
    pageCount;

    constructor(private fb:FormBuilder,private topologySer:TopologyService,
                private message:NzMessageService) {
    }

    ngOnInit() {
        this.initForm();
    }

    initForm(){
        this.validateForm = this.fb.group(
            {
                name:['',[Validators.required]],
                refreshRete:['',[Validators.required]],
                json:[''],
                users:[[]],
                userGroups:[[]]
            }
        )
    }

    showAddMOodal() {
        this.isVisible = true;
        this.title = '添加拓扑图';
    }

    showEditModal(id) {
        this.title = '编辑拓扑图';
        this.isVisible = true;
        this.topoId = id;
        this.topologySer.findItem(id).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                   this.validateForm.patchValue(res.data);
                }else{
                    this.message.create('error',`${res.errMsg}`);
                }
            }
        );
    }

    handleOk(){
        for(let i  in this.validateForm.controls){
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if(this.validateForm.invalid){
            return false;
        }
        if(this.title == "添加拓扑图"){
            this.create();
        }
        if(this.title == "编辑拓扑图"){
            this.update();
        }
    }

    create(){
        this.topologySer.create(this.validateForm.value).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.initForm();
                    this.message.create('success', `创建成功`);
                    this.isVisible = false;
                    this.done.emit();
                }else{
                    this.message.create('error', `${res.errMsg}`);
                }
            }
        )
    }

    update(){
        this.validateForm.addControl('id',new FormControl(this.topoId));
        this.topologySer.update(this.validateForm.value).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.initForm();
                    this.message.create('success', `编辑成功`);
                    this.done.emit();
                    this.isVisible = false;
                }else{
                    this.message.create('error', `${res.errMsg}`);
                }
            }
        )
    }

    handleCancel() {
        this.isVisible = false;
        this.initForm();
    }

}
