import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { NzCardModule } from 'ng-zorro-antd/card';
import { NzInputModule } from 'ng-zorro-antd/input'
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzCheckboxModule } from 'ng-zorro-antd/checkbox';
import { NzFormModule } from 'ng-zorro-antd/form';
import { NzAlertModule } from 'ng-zorro-antd/alert';

import { CommonModule } from '@angular/common';
import { FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { catchError, finalize, Observable, tap } from 'rxjs';
import { AuthService } from '../data-access/Services/ApiService.service';
import { response } from 'express';
@Component({
  selector: 'meu-login',
  standalone: true,
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss'],
  imports: [CommonModule,
    FormsModule,
    ReactiveFormsModule,
    NzFormModule,
    NzCardModule,
    NzInputModule,
    NzButtonModule,
    NzIconModule,
    NzCheckboxModule,
    NzAlertModule,
    NzFormModule,
    ReactiveFormsModule,
],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LoginComponent implements OnInit {
  loginForm: FormGroup;
  constructor(private fb: FormBuilder, private authService: AuthService) {
    this.loginForm = this.fb.group({
      username: ['', [Validators.required]],
      password: ['', [Validators.required]],
    });
  }
  ngOnInit() {}
  onSubmit() {
    const headers  = new HttpHeaders()
    if (!this.loginForm.valid) {
      alert('vui lòng nhập đầy đủ thông tin');
      return;
    }
    console.log(this.loginForm.value.username, this.loginForm.value.password);

    this.authService.login(this.loginForm.value.username, this.loginForm.value.password).pipe(
      tap(() => {

      }),
      catchError((error) => {
        console.log(error);
        throw error;
      }),
      finalize(() => {})
    ).subscribe(
      response => {
        alert(response.message);
      }
    );
  }
}