import { Component, Inject, OnInit } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';

import { FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-cambiar-contrasena',
  templateUrl: './cambiar-contrasena.component.html',
  styleUrls: ['./cambiar-contrasena.component.css'],
})
export class CambiarContrasenaComponent implements OnInit {
  hide1 = true;
  hide2 = true;

  formGroup: FormGroup;

  constructor(
    public dialogRef: MatDialogRef<any>,
    @Inject(MAT_DIALOG_DATA) public data: any
  ) {
    this.formGroup = this.createFormGroup();
  }
  private createFormGroup() {
    return new FormGroup({
      nombre: new FormControl( { value: `${this.data.NOMBRE} ${this.data.APELLIDO_PATERNO}`, disabled:true }, [Validators.required, Validators.min(8)]),
      password1: new FormControl('', [Validators.required, Validators.min(8)]),
      password2: new FormControl('', [Validators.required, Validators.min(8)]),
    });
  }

  get password1() {
    return this.formGroup.get('password1');
  }
  get password2() {
    return this.formGroup.get('password2');
  }

  ngOnInit(): void {}

  generatePassword() {
    let chars =
      '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*-_=+ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let passwordLength = 12;
    let password = '';
    for (let i = 0; i <= passwordLength; i++) {
      let randomNumber = Math.floor(Math.random() * chars.length);
      password += chars.substring(randomNumber, randomNumber + 1);
    }
    this.password1?.setValue(password);
    this.password2?.setValue(password);
  }
}
