@php
  use Illuminate\Support\Facades\Route;
  use App\Models\Attivita;
  use App\Models\Saltacoda;

  /** @var \App\Models\Attivita|null $attivita */
  $attivita = $attivita ?? $saltacoda_ordine?->attivita ?? null;
  $accent = $attivita?->colore ?: '#f58220';
  $festLogoLarge = asset('assets/img/logo_fest_L.png');
  $festLogoFallback = Attivita::defaultLogoUrl();
  $backUrl = $attivita
    ? route('cliente.attivita.show', ['attivita_id' => $attivita->id])
    : (Route::has('welcome') ? route('welcome') : url('/'));
  $istruzioni = Saltacoda::where('attivita_id', $saltacoda_ordine->attivita_id)->first()?->getIstruzioni()
    ?? Saltacoda::defaultIstruzioni();
@endphp

@extends('layouts/guest')

@section('title', 'Pre-ordine · ' . ($saltacoda_ordine->codice ?? 'Saltacoda'))

@section('page-meta')
  <script>document.documentElement.classList.add('saltacoda-cliente-page');</script>
  <link rel="preconnect" href="https://fonts.googleapis.com" />
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
  <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:ital,wght@0,400;0,500;0,600;0,700;0,800;1,400&display=swap" rel="stylesheet" />
  <meta name="theme-color" content="{{ $accent }}">
  <meta name="theme-color" media="(prefers-color-scheme: light)" content="{{ $accent }}">
  <meta name="theme-color" media="(prefers-color-scheme: dark)" content="{{ $accent }}">
  <meta name="color-scheme" content="light">
  <meta name="mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="default">
@endsection

@section('page-style')
  @include('saltacoda._partials.cliente.fest-layout-style')
