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

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

import { categoryFields } from './fields'
import { CategoryRead } from '../../../openapi'

export interface DialogData {
    category: CategoryRead
}


@Component({
    selector: 'gisaf-admin-category-dialog',
    templateUrl: 'dialog.component.html',
    styleUrls: ['dialog.component.css'],
})
export class GisafAdminCategoryDialogComponent implements OnInit {
    formGroup: UntypedFormGroup

    constructor(
        public dialogRef: MatDialogRef<GisafAdminCategoryDialogComponent>,
        @Inject(MAT_DIALOG_DATA) public data: DialogData
    ) {}

    ngOnInit() {
        let category: CategoryRead = this.data['category']
        this.formGroup = new UntypedFormGroup({})
        //this.formGroup.addControl('name', new FormControl(category.name, [Validators.required]))
        for (let categoryField of categoryFields) {
            this.formGroup.addControl(
                categoryField.name,
                new UntypedFormControl(
                    category[categoryField.name] || categoryField.defaultValue,
                    categoryField.validators,
                )
            )
        }
    }

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