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

@Component({
  selector: 'ngx-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss'],
  // host: { class: 'custom-login' },
})
export class LoginComponent implements OnInit {
  loginForm: FormGroup | any;
   disable:any =true;
 
  minimize = false;
  maximize = false;
  fullScreen = true;
  close = true;
  formData:any=[]
  index = 1;
  destroyByClick = true;
  duration = 2000;
  hasIcon = true;
 
  position: NbGlobalPosition = NbGlobalPhysicalPosition.TOP_RIGHT;
  preventDuplicates = false;
  status: NbComponentStatus = 'primary';
  showPassword: boolean = false;
  constructor( private router:Router,public _ser:AuthService,private toastrService: NbToastrService) { }

  ngOnInit(): void {
    this.loginForm = new FormGroup({
      
      email: new FormControl('', [Validators.required, Validators.email,Validators.pattern(
        '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,63}$',
      ),]),
      // password: new FormControl('', [ Validators.required])
      password: new FormControl('', [Validators.required,Validators.pattern(
        '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*_=+-]).{8,12}$'
      )])
    });
  }
  toggleShowPassword() {
    this.showPassword = !this.showPassword;
  }

  getInputType() {
    if (this.showPassword) {
      return 'text';
    }
    return 'password';
  }
  onSubmit(){
    this.disable= false
    console.log("enter",  this.loginForm.value)
   let auth = { 'email': this.loginForm.value.email, 'password': this.loginForm.value.password};   
   
      this._ser.getLoginToken(auth).subscribe(res => {
      console.log(res)
      if(res.status == 200 || res.status == 'Logged in'){
        if (res.token !== '') {
          localStorage.setItem('user_token',res.token);
          localStorage.setItem('user_role',res.role);
          localStorage.setItem('user_name',res.UserName);
          localStorage.setItem('roleId',res.roleId);
          
          localStorage.setItem('email',JSON.stringify(auth))
          this.disable= true
            this.showToast('success', 'Login Successfully', 'Welcome..!');

            if(res.role == 'FRONT-OFFICE'){
              this.router.navigate(['/home'])
            }else{
              this.router.navigate(['/pages/dashboard'])
            }
        
         

        }
      }else{
        console.log('123')
        this.showToast('warning', 'Somthing is Wrong...!',res.message);
      }
    })
   
  }


  private showToast(type: NbComponentStatus, title: string, body: string) {
    const config = {
      status: type,
      destroyByClick: this.destroyByClick,
      duration: this.duration,
      hasIcon: this.hasIcon,
      position: this.position,
      preventDuplicates: this.preventDuplicates,
    };
    const titleContent = title ? ` ${title}` : '';

    this.index += 1;
    this.toastrService.show(
      body,
      `${titleContent}`,
      config);
  }

  openForgot(){

  }
}
