import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-account',
  standalone: false,
  templateUrl: './account.component.html',
  styleUrl: './account.component.css'
})

export class AccountComponent {
  infoForm: FormGroup;
  infos: any[] = [];

  constructor(private fb: FormBuilder) {
    this.infoForm = this.fb.group({
      gender: ['', Validators.required],
      nameLast: ['', [Validators.required, Validators.minLength(3)]],
      nameFirst: ['', [Validators.required, Validators.minLength(3)]],
      email: ['', [Validators.required, Validators.pattern(/^[a-zA-Z0-9._%+-]+$/)]],
      address: ['', [Validators.required, Validators.minLength(10)]],
      codePostal: ['', [Validators.required, Validators.minLength(4), Validators.pattern(/^0-9$/)]],
    }); 
  }

  addInfo() {
    if (this.infoForm.valid) {
      const formData = this.infoForm.value;

      // Ajouter @edu.igensia.com automatiquement
      const email = formData.email.toLowerCase();
      const fullEmail = `${email}@edu.igensia.com`;
      const nameLast = formData.nameLast.toUpperCase();
      const nameFirst = formData.nameFirst.charAt(0).toUpperCase() + formData.nameFirst.slice(1).toLowerCase();

      const newInfo = {
        ...formData,
        nameLast: nameLast, // Mettre l'email complet
        nameFirst: nameFirst, // Mettre l'email complet
        email: fullEmail, // Mettre l'email complet
        date: new Date() // Ajoute la date actuelle
      };

      this.infos.push(newInfo);
      this.infoForm.reset(); // Réinitialise le formulaire après l'ajout
    }
  }
}
