import { Component, OnInit, ViewChild, HostListener } from "@angular/core";
import { AuthServiceService } from "src/app/shared/auth-service.service";
import { Router, ActivatedRoute } from "@angular/router";

import { Location } from '@angular/common';
import { NgxSpinnerService } from "ngx-spinner";
import { ToastrService } from "ngx-toastr";
import { NgForm } from "@angular/forms";
import { Response } from '@angular/http';
import { HttpClient } from "@angular/common/http";

@Component({
  selector: 'app-forget-password',
  templateUrl: './forget-password.component.html',
  styleUrls: ['./forget-password.component.css']
})
export class ForgetPasswordComponent implements OnInit {

  constructor(
    public authSer: AuthServiceService, 
    private router: Router, 
    private route: ActivatedRoute, 
    private location: Location,
    private spinner: NgxSpinnerService,
    private toastr: ToastrService,
    private http : HttpClient
  ) { }

  @ViewChild('f') forgetDataForm: NgForm;

  screenHeight:any;
  saveDisabled: boolean = false;
  sendEmail : boolean = true;
  checkCode: boolean = false;
  changePass: boolean = false;
  notequal: boolean = false;
  identy : string ;
  @HostListener('window:resize', ['$event'])
  getScreenSize(event?) {
        this.screenHeight = window.innerHeight;
        console.log(this.screenHeight);
  }

  ngOnInit() {

      // this.authSer.showSearchHeader = false;
      this.authSer.notificationLogin = false;
      // this.authSer.showHeaderLogin = true;
      this.authSer.showHeaderDashBoard = false;
      this.authSer.showDashboardHeader = true;
      this.authSer.arabicTemplate = true;
      this.spinner.hide();

  }
  onSubmitedForm(status){
    if (status == 'sendEmail') {
      console.log(this.forgetDataForm.value);

      this.http.post(this.authSer.pathApi + '/send/email' , this.forgetDataForm.value).subscribe(
        res=> {
          this.sendEmail = false;
          this.checkCode = true;
          this.identy=this.forgetDataForm.value.identity;
          console.log(res);
          
        },
        err =>{
          if (err.error.status == 'wrong identity') {
            this.toastr.warning('رقم الهوية او البريد الألكتروني الذي ادخلته غير موجود على النظام');
          }else{
            this.toastr.error('خطأ في السرفير')
          }
         
          
        }
      );
     
    }else if (status == 'checkCode') {
      this.forgetDataForm.value.identity = this.identy;

      this.http.post(this.authSer.pathApi + '/check-code' , this.forgetDataForm.value).subscribe(
        res=>{
          console.log(this.forgetDataForm.value);
          this.checkCode = false;
          this.changePass = true;
        },
        err =>{
          this.toastr.warning('الرمز الذي أدخلته غير صحيح')
        }
      );
     

    }else if (status == 'changePass') {
      const newPass = this.forgetDataForm.value.password;
      const confirmPass = this.forgetDataForm.value.password_confirmation;
      if (newPass != confirmPass){
        this.notequal = true;
        return 0;
        
      }
      this.forgetDataForm.value.identity = this.identy;
      this.http.post(this.authSer.pathApi + '/reset-password' , this.forgetDataForm.value).subscribe(
        res=>{
          this.toastr.success('تم تغير كلمة المرور بنجاح')
          this.router.navigate(['auth/login']);

        },
        err=>{
          console.log(err);
          
        }
      );
     
    }
 

  }

}
