@php
  $reportTitle = $reportTitle ?? 'Report Movimenti';
  $attivitaNome = $attivitaNome ?? 'Attivita';
  $periodoLabel = $periodoLabel ?? 'Periodo non specificato';
  $generatoIl = $generatoIl ?? now()->format('d/m/Y H:i');
  $movimenti = collect($movimenti ?? [])
    ->map(function ($movimento) {
      $rawTs = $movimento['timestamp'] ?? $movimento['data'] ?? null;
      $parsedTs = $rawTs ? strtotime((string) $rawTs) : false;

      if ($parsedTs !== false) {
        $movimento['_sort_ts'] = $parsedTs;
        $movimento['_display_ts'] = date('Y-m-d H:i:s', $parsedTs);
      } else {
        $movimento['_sort_ts'] = 0;
        $movimento['_display_ts'] = '-';
      }

      return $movimento;
    })
    ->sortByDesc('_sort_ts')
    ->values();

  $totEntrate = (float) ($totEntrate ?? $movimenti->where('tipo', 'entrata')->sum('importo'));
  $totUscite = (float) ($totUscite ?? $movimenti->where('tipo', 'uscita')->sum('importo'));
  $saldo = (float) ($saldo ?? ($totEntrate - $totUscite));
@endphp

<style>
  .report-print {
    background: #fff;
    color: #111827;
    padding: 24px;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    font-family: Arial, Helvetica, sans-serif;
  }

  .report-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 18px;
  }

  .report-title {
    font-size: 24px;
    font-weight: 700;
    margin: 0 0 4px;
    color: #111827;
  }

  .report-title-row {
    display: flex;
    align-items: baseline;
    gap: 10px;
    flex-wrap: wrap;
  }

  .report-title-periodo {
    font-size: 20px;
    font-weight: 700;
    color: #374151;
    margin: 0;
  }

  .report-subtitle {
    margin: 0;
    font-size: 13px;
    color: #4b5563;
  }

  .report-meta {
    text-align: right;
    font-size: 12px;
    color: #6b7280;
  }

  .report-cards {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 18px;
  }

  .report-card {
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 10px 12px;
    background: #f9fafb;
  }

  .report-card .label {
    display: block;
    font-size: 12px;
    color: #6b7280;
    margin-bottom: 4px;
  }

  .report-card .value {
    font-size: 18px;
    font-weight: 700;
    color: #111827;
  }

  .report-table {
    width: 100%;
    border-collapse: collapse;
    border: 1px solid #e5e7eb;
  }

  .report-table thead th {
    font-size: 12px;
    font-weight: 700;
    text-align: left;
    padding: 8px 10px;
    color: #374151;
    background: #f3f4f6;
    border-bottom: 1px solid #e5e7eb;
  }

  .report-table tbody td {
    font-size: 12px;
    padding: 8px 10px;
    border-bottom: 1px solid #f3f4f6;
    color: #1f2937;
  }

  .text-right {
    text-align: right;
  }

  .font-mono {
    font-family: "Courier New", Courier, monospace;
    font-variant-numeric: tabular-nums;
  }

  .badge-tipo {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 30px;
    border-radius: 999px;
    padding: 3px 9px;
    font-size: 14px;
    font-weight: 700;
    line-height: 1;
  }

  .badge-entrata {
    color: #166534;
    background: #dcfce7;
  }

  .badge-uscita {
    color: #991b1b;
    background: #fee2e2;
  }

  .report-footer {
    margin-top: 12px;
    font-size: 11px;
    color: #6b7280;
  }

  @media print {
    body {
      background: #fff !important;
    }

    .report-print {
      border: 0;
      border-radius: 0;
      padding: 0;
    }
  }
</style>

<section class="report-print">
  <header class="report-head">
    <div>
      <div class="report-title-row">
        <h1 class="report-title">{{ $reportTitle }}</h1>
    
        <h2 class="report-title-periodo">Periodo: {{ $periodoLabel }}</h2>
      </div>
      <p class="report-subtitle">
        {{ $attivitaNome }}
      </p>
    </div>
    <div class="report-meta">
      Generato il<br>
      <strong>{{ $generatoIl }}</strong>
    </div>
  </header>

  <div class="report-cards">
    <div class="report-card">
      <span class="label">Totale Entrate</span>
      <span class="value">EUR {{ number_format($totEntrate, 2, ',', '.') }}</span>
    </div>
    <div class="report-card">
      <span class="label">Totale Uscite</span>
      <span class="value">EUR {{ number_format($totUscite, 2, ',', '.') }}</span>
    </div>
    <div class="report-card">
      <span class="label">Saldo</span>
      <span class="value">EUR {{ number_format($saldo, 2, ',', '.') }}</span>
    </div>
  </div>

  <table class="report-table">
    <thead>
      <tr>
        <th class="font-mono">Timestamp</th>
        <th>Tipo</th>
        <th class="text-right font-mono">Importo</th>
        <th>Categoria</th>
        <th>Causale</th>
        <th>Metodo</th>
      </tr>
    </thead>
    <tbody>
      @forelse($movimenti as $movimento)
        @php
          $tipo = strtolower((string) ($movimento['tipo'] ?? ''));
          $isEntrata = $tipo === 'entrata';
          $importo = (float) ($movimento['importo'] ?? 0);
        @endphp
        <tr>
          <td class="font-mono">{{ $movimento['_display_ts'] ?? '-' }}</td>
          <td>
            <span class="badge-tipo {{ $isEntrata ? 'badge-entrata' : 'badge-uscita' }}">
              {{ $isEntrata ? '+' : '-' }}
            </span>
          </td>
          <td class="text-right font-mono">
            {{ $isEntrata ? '+' : '-' }} EUR {{ number_format(abs($importo), 2, ',', '.') }}
          </td>
          <td>{{ $movimento['categoria'] ?? '-' }}</td>
          <td>{{ $movimento['causale'] ?? '-' }}</td>
          <td>{{ $movimento['metodo'] ?? '-' }}</td>
        </tr>
      @empty
        <tr>
          <td colspan="6">Nessun movimento trovato nel periodo selezionato.</td>
        </tr>
      @endforelse
    </tbody>
  </table>

  <div class="report-footer">
    Documento a uso interno. Valori espressi in euro.
  </div>
</section>
