import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { GeneralService } from '../../services/generalservice';


@Component({
  selector: 'app-login-forgot',
  standalone: false,
  templateUrl: './login-forgot.component.html',
  styleUrl: './login-forgot.component.css'
})
export class LoginForgotComponent implements OnInit {
  // @Input() brandName = 'Contoso';
  // @Input() subtitle = 'to your account';
  // @Input() mode = '';
  @Output() otpEventSubmit = new EventEmitter<any>();
  @Output() otpVerifyEventSubmit = new EventEmitter<any>();

  forgotFrm!: FormGroup;
  isLoading: boolean = false;
  isSendOtp: boolean = false;
  isModalOpen: boolean = false;
  // busy = false;
  // message = '';

  constructor(
    // private generalService: GeneralService
    // private fb: FormBuilder
  ) {}

  ngOnInit(): void {
    this.setupFormControl();
  }

  setupFormControl(){
    this.forgotFrm = new FormGroup({
      email: new FormControl('',[Validators.required, Validators.email, Validators.maxLength(100)]),
      otp:   new FormControl('',[Validators.required, Validators.maxLength(6)]),
      newPassword: new FormControl('',[Validators.required, Validators.maxLength(50)]),
      confirmPassword: new FormControl('',[Validators.required, Validators.maxLength(50)])
    });

  }

  EventSubmit(event: any){
    this.otpEventSubmit.emit(event);
  }


  VerifyEventSubmit(event: any){
    this.otpVerifyEventSubmit.emit(event);
  }

  onSubmin(){
    let data = {
      email: this.forgotFrm.get('email')?.value
    }

    this.EventSubmit(data);
  }

  onVerifySubmit(){
    let data = {
      email: this.forgotFrm.get('email')?.value,
      otp: this.forgotFrm.get('otp')?.value
    }
    this.VerifyEventSubmit(data);
  }

  onSetNewPassword(){
    let newpassword = this.forgotFrm.get('newPassword')?.value;
    let confirmPassword = this.forgotFrm.get('confirmPassword')?.value;

    let data = {
      email: this.forgotFrm.get('email')?.value,
      otp: this.forgotFrm.get('otp')?.value,
      newPassword: newpassword
    }

    if (newpassword.trim() === confirmPassword.trim()) {
      // this.VerifyEventSubmit(data);
      console.log("Password matched! (รหัสผ่านตรงกัน)");
    } else {
      console.error("Password mismatched! (รหัสผ่านไม่ตรงกัน)");
    }

    // console.log(newpassword.value);

  }
   // otp:  }
}
