<h2 mat-dialog-title>{{ title }}</h2>

<mat-dialog-content [formGroup]="contractForm">
  <!-- Messaggio errore validatore date -->
   <mat-error *ngIf="contractForm.hasError('dateRange') && (contractForm.get('start_date')?.touched || contractForm.get('end_date')?.touched)">
      La data di fine deve essere successiva alla data di inizio.
   </mat-error>

   <!-- Indicatore caricamento per le select -->
   <div *ngIf="isLoadingSelects" style="text-align: center; padding: 20px;">
       <mat-spinner diameter="40"></mat-spinner>
       <p>Caricamento dati...</p>
   </div>

  <div class="form-grid" *ngIf="!isLoadingSelects">

    <!-- Select Insegnante -->
    <mat-form-field appearance="fill">
      <mat-label>Insegnante</mat-label>
      <mat-select formControlName="teacher_id" required>
        <!-- Itera sull'array teachers -->
        <mat-option *ngFor="let teacher of teachers" [value]="teacher.id">
          {{ teacher.last_name }} {{ teacher.first_name }}
        </mat-option>
      </mat-select>
      <mat-error *ngIf="contractForm.get('teacher_id')?.hasError('required')">
        Selezionare un insegnante è obbligatorio.
      </mat-error>
    </mat-form-field>

    <!-- Select Anno Scolastico -->
    <mat-form-field appearance="fill">
      <mat-label>Anno Scolastico</mat-label>
      <mat-select formControlName="school_year_id" required>
        <!-- Itera sull'array schoolYears -->
        <mat-option *ngFor="let year of schoolYears" [value]="year.id">
          {{ year.name }} ({{ year.start_date | date:'dd/MM/yy' }} - {{ year.end_date | date:'dd/MM/yy' }})
        </mat-option>
      </mat-select>
       <mat-error *ngIf="contractForm.get('school_year_id')?.hasError('required')">
        Selezionare un anno scolastico è obbligatorio.
      </mat-error>
    </mat-form-field>

     <!-- Select Struttura (Opzionale) -->
    <mat-form-field appearance="fill">
      <mat-label>Struttura (Opzionale)</mat-label>
      <mat-select formControlName="structure_id">
         <mat-option [value]="null">-- Nessuna --</mat-option> <!-- Opzione per non selezionare -->
        <mat-option *ngFor="let structure of (structures$ | async)" [value]="structure.id">
          {{ structure.name }}
        </mat-option>
      </mat-select>
    </mat-form-field>

    <!-- Tipo Contratto -->
    <mat-form-field appearance="fill">
      <mat-label>Tipo Contratto</mat-label>
      <input matInput formControlName="contract_type">
    </mat-form-field>

    <!-- Data Inizio -->
    <mat-form-field appearance="fill">
      <mat-label>Data Inizio Contratto</mat-label>
      <input matInput [matDatepicker]="contractStartPicker" formControlName="start_date" required readonly>
      <mat-hint>GG/MM/AAAA</mat-hint>
      <mat-datepicker-toggle matSuffix [for]="contractStartPicker"></mat-datepicker-toggle>
      <mat-datepicker #contractStartPicker></mat-datepicker>
       <mat-error *ngIf="contractForm.get('start_date')?.hasError('required')">
        La data di inizio è obbligatoria.
      </mat-error>
    </mat-form-field>

    <!-- Data Fine (Opzionale) -->
    <mat-form-field appearance="fill">
      <mat-label>Data Fine Contratto (Opzionale)</mat-label>
      <input matInput [matDatepicker]="contractEndPicker" formControlName="end_date" readonly>
      <mat-hint>GG/MM/AAAA</mat-hint>
      <mat-datepicker-toggle matSuffix [for]="contractEndPicker"></mat-datepicker-toggle>
      <mat-datepicker #contractEndPicker></mat-datepicker>
    </mat-form-field>

    <!-- Ore Settimanali -->
     <mat-form-field appearance="fill">
      <mat-label>Ore Settimanali</mat-label>
      <input matInput type="number" formControlName="weekly_hours" min="0" step="0.01">
       <mat-error *ngIf="contractForm.get('weekly_hours')?.hasError('pattern') || contractForm.get('weekly_hours')?.hasError('min')">
        Inserire un numero positivo (max 2 decimali).
      </mat-error>
    </mat-form-field>

    <!-- Stipendio -->
     <mat-form-field appearance="fill">
      <mat-label>Stipendio</mat-label>
      <input matInput type="number" formControlName="salary" min="0" step="0.01">
       <mat-error *ngIf="contractForm.get('salary')?.hasError('pattern') || contractForm.get('salary')?.hasError('min')">
         Inserire un numero positivo (max 2 decimali).
      </mat-error>
    </mat-form-field>

    <!-- Note -->
    <mat-form-field appearance="fill" class="full-width-field">
      <mat-label>Note</mat-label>
      <textarea matInput formControlName="notes" rows="3"></textarea>
    </mat-form-field>

  </div>
</mat-dialog-content>

<mat-dialog-actions align="end">
  <button mat-button (click)="onCancel()">Annulla</button>
  <button mat-raised-button color="primary" (click)="onSave()" [disabled]="!contractForm.valid">
    {{ isEditMode ? 'Salva Modifiche' : 'Aggiungi' }}
  </button>
</mat-dialog-actions>
