import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {FormBuilder, FormGroup, Validators, FormControl} from '@angular/forms';
import {Dialog} from '../../../classes/dialog';
import {AuthService} from '../../../classes/auth.service';
import {SelectedComponent} from '../selected/selected.component';
import {repairErrors, setErrors} from '../../../classes/utils';

@Component({
    selector: 'app-forgot',
    templateUrl: './forgot.component.html',
    styleUrls: ['./forgot.component.scss']
})
export class ForgotComponent implements OnInit {
    public form: FormGroup;
  usernameCtrl = new FormControl('', [Validators.required]);
  public loading = false;
    constructor(private fb: FormBuilder,
                private router: Router,
                private dialog: Dialog,
                private auth: AuthService,
    ) {
      this.loading = false ;
    }

    ngOnInit() {
        this.form = this.fb.group({
          usernameCtrl: this.usernameCtrl,
        });
    }

    onSubmit() {
      this.loading = true ;
        this.auth.forgotPass1(this.form.controls['usernameCtrl'].value , '1').subscribe(
            res =>{
              // this.loading = false ;
              this.dialog.openWithData(SelectedComponent , this.form.controls['usernameCtrl'].value);
            } ,
            error =>{
              this.loading = false ;
              let errors = error.json().errors;
                setErrors(repairErrors(errors), this.form.controls, this.dialog);
            }
        );
    }
}
