import { Component, inject } from '@angular/core';
import {
  FormGroup,
  Validators,
  ReactiveFormsModule,
  FormControl,
} from '@angular/forms';
import { AuthService } from '../../shared/services/auth.service';
import { User } from '../../shared/interfaces/auth';

@Component({
  selector: 'app-login',
  imports: [ReactiveFormsModule],
  templateUrl: './login.component.html',
})
export class LoginComponent {
  private auth = inject(AuthService);
  form = new FormGroup({
    name: new FormControl('', [Validators.required]),
    password: new FormControl('', [Validators.required]),
  });

  get name() {
    return this.form.controls.name;
  }
  get password() {
    return this.form.controls.password;
  }
  submit() {
    if (this.form.invalid) {
      return;
    }
    const user: User = {
      name: this.form.controls.name.value!,
      password: this.form.controls.password.value!,
    };
    this.auth.login(user);
  }
}
