import {
  Component,
  ContentChild,
  EventEmitter,
  Input,
  Output,
  TemplateRef,
} from '@angular/core';
import { FormGroup } from '@angular/forms';
import { SpinnerService } from 'src/app/service/spinner.service';
import {
  FormPageContentDirective,
  FormPageHeaderDirective,
} from 'src/app/modules/shared/form';

@Component({
  selector: 'app-form-page',
  templateUrl: './form-page.component.html',
  styleUrl: './form-page.component.scss',
})
export class FormPageComponent<T> {
  @Input({ required: true }) formGroup!: FormGroup;
  @Input() isUpdate = false;
  // eslint-disable-next-line @angular-eslint/no-output-on-prefix
  @Output() onSave = new EventEmitter<T>();
  // eslint-disable-next-line @angular-eslint/no-output-on-prefix
  @Output() onClose = new EventEmitter<void>();

  @ContentChild(FormPageHeaderDirective, { read: TemplateRef })
  formPageHeader!: TemplateRef<never>;

  @ContentChild(FormPageContentDirective, { read: TemplateRef })
  formPageContent!: TemplateRef<never>;

  constructor(private spinner: SpinnerService) {}

  save() {
    this.spinner.show();
    this.onSave.emit();
  }
}
