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

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

    title = '新增角色';
    isVisible = false;
    validateForm: FormGroup;

    constructor(private fb: FormBuilder, private message: NzMessageService,
                private systemSer: SystemService) {
    }

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

    showModal(): void {
        this.isVisible = true;
    }

    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('新增成功');
                }
            }
        );
    }

}
