<h2>Gestione Strutture</h2>

<div class="actions-header">
  <button mat-raised-button color="primary" (click)="addStructure()">
    <mat-icon>add</mat-icon> Aggiungi Struttura
  </button>
</div>

<div class="spinner-container" *ngIf="isLoading">
  <mat-spinner diameter="50"></mat-spinner>
</div>

<div *ngIf="error" class="error-message">
  {{ error }}
</div>

<div *ngIf="!isLoading && !error">
  <table mat-table [dataSource]="(structures$ | async) ?? []" class="mat-elevation-z8 structure-table">

    <!-- Colonna ID -->
    <ng-container matColumnDef="id">
      <th mat-header-cell *matHeaderCellDef> ID </th>
      <td mat-cell *matCellDef="let structure"> {{structure.id}} </td>
    </ng-container>

    <!-- Colonna Nome -->
    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef> Nome </th>
      <td mat-cell *matCellDef="let structure"> {{structure.name}} </td>
    </ng-container>

    <!-- Colonna Città -->
    <ng-container matColumnDef="city">
      <th mat-header-cell *matHeaderCellDef> Città </th>
      <td mat-cell *matCellDef="let structure"> {{structure.city || '-'}} </td>
    </ng-container>

    <!-- Colonna Email -->
    <ng-container matColumnDef="email">
      <th mat-header-cell *matHeaderCellDef> Email </th>
      <td mat-cell *matCellDef="let structure"> {{structure.email || '-'}} </td>
    </ng-container>

    <!-- Colonna Azioni -->
    <ng-container matColumnDef="actions">
      <th mat-header-cell *matHeaderCellDef> Azioni </th>
      <td mat-cell *matCellDef="let structure">
        <button mat-icon-button color="primary" aria-label="Modifica struttura" (click)="editStructure(structure)">
          <mat-icon>edit</mat-icon>
        </button>
        <button mat-icon-button color="warn" aria-label="Elimina struttura" (click)="deleteStructure(structure)">
          <mat-icon>delete</mat-icon>
        </button>
      </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>

    <!-- Riga da mostrare quando non ci sono dati -->
    <tr class="mat-row" *matNoDataRow>
        <td class="mat-cell" [attr.colspan]="displayedColumns.length">
            Nessuna struttura trovata.
        </td>
    </tr>

  </table>
</div>
