@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));
  $saldoClass = $saldo >= 0 ? 'value-positive' : 'value-negative';
@endphp

<style>

  @page {
    margin: 68px 28px 44px;
  }

  .report-print {
    background: #fff;
    color: #0f172a;
    padding: 0;
    /* border: 1px solid #dbe3ee; */
    border-radius: 10px;
    box-shadow: 0 8px 22px rgba(15, 23, 42, 0.05);
    overflow: hidden;
    font-family: "DejaVu Sans", "Segoe UI", Arial, sans-serif;
  }

  .report-topline {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin: 16px 18px 10px;
  }

  .report-pill {
    margin-top:10px;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    border: 1px solid #cbd5e1;
    border-radius: 999px;
    padding: 6px 12px;
    background: #f8fafc;
    font-size: 12px;
    color: #0f172a;
    white-space: nowrap;
  }

  .report-pill-label {
    font-weight: 700;
    color: #64748b;
    text-transform: uppercase;
    letter-spacing: 0.04em;
  }

  .report-pill-value {
    font-weight: 700;
    color: #0f172a;
  }

  .report-title-wrap {
    margin: 0 18px 14px;
    text-align: center;
  }

  .report-title {
    font-size: 22px;
    font-weight: 700;
    margin: 0;
    color: #0f172a;
    letter-spacing: 0.03em;
    text-transform: uppercase;
  }

  .report-title-sub {
    margin-top: 2px;
    font-size: 12px;
    color: #64748b;
    letter-spacing: 0.03em;
    text-transform: uppercase;
  }

  .report-content {
    padding: 0 18px 18px;
  }

  .report-cards {
    display: table;
    table-layout: fixed;
    width: 100%;
    border-spacing: 10px 0;
    border-collapse: separate;
    margin-bottom: 18px;
    margin-left: -10px;
    margin-right: -10px;
  }

  .report-card {
    display: table-cell;
    border: 1px solid #dbe3ee;
    border-radius: 10px;
    padding: 10px 12px;
    vertical-align: middle;
  }
  .report-card--entrate {
    background: #ecfdf3;
    border-color: #bbf7d0;
  }
  .report-card--uscite {
    background: #fef2f2;
    border-color: #fecaca;
  }
  .report-card--saldo {
    background: #eff6ff;
    border-color: #bfdbfe;
  }

  .report-card .label {
    display: block;
    font-size: 12px;
    color: #475569;
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
  }

  .report-card .value {
    font-size: 19px;
    font-weight: 700;
    color: #0f172a;
  }

  .report-card--entrate .label,
  .report-card--entrate .value {
    color: #166534;
  }

  .report-card--uscite .label,
  .report-card--uscite .value {
    color: #991b1b;
  }

  .report-card--saldo .label,
  .report-card--saldo .value {
    color: #1d4ed8;
  }

  .report-table {
    width: 100%;
    border-collapse: collapse;
    border: 1px solid #dbe3ee;
    border-radius: 8px;
    overflow: hidden;
  }

  .report-table thead th {
    font-size: 12px;
    font-weight: 700;
    text-align: left;
    padding: 9px 10px;
    color: #0f172a;
    background: #e2e8f0;
    border-bottom: 1px solid #cbd5e1;
    text-transform: uppercase;
    letter-spacing: 0.03em;
  }
  .report-table thead {
    display: table-header-group;
  }
  .report-table tfoot {
    display: table-row-group;
  }

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

  .report-table tbody tr:nth-child(even) {
    background: #fcfdff;
  }

  .report-table tbody tr:hover {
    background: #f1f5f9;
  }
  .report-table tr {
    page-break-inside: avoid;
  }

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

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

  .report-causale {
    font-weight: 600;
    color: #0f172a;
  }

  .report-footer {
    margin-top: 12px;
    font-size: 11px;
    color: #64748b;
    border-top: 1px dashed #dbe3ee;
    padding-top: 8px;
  }

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

    .report-print {
      border: 0;
      border-radius: 0;
      padding: 0;
      box-shadow: none;
    }

    .report-table tbody tr:hover {
      background: transparent;
    }
  }
</style>

<section class="report-print">
  <div class="report-topline">
    <div class="report-pill">
      <span class="report-pill-label">Attivita</span>
      <span class="report-pill-value">{{ $attivitaNome }}</span>
    </div>
    <div class="report-pill">
      <span class="report-pill-label">Periodo</span>
      <span class="report-pill-value">{{ $periodoLabel }}</span>
    </div>
  </div>

  <div class="report-title-wrap">
    <h1 class="report-title">Elenco dei movimenti in EURO</h1>
    <div class="report-title-sub">Generato il {{ $generatoIl }}</div>
  </div>

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

    <table class="report-table">
      <thead>
        <tr>
          <th class="font-mono">Data e ora</th>
          <th class="text-right font-mono">Mov. Dare</th>
          <th class="text-right font-mono">Mov. Avere</th>
          <th>Descrizione operazioni</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 class="text-right font-mono">
              {{ $isEntrata ? '-' : number_format(abs($importo), 2, ',', '.') }}
            </td>
            <td class="text-right font-mono">
              {{ $isEntrata ? number_format(abs($importo), 2, ',', '.') : '-' }}
            </td>
            <td class="report-causale">{{ $movimento['causale'] ?? '-' }}</td>
          </tr>
        @empty
          <tr>
            <td colspan="4">Nessun movimento trovato nel periodo selezionato.</td>
          </tr>
        @endforelse
      </tbody>
    </table>

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