import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {
    AbstractControl,
    FormBuilder,
    FormControl,
    FormGroup,
    Validators
} from '@angular/forms';
import {SystemService} from '../../system.service';
import {NzMessageService, NzTreeNode} from 'ng-zorro-antd';
import {LayoutService} from '../../../layouts/layout.service';
import {ToTree} from '../../../shared/common/toTree';

@Component({
    selector: 'smart-role',
    templateUrl: './role.component.html',
    styles: []
})
export class RoleComponent implements OnInit {
    @Output() add = new EventEmitter<any>();

    title;
    isVisible = false;
    validateForm: FormGroup;
    menuList;

    nodes;
    nodeList;

    constructor(private fb: FormBuilder, private message: NzMessageService,
                private systemSer: SystemService,private layoutSer:LayoutService,private toTreeSer:ToTree) {
    }

    ngOnInit() {
        this.ininForm();
        this.getMenu();
    }

    ininForm(){
        this.validateForm = this.fb.group({
            name: [null, [Validators.required]],
            status: [null, [Validators.required]],
            comments: [null, [Validators.required]],
        });
    }

    //获取菜单栏
    getMenu(){
        this.layoutSer.getMenu().subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.menuList =this.toTreeSer.listToTree('id','parentId',res.data);
                    this.menuList.forEach(res => {
                        res.title = res.name;
                        res.key = res.id;
                        res.children.forEach((child)=>{
                            child.title = child.name;
                            child.key = child.id;
                            child.isLeaf = true;
                        })
                    });
                    this.toNode(this.menuList);
                }
            }
        )
    }

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

    showAddModal(): void {
        this.title = '新增角色';
        this.isVisible = true;
    }

    showEditModal(id){
        this.title = "编辑角色";
        this.isVisible = true;
        this.systemSer.getRole(id).subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    const data = res.data;
                    data.status += "";
                    this.validateForm.patchValue(data);
                }
            }
        )
    }

    handleCancel(): void {
        this.isVisible = false;
    }

    handleOk() {
        for (let i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if (this.validateForm.invalid) {
            return false;
        }
        const data = {
            role:{
                name:this.validateForm.value.name,
                status:this.validateForm.value.status,
                comments:this.validateForm.value.comments,
            },
            resources:[],
            menus:[]
        };
        this.systemSer.addRole(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.add.emit();
                    this.message.info('新增成功');
                }
            }
        );
    }

}
