@php
  $reportTitle = $reportTitle ?? 'Report Cucine';
  $attivitaNome = $attivitaNome ?? 'Attivita';
  $periodoLabel = $periodoLabel ?? 'Periodo non specificato';
  $generatoIl = $generatoIl ?? now()->format('d/m/Y H:i');

  $rows = collect($cucineReportRows ?? [])->map(function ($row) {
    $ordini = (int) ($row['ordini'] ?? 0);
    $righe = (int) ($row['righe'] ?? 0);
    $quantita = (int) ($row['quantita'] ?? 0);
    $incasso = (float) ($row['incasso'] ?? 0);

    return [
      'cucina' => (string) ($row['cucina'] ?? 'Cucina'),
      'ordini' => $ordini,
      'righe' => $righe,
      'quantita' => $quantita,
      'incasso' => $incasso,
    ];
  });

  if ($rows->isEmpty() && isset($cucine)) {
    $rows = collect($cucine)->map(function ($cucina) {
      return [
        'cucina' => $cucina->nome ?? 'Cucina',
        'ordini' => 0,
        'righe' => 0,
        'quantita' => 0,
        'incasso' => 0.0,
      ];
    });
  }

  $rows = $rows->sortByDesc('incasso')->values();

  $totOrdini = (int) $rows->sum('ordini');
  $totRighe = (int) $rows->sum('righe');
  $totQuantita = (int) $rows->sum('quantita');
  $totIncasso = (float) $rows->sum('incasso');
@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-currency {
    margin-bottom: 10px;
    font-size: 12px;
    color: #6b7280;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
  }

  .report-cards {
    display: grid;
    grid-template-columns: repeat(4, 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;
    text-transform: uppercase;
    letter-spacing: 0.03em;
  }

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

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

  .report-table tfoot td {
    font-size: 12px;
    font-weight: 700;
    padding: 8px 10px;
    border-top: 2px solid #d1d5db;
    background: #f9fafb;
  }

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

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

  .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-currency">Valuta: EUR</div>

  <div class="report-cards">
    <div class="report-card">
      <span class="label">Cucine</span>
      <span class="value">{{ number_format($rows->count(), 0, ',', '.') }}</span>
    </div>
    <div class="report-card">
      <span class="label">Ordini gestiti</span>
      <span class="value">{{ number_format($totOrdini, 0, ',', '.') }}</span>
    </div>
    <div class="report-card">
      <span class="label">Righe/Portate</span>
      <span class="value">{{ number_format($totRighe, 0, ',', '.') }}</span>
    </div>
    <div class="report-card">
      <span class="label">Incasso totale</span>
      <span class="value">{{ number_format($totIncasso, 2, ',', '.') }}</span>
    </div>
  </div>

  <table class="report-table">
    <thead>
      <tr>
        <th>Cucina</th>
        <th class="text-right font-mono">Ordini</th>
        <th class="text-right font-mono">Righe</th>
        <th class="text-right font-mono">Quantita</th>
        <th class="text-right font-mono">Incasso</th>
      </tr>
    </thead>
    <tbody>
      @forelse($rows as $row)
        <tr>
          <td>{{ $row['cucina'] }}</td>
          <td class="text-right font-mono">{{ number_format($row['ordini'], 0, ',', '.') }}</td>
          <td class="text-right font-mono">{{ number_format($row['righe'], 0, ',', '.') }}</td>
          <td class="text-right font-mono">{{ number_format($row['quantita'], 0, ',', '.') }}</td>
          <td class="text-right font-mono">{{ number_format($row['incasso'], 2, ',', '.') }}</td>
        </tr>
      @empty
        <tr>
          <td colspan="5">Nessun dato cucina disponibile nel periodo selezionato.</td>
        </tr>
      @endforelse
    </tbody>
    @if($rows->isNotEmpty())
      <tfoot>
        <tr>
          <td>Totale</td>
          <td class="text-right font-mono">{{ number_format($totOrdini, 0, ',', '.') }}</td>
          <td class="text-right font-mono">{{ number_format($totRighe, 0, ',', '.') }}</td>
          <td class="text-right font-mono">{{ number_format($totQuantita, 0, ',', '.') }}</td>
          <td class="text-right font-mono">{{ number_format($totIncasso, 2, ',', '.') }}</td>
        </tr>
      </tfoot>
    @endif
  </table>

  <div class="report-footer">
    Report per cucina dell'attivita nel periodo selezionato.
  </div>
</section>