import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
 
import { Router } from '@angular/router';
import { NbToastrService, NbWindowControlButtonsConfig, NbWindowRef, NbWindowService } from '@nebular/theme';
import { environment } from '../../../../environments/environment';
import { AuthService } from '../auth.service';
import { OtpComponent } from '../otp/otp.component';

@Component({
  selector: 'ngx-forgot-password',
  templateUrl: './forgot-password.component.html',
  styleUrls: ['./forgot-password.component.scss']
})
export class ForgotPasswordComponent implements OnInit {
  redirectDelay: number;
  showMessages: any;
  strategy: string;
  submitted: boolean;
  errors: string[];
  messages: string[];
  user: any;
  forgotMailDara: any;
  form: FormGroup;
  email: string;
  password: string; 
  forgotTokenEmail: any; 
  public mailUrl = environment.mailUrl;
  resetLink: any = 0;
  minimize = false;
  maximize = false;
  fullScreen = true;
  close = true;
  formData:any=[]
  index = 1;
  destroyByClick = true;
  duration = 2000;
  constructor(private router:Router,public _ser:AuthService,private toastrService: NbToastrService,private windowService: NbWindowService) { 
  //   this._ser.forgotEmail_observable$.subscribe((res) => {
  //     console.log('email', res); 
  //    this.forgotMailDara = res
  // })
  }

  ngOnInit() {
    this.form = new FormGroup({    
      email: new FormControl('',[Validators.required,Validators.email,Validators.pattern('[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,63}$')]),
    });
    this.form.controls['email'].setValue(this.forgotMailDara)
  }

  submit(){
    console.log(this.form.value)   
    const mailid = this.form['value'].email.split(/\s/).join('');  
    // this.router.navigate(['/reset-password'])  
    let forgotMail = {'email':mailid}
    // let forgotMail = {'email':mailid}
    console.log(forgotMail)
    this._ser.getForgotToken(forgotMail).subscribe(res => {
      if(res.status == 200){
        this.resetLink = 1
        console.log(res)
        this.forgotTokenEmail = res

        const buttonsConfig: NbWindowControlButtonsConfig = {
          minimize: this.minimize,
          maximize: this.maximize,
          fullScreen: this.fullScreen,
          close: this.close,
        };
        const windowRef: NbWindowRef =  this.windowService.open(OtpComponent, { title: `OTP`, context: forgotMail
        , buttons: buttonsConfig  });
       
    
    
          // Subscribe to the afterClosed event
          windowRef.onClose.subscribe((data) => {
            // Handle the window closure
            console.log(data);
            if(data == 1){
             // this.showToast('success', 'Data Changed Successfully', 'Order Cancel Completed..!');
          //   this.getorderlistApi();
          this.router.navigate(['auth/reset-password/'+mailid])
               console.log('Window closed 123');
            }else if(data == 0){
            //  this.showToast('warning', 'Somthing is Wrong...!', 'Order Cancel Failed..!');
            }
           
          
          });
       // this.toastrService.success('Login',res.message)
        // this._ser.getForgotDetails(this.forgotTokenEmail)
        // this.notifierService.notify('success', res.message);
        //this.router.navigate(['authentication/reset-password'])
      } else{
       // this.toastrService.warning('Login',res.message)
      }
    }, err => {
     
      //alert(err.message);
    })
    
  }

}
