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(): void {
        for (const i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        this.systemSer.addRole(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.add.emit();
                    this.message.info('新增成功');
                }
            }
        );
    }

}
