import { Component, OnInit } from "@angular/core";
import { FormBuilder, FormControl, Validators } from "@angular/forms";
import { MatDialogRef } from "@angular/material/dialog";
import { marker } from "@biesbjerg/ngx-translate-extract-marker";
import { NanoDialogBase } from "src/app/shared/dialogs/nano-base-dialog";
import { NanoValidators } from "src/app/shared/nano-validators";
import { DialogService } from "src/app/shared/services/dialog.service";

@Component({
  selector: "app-change-email-dialog",
  templateUrl: "./change-email-dialog.component.html",
  styleUrls: ["./change-email-dialog.component.scss"],
})
export class ChangeEmailDialogComponent
  extends NanoDialogBase<ChangeEmailDialogComponent>
  implements OnInit
{
  inputsValid: boolean = false;

  public emailForm = this.fb.group({
    email: new FormControl('', [Validators.required, Validators.email]),
    email2: new FormControl('', [Validators.required, Validators.email])
  });

  constructor(
    public dialogRef: MatDialogRef<ChangeEmailDialogComponent>,
    public dialogService: DialogService,
    public fb: FormBuilder
  ) {
    super(dialogRef);
  }

  ngOnInit(): void {
    this.emailForm.get("email2").addValidators(NanoValidators.controlEquals(this.emailForm.get("email")));
  }

  changeEmail() {
    this.dialogRef.close(this.emailForm.get("email").value);
  }
}
