import { HttpClient } from '@angular/common/http';
import { Component, Inject, inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
import { environment } from '../../../environments/environment';
import { Penalty } from './model/penalty.interface';
import { MatButtonModule } from '@angular/material/button';
import { MatListModule } from '@angular/material/list';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatInputModule } from '@angular/material/input';

@Component({
  selector: 'app-penalties',
  standalone: true,
  imports: [MatDialogModule, MatButtonModule, MatListModule, CommonModule, MatInputModule, FormsModule, ReactiveFormsModule],
  templateUrl: './penalties.component.html',
  styleUrl: './penalties.component.scss'
})
export class PenaltiesComponent {

  searchStr = "";

  private http: HttpClient = inject(HttpClient);
  public penalties: Penalty[] = [];
  constructor(
    public dialogRef: MatDialogRef<PenaltiesComponent>,
        @Inject(MAT_DIALOG_DATA) public teamId: string,
  ) {}

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

  get getPenalties(): any[] {

    return this.penalties.filter(p => p.description.toLocaleLowerCase().includes(this.searchStr.toLowerCase()))
    return [];
  }

  loadPenalties() {
    const url = `${environment.apiUrl}penalty/` + this.teamId;
    this.http.get(url).subscribe({
      next: n => {
        this.penalties = n as Penalty[];
      }
    })
  }

}
