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

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

import { User } from './models'

export interface DialogData {
    user: User
}

@Component({
    selector: 'gisaf-admin-access-user-dialog',
    templateUrl: 'user-dialog.component.html',
    styleUrls: ['user-dialog.component.css'],
})
export class GisafAdminAccessUserDialogComponent implements OnInit {
    formGroup: UntypedFormGroup
    constructor(
        public dialogRef: MatDialogRef<GisafAdminAccessUserDialogComponent>,
        @Inject(MAT_DIALOG_DATA) public data: DialogData
    ) {}

    ngOnInit() {
        let user: User = this.data['user'] || new User(undefined, '', '', true)
        this.formGroup = new UntypedFormGroup({})
        this.formGroup.addControl('id', new UntypedFormControl(user.id))
        this.formGroup.addControl('name', new UntypedFormControl(user.name, [Validators.required]))
        this.formGroup.addControl('email', new UntypedFormControl(user.email, [Validators.required, Validators.email]))
        this.formGroup.addControl('password', new UntypedFormControl(''))
        this.formGroup.addControl('active', new UntypedFormControl(user.active))
    }

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