import { Component, OnInit } from '@angular/core';
import { NavController, LoadingController } from '@ionic/angular';
import { NgForm } from '@angular/forms';
import { AuthService } from 'src/app/services/auth.service';
import { AlertService } from 'src/app/services/alert.service';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-forgot',
  templateUrl: './forgot.page.html',
  styleUrls: ['./forgot.page.scss'],
})
export class ForgotPage implements OnInit {

  private loading;

  constructor(
    private authService: AuthService,
    private navCtrl: NavController,
    private alertService: AlertService,
    private translateService: TranslateService,
    private loadingCtrl: LoadingController
  ) { }
  ngOnInit() {
  }
  // Dismiss Forgot Modal
  dismissForgot() {
    this.navCtrl.back();
  }
  // On Register button tap, dismiss login modal and open register modal

  forgot(form: NgForm) {
    this.loadingCtrl.create().then((overlay) => {
      this.loading = overlay;
      this.loading.present();
    });
    this.authService.forgot(form.value.email).subscribe(
      data => {
        this.loading.dismiss();
        this.alertService.presentToast(this.translateService.instant('alerts.reset_pass'));
      },
      error => {
          this.loading.dismiss();
          this.alertService.presentToast(this.translateService.instant('alerts.error') + error['message']);
          console.log(error);
      },
      () => {
        this.dismissForgot();
      }
    );
  }
}
