import { Component, OnInit, Inject } from '@angular/core'
import { UntypedFormGroup, UntypedFormControl, Validators } from '@angular/forms'

import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'

import { Role } from './models'

export interface DialogData {
  role: Role
}

@Component({
  selector: 'gisaf-admin-access-role-dialog',
  templateUrl: 'role-dialog.component.html',
})
export class GisafAdminAccessRoleDialogComponent implements OnInit {
  formGroup: UntypedFormGroup = new UntypedFormGroup({})
  constructor(
    public dialogRef: MatDialogRef<GisafAdminAccessRoleDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: DialogData
  ) { }

  ngOnInit() {
    let role: Role = this.data['role'] || new Role(undefined, '', '')
    this.formGroup.addControl('id', new UntypedFormControl(role.id))
    this.formGroup.addControl('name', new UntypedFormControl(role.name, [Validators.required]))
    this.formGroup.addControl('description', new UntypedFormControl(role.description, [Validators.required]))
  }

  save() {
    this.dialogRef.close(this.formGroup.value);
  }
}
