import { Component, OnInit } from "@angular/core";
import { FormBuilder, Validators } from "@angular/forms";
import { marker } from "@biesbjerg/ngx-translate-extract-marker";
import { DialogService } from "src/app/shared/services/dialog.service";
import { ServerRestApiService } from "src/app/shared/services/server-rest-api.service";

@Component({
  selector: "app-account-recovery",
  templateUrl: "./account-recovery.component.html",
  styleUrls: ["./account-recovery.component.scss"],
})
export class AccountRecoveryComponent implements OnInit {
  public isPending: boolean = false;
  public isSendSuccess: boolean = false;

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

  constructor(
    private restApiService: ServerRestApiService,
    private dialogService: DialogService,
    private fb: FormBuilder
  ) {}

  public start() {
    this.isPending = true;
    this.restApiService
      .accountRecoveryPrepare(this.emailForm.get('email').value)
      .then(() => {
        this.isSendSuccess = true;
      })
      .catch((err) => {
        console.log("account rec error", err);
        this.dialogService.openAlertDialog(
          marker("Error"),
          marker("Error happened during the operation")
        );
      })
      .finally(() => {
        this.isPending = false;
      });
  }

  ngOnInit(): void {}

}
