/**
 * @license
 * Copyright renie.tech, all Rights Reserved.
 * Licensed under the MIT License. See LICENSE in the project root for more information.
 */
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
export class RForm {
    constructor() {
        this.showSpinner = false;
        this.submit = new EventEmitter();
        this.form = new FormGroup({});
    }
    ngOnInit() {
        this.initForm();
    }
    initForm() {
        this.config.fields.forEach((field) => {
            this.form.addControl(field.formControlName, new FormControl(field.value, field.validators, field.asyncValidators));
        });
    }
    onSubmit() {
        console.log(this.form.value);
        this.submit.next(this.form.value);
    }
    hasError(controlName, error) {
        return this.form.get(controlName).touched && this.form.get(controlName).hasError(error);
    }
}
RForm.decorators = [
    { type: Component, args: [{
                selector: 'r-form',
                template: "<form [formGroup]=\"form\" (ngSubmit)=\"onSubmit()\">\r\n  <ng-container *ngFor=\"let field of config.fields\">\r\n    <ng-container [ngSwitch]=\"field.type\">\r\n      <input rInput [fullWidth]=\"true\" *ngSwitchCase=\"'text'\" type=\"text\"\r\n        [placeholder]=\"field.placeholder\" [formControlName]=\"field.formControlName\"\r\n      >\r\n      <input rInput [fullWidth]=\"true\" *ngSwitchCase=\"'number'\" type=\"number\"\r\n        [placeholder]=\"field.placeholder\" [formControlName]=\"field.formControlName\"\r\n      >\r\n      <ng-container *ngSwitchCase=\"'select'\">\r\n        <r-select [formControlName]=\"field.formControlName\" [placeholder]=\"field.placeholder\">\r\n          <r-option *ngFor=\"let option of field.options\" [value]=\"option.value\">\r\n            {{option.label}}\r\n          </r-option>\r\n        </r-select>\r\n      </ng-container>\r\n      <ng-container *ngSwitchCase=\"'multiselect'\">\r\n        <r-select multiple=\"true\" [formControlName]=\"field.formControlName\" [placeholder]=\"field.placeholder\">\r\n          <r-option *ngFor=\"let option of field.options\" [value]=\"option.value\">\r\n            {{option.label}}\r\n          </r-option>\r\n        </r-select>\r\n      </ng-container>\r\n      <ng-container *ngSwitchCase=\"'datepicker'\">\r\n        <input\r\n          rInput\r\n          [fullWidth]=\"true\"\r\n          [formControlName]=\"field.formControlName\"\r\n          [placeholder]=\"field.placeholder\"\r\n          [rDatepicker]=\"dateTimePicker\"\r\n        >\r\n        <r-datepicker #dateTimePicker></r-datepicker>\r\n      </ng-container>\r\n      <r-file-input\r\n        *ngSwitchCase=\"'file'\"\r\n        status=\"info\"\r\n        [formControlName]=\"field.formControlName\"\r\n      >\r\n      </r-file-input>\r\n    </ng-container>\r\n    <span *ngIf=\"hasError(field.formControlName, 'required')\" class=\"r-error\">\r\n      Field required\r\n    </span>\r\n  </ng-container>\r\n  <button [rSpinner]=\"showSpinner\" rSpinnerStatus=\"basic\" [disabled]=\"showSpinner || !form.valid\" r-button type=\"submit\">\r\n    {{config.submitButtonText || 'Submit'}}\r\n  </button>\r\n</form>",
                styles: [":host{display:block}:host form{display:flex;flex-direction:column}"]
            },] }
];
RForm.propDecorators = {
    config: [{ type: Input }],
    showSpinner: [{ type: Input }],
    submit: [{ type: Output }]
};
//# sourceMappingURL=form.js.map