<style>
  .saltacoda-preordine {
    --saltacoda-topbar-h: 72px;
    position: relative;
    z-index: 1;
    min-height: 100vh;
    min-height: 100dvh;
    width: 100vw;
    margin: 0;
    background: transparent;
  }

  .saltacoda-preordine-topbar {
    height: var(--saltacoda-topbar-h);
  }

  .saltacoda-preordine-content {
    margin-top: var(--saltacoda-topbar-h);
    padding: 0.8rem 0.75rem calc(1rem + env(safe-area-inset-bottom));
    min-height: calc(100vh - var(--saltacoda-topbar-h));
    min-height: calc(100dvh - var(--saltacoda-topbar-h));
  }

  .saltacoda-section {
    background: #fff;
    border: 1px solid color-mix(in srgb, var(--text-main) 8%, transparent);
    border-radius: 1rem;
    box-shadow: 0 5px 14px rgba(21, 38, 92, 0.04);
    padding: 0.95rem;
    margin-bottom: 0.85rem;
  }

  .saltacoda-section-title {
    margin: 0 0 0.7rem;
    font-size: 0.83rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    color: var(--text-muted);
  }

  .saltacoda-info-list {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.45rem;
    margin: 0;
  }

  .saltacoda-info-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.65rem;
  }

  .saltacoda-info-key {
    color: var(--text-muted);
    font-size: 0.86rem;
    margin: 0;
  }

  .saltacoda-info-value {
    color: var(--text-main);
    font-weight: 600;
    font-size: 0.9rem;
    margin: 0;
    text-align: right;
  }

  .saltacoda-riga {
    border: 1px solid color-mix(in srgb, var(--text-main) 8%, transparent);
    border-radius: 0.85rem;
    background: color-mix(in srgb, var(--card-accent) 4%, #fff);
    padding: 0.72rem 0.78rem;
    margin-bottom: 0.55rem;
  }

  .saltacoda-riga:last-child {
    margin-bottom: 0;
  }

  .saltacoda-riga-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.6rem;
  }

  .saltacoda-riga-name {
    font-weight: 600;
    color: var(--text-main);
    margin: 0;
  }

  .saltacoda-riga-prezzo {
    color: var(--card-accent);
    font-weight: 700;
    background: color-mix(in srgb, var(--card-accent) 12%, #fff);
    border-radius: 999px;
    padding: 0.14rem 0.55rem;
    white-space: nowrap;
  }

  .saltacoda-riga-meta {
    margin-top: 0.45rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
  }

  .saltacoda-riga-note {
    color: var(--text-muted);
    font-size: 0.82rem;
    margin: 0;
    word-break: break-word;
  }

  .saltacoda-riga-qta {
    font-size: 0.8rem;
    font-weight: 700;
    border-radius: 999px;
    background: color-mix(in srgb, var(--card-accent) 10%, #fff);
    color: var(--text-main);
    padding: 0.16rem 0.45rem;
  }

  .saltacoda-totali {
    border-top: 1px solid color-mix(in srgb, var(--text-main) 8%, transparent);
    margin-top: 0.75rem;
    padding-top: 0.65rem;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 0.6rem;
  }

  .saltacoda-totale-label {
    font-size: 0.82rem;
    color: var(--text-muted);
    margin: 0;
  }

  .saltacoda-totale-value {
    margin: 0;
    color: var(--text-main);
    font-weight: 800;
    font-size: 1.05rem;
  }

  .saltacoda-codice-wrap {
    text-align: center;
    margin-bottom: 0.8rem;
  }

  .saltacoda-codice-label {
    margin: 0;
    font-size: 0.8rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.02em;
  }

  .saltacoda-codice-value {
    margin: 0.15rem 0 0;
    font-size: 1.6rem;
    line-height: 1.1;
    font-weight: 800;
    color: var(--card-accent);
  }

  .saltacoda-codice-date {
    margin: 0.25rem 0 0;
    font-size: 0.85rem;
    color: var(--text-muted);
  }

  .saltacoda-stato-badge {
    display: inline-block;
    margin-top: 0.35rem;
    padding: 0.2rem 0.65rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: #fff;
    background: linear-gradient(
      135deg,
      color-mix(in srgb, var(--card-accent) 92%, #fff) 0%,
      color-mix(in srgb, var(--card-accent) 78%, #15265c) 100%
    );
  }

  .saltacoda-preordine-content > .sc-istruzione-panel {
    margin-bottom: 0.85rem;
  }

  @media (min-width: 768px) {
    .saltacoda-preordine {
      margin: 0 auto;
      max-width: 680px;
      border-left: 1px solid color-mix(in srgb, var(--text-main) 8%, transparent);
      border-right: 1px solid color-mix(in srgb, var(--text-main) 8%, transparent);
    }
  }
</style>
@endsection

@section('content')
@php
  $infoCliente = $saltacoda_ordine->info;
  if (is_string($infoCliente)) {
      $decodedInfo = json_decode($infoCliente, true);
      $infoCliente = is_array($decodedInfo) ? $decodedInfo : [];
  }
@endphp

<div class="saltacoda-cliente">
  <div class="saltacoda-cliente__bg" aria-hidden="true"></div>

  <div class="saltacoda-preordine">
    <header class="saltacoda-preordine-topbar saltacoda-fest-topbar">
      <a href="{{ route('cliente.saltacoda.show', ['attivita_id' => $saltacoda_ordine->attivita_id]) }}" class="saltacoda-fest-topbar__back" aria-label="Torna al menu">
        <i class="bx bx-arrow-back"></i>
      </a>
      <h1 class="saltacoda-fest-topbar__title">
        Pre-ordine #{{ $saltacoda_ordine->codice ?? '—' }}
      </h1>
      <a href="{{ route('cliente.saltacoda.show', ['attivita_id' => $saltacoda_ordine->attivita_id]) }}" class="saltacoda-fest-btn saltacoda-fest-btn--sm">
        <i class="bx bx-plus"></i> Nuovo
      </a>
      <a href="{{ $backUrl }}" class="saltacoda-fest-topbar__brand" aria-label="{{ config('app.name') }}">
        <img src="{{ $festLogoLarge }}" alt="{{ config('app.name') }}" loading="lazy">
      </a>
    </header>

    <main class="saltacoda-preordine-content">
      @include('saltacoda._partials.cliente.istruzione-panel', [
        'slotData' => $istruzioni['post_invio'] ?? Saltacoda::defaultSlot(),
        'ariaLabel' => 'Messaggio post-invio',
      ])

      <section class="saltacoda-section">
        <h2 class="saltacoda-section-title">Riepilogo</h2>
        <div class="saltacoda-codice-wrap">
          <p class="saltacoda-codice-label">Codice pre-ordine</p>
          <p class="saltacoda-codice-value">#{{ $saltacoda_ordine->codice ?? '—' }}</p>
          <span class="saltacoda-stato-badge">{{ $saltacoda_ordine->stato ?? '—' }}</span>
          <p class="saltacoda-codice-date">
            {{ $saltacoda_ordine->created_at ? $saltacoda_ordine->created_at->format('d/m/Y H:i') : '—' }}
          </p>
        </div>
      </section>

      <section class="saltacoda-section">
        <h2 class="saltacoda-section-title">Dettaglio ordine</h2>
        @if($saltacoda_ordine->riga_ordine && $saltacoda_ordine->riga_ordine->count())
          @foreach($saltacoda_ordine->riga_ordine as $riga)
            <article class="saltacoda-riga">
              <div class="saltacoda-riga-head">
                <p class="saltacoda-riga-name mb-0">{{ $riga->piatto->nome ?? 'Piatto #' . $riga->piatto_id }}</p>
                <span class="saltacoda-riga-prezzo">&euro; {{ number_format((float) $riga->prezzo, 2, ',', '.') }}</span>
              </div>
              <div class="saltacoda-riga-meta">
                <p class="saltacoda-riga-note mb-0">{{ $riga->note ?: 'Nessuna nota' }}</p>
                <span class="saltacoda-riga-qta">x{{ $riga->quantita }}</span>
              </div>
            </article>
          @endforeach
          <div class="saltacoda-totali">
            <div>
              <p class="saltacoda-totale-label">Totale articoli</p>
              <p class="saltacoda-info-value mb-0">{{ $saltacoda_ordine->riga_ordine->sum('quantita') }}</p>
            </div>
            <div class="text-end">
              <p class="saltacoda-totale-label">Totale ordine</p>
              <p class="saltacoda-totale-value">&euro; {{ number_format((float) $saltacoda_ordine->prezzo, 2, ',', '.') }}</p>
            </div>
          </div>
        @else
          <p class="text-muted mb-0">Nessun piatto presente nell'ordine.</p>
        @endif
      </section>

      @if(!empty($infoCliente))
        <section class="saltacoda-section">
          <h2 class="saltacoda-section-title">Informazioni cliente</h2>
          <div class="saltacoda-info-list">
            @foreach($infoCliente as $key => $val)
              <div class="saltacoda-info-row">
                <p class="saltacoda-info-key mb-0">{{ ucfirst(str_replace('_', ' ', $key)) }}</p>
                <p class="saltacoda-info-value mb-0">{{ $val ?: '—' }}</p>
              </div>
            @endforeach
          </div>
        </section>
      @endif
    </main>
  </div>
</div>
@endsection
