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 { Category } from '../data.service'
import { categoryFields } from './fields'

export interface DialogData {
    category: Category
}


@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: Category = 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);
    }
}
