import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { NbToastrService } from '@nebular/theme';
 
import { ActivatedRoute, Router } from '@angular/router';
import { AuthService } from '../auth.service';

@Component({
  selector: 'ngx-reset-password',
  templateUrl: './reset-password.component.html',
  styleUrls: ['./reset-password.component.scss']
})
export class ResetPasswordComponent implements OnInit {
  form: FormGroup;
  forgotApiDetails: string;
  email:any=[];
  token:any = [];
  hide: boolean;
  hide2:boolean;
  submitted: boolean;
  showPassword = false;
  showPassword2 = false;
  constructor(private router:Router,public _ser:AuthService,private toastrService: NbToastrService,private route: ActivatedRoute) { }

  ngOnInit(): void {
    this.hide = true;
    this.hide2 =true
    // this._ser.name.subscribe(data=>{
    //   this.forgotApiDetails = data;
    //   console.log(this.forgotApiDetails)
    // });
    this.email = this.route.snapshot.paramMap.get('email');
   // this.token = this.route.snapshot.paramMap.get('token');
console.log(this.email)
    this.form = new FormGroup({     
      password: new FormControl('', [Validators.required,Validators.pattern(
        '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*_=+-]).{8,16}$'
      )]),
      confirm_password: new FormControl('', [Validators.required,Validators.pattern(
        '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*_=+-]).{8,16}$'
      )]),
    });
  }
  //  public myError = (controlName: string, errorName: string) =>{
  //     return this.form.controls[controlName].hasError(errorName);
  //   }

  submit(){
    console.log(this.form.value)  
    let param ={email :this.email,password:this.form.value.password,password2:this.form.value.confirm_password}
    this._ser.resetPasswordApi(param).subscribe(res => {
      console.log(res)
      if(res.status == 200){
        this.toastrService.success('Reset',res.message)
        setTimeout(() => {
              this.router.navigate(['auth/login']);
              return false;
              },1000); 
      }
     
      // this.forgotTokenEmail = res     
    }, err => {
     
      //alert(err.message);
    })
    
  }

  getInputType(event) {
    if(event == 1){
      if (this.showPassword) {
        return 'text';
      }
      return 'password';
    } else {
      if (this.showPassword2) {
        return 'text';
      }
      return 'password';
    }
   
  }

  toggleShowPassword(event) {
    if(event == 1){
      this.showPassword = !this.showPassword;
    } else {
      this.showPassword2 = !this.showPassword2;
    }
  }

}
