@extends('layouts/guest')

@section('title', 'Il tuo scontrino')

@section('page-meta')
  <script>document.documentElement.classList.add('scontrino-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" />
@endsection

@section('page-style')
<style>
  html.scontrino-cliente-page,
  html.scontrino-cliente-page body {
    background: #f4eef8;
  }

  html.scontrino-cliente-page .misc-wrapper {
    text-align: start;
    align-items: flex-start;
    justify-content: flex-start;
    padding: 0;
    max-width: 100%;
    min-block-size: auto;
    background: transparent;
  }

  .scontrino-cliente {
    --fest-orange: #f58220;
    --fest-orange-soft: #ff9a2e;
    --fest-purple: #602d91;
    --fest-navy: #15265c;
    --fest-text: #1a2744;
    --fest-text-muted: #4a5568;
    --fest-radius: 1.15rem;
    --fest-shadow: 0 4px 24px rgba(21, 38, 92, 0.08), 0 1px 3px rgba(21, 38, 92, 0.06);

    position: relative;
    width: 100%;
    max-width: 32rem;
    margin: 0 auto;
    padding: 1rem 0.85rem 2.5rem;
    font-family: "Plus Jakarta Sans", "Public Sans", system-ui, sans-serif;
    color: var(--fest-text);
  }

  .scontrino-cliente__bg {
    position: fixed;
    inset: 0;
    z-index: -1;
    overflow: hidden;
    pointer-events: none;
    background: linear-gradient(145deg, #ffe8d4 0%, #ecd8f5 38%, #d8e4f8 100%);
  }

  .scontrino-cliente__bg::before {
    content: "";
    position: absolute;
    inset: 0;
    background:
      radial-gradient(ellipse 80% 60% at 5% 15%, rgba(245, 130, 32, 0.45) 0%, transparent 55%),
      radial-gradient(ellipse 75% 55% at 98% 88%, rgba(96, 45, 145, 0.38) 0%, transparent 52%);
    pointer-events: none;
  }

  .scontrino-cliente__bg-stripe {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 5px;
    background: linear-gradient(90deg, var(--fest-orange) 0%, var(--fest-purple) 50%, var(--fest-navy) 100%);
  }

  .scontrino-cliente__header {
    text-align: center;
    margin-bottom: 0.25rem;
  }

  .scontrino-cliente__card {
    background: rgba(255, 255, 255, 0.94);
    backdrop-filter: blur(14px);
    border: 1px solid rgba(255, 255, 255, 0.95);
    border-radius: calc(var(--fest-radius) + 0.1rem);
    box-shadow: var(--fest-shadow);
    padding: 1.35rem 1.25rem;
    margin-bottom: 1rem;
  }

  .scontrino-cliente__logo {
    display: block;
    max-height: 44px;
    width: auto;
    margin: 0 auto 1rem;
  }

  .scontrino-cliente__eyebrow {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    margin-bottom: 0.65rem;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--fest-purple);
  }

  .scontrino-cliente__venue-name {
    display: block;
    width: 85%;
    max-width: 85%;
    margin: 0 auto 0.85rem;
    font-size: clamp(1.35rem, 5vw, 1.75rem);
    font-weight: 800;
    line-height: 1.15;
    letter-spacing: -0.03em;
    color: var(--fest-orange);
    word-break: break-word;
    text-align: center;
  }

  .scontrino-cliente__divider {
    margin: 1.15rem 0;
    border: none;
    border-top: 1px dashed rgba(21, 38, 92, 0.12);
  }

  .scontrino-cliente__meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    font-size: 0.875rem;
    color: var(--fest-text-muted);
    margin-bottom: 0.25rem;
  }

  .scontrino-cliente__badge {
    display: inline-flex;
    align-items: center;
    padding: 0.2rem 0.65rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: capitalize;
  }

  .scontrino-cliente__badge--ok {
    background: rgba(34, 197, 94, 0.15);
    color: #15803d;
  }

  .scontrino-cliente__badge--pending {
    background: rgba(21, 38, 92, 0.08);
    color: var(--fest-navy);
  }

  .scontrino-cliente__info-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px dashed rgba(21, 38, 92, 0.1);
  }

  .scontrino-cliente__info-label {
    display: block;
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--fest-text-muted);
    margin-bottom: 0.15rem;
  }

  .scontrino-cliente__info-value {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--fest-navy);
  }

  .scontrino-cliente__note {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    margin-top: 1rem;
    padding: 0.75rem 0.9rem;
    border-radius: 0.85rem;
    background: rgba(245, 130, 32, 0.1);
    border: 1px solid rgba(245, 130, 32, 0.2);
    font-size: 0.875rem;
    color: var(--fest-text);
  }

  .scontrino-cliente__note i {
    color: var(--fest-orange);
    font-size: 1.1rem;
    margin-top: 0.1rem;
  }

  .scontrino-cliente__section-title {
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--fest-text-muted);
    margin: 0 0 0.75rem;
  }

  .scontrino-cliente__items {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
  }

  .scontrino-cliente__item {
    display: flex;
    align-items: center;
    gap: 0.85rem;
    padding: 0.75rem 0.85rem;
    border-radius: 0.9rem;
    background: #fff;
    border: 1px solid rgba(21, 38, 92, 0.06);
    box-shadow: 0 1px 4px rgba(21, 38, 92, 0.04);
  }

  .scontrino-cliente__qty {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 3.25rem;
    padding: 0.4rem 0.5rem;
    border-radius: 0.75rem;
    background: linear-gradient(135deg, var(--fest-orange) 0%, var(--fest-orange-soft) 100%);
    color: #fff;
    font-weight: 800;
    line-height: 1.15;
    box-shadow: 0 2px 8px rgba(245, 130, 32, 0.28);
  }

  .scontrino-cliente__qty small {
    font-size: 0.68rem;
    font-weight: 600;
    opacity: 0.92;
  }

  .scontrino-cliente__item-name {
    flex: 1;
    font-size: 1rem;
    font-weight: 700;
    color: var(--fest-navy);
    line-height: 1.25;
  }

  .scontrino-cliente__item-note {
    display: block;
    margin-top: 0.2rem;
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--fest-text-muted);
  }

  .scontrino-cliente__item-price {
    flex-shrink: 0;
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--fest-navy);
  }

  .scontrino-cliente__empty {
    text-align: center;
    padding: 1.5rem 1rem;
    border-radius: 0.9rem;
    background: rgba(21, 38, 92, 0.04);
    color: var(--fest-text-muted);
    font-size: 0.9rem;
  }

  .scontrino-cliente__total {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 1rem;
    padding: 1rem 1.15rem;
    border-radius: 0.95rem;
    background: linear-gradient(135deg, rgba(245, 130, 32, 0.12) 0%, rgba(255, 154, 46, 0.18) 100%);
    border: 1px solid rgba(245, 130, 32, 0.22);
  }

  .scontrino-cliente__total-label {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--fest-navy);
  }

  .scontrino-cliente__total-value {
    font-size: 1.45rem;
    font-weight: 800;
    color: var(--fest-orange);
    letter-spacing: -0.02em;
  }

  .scontrino-cliente__actions {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
    text-align: center;
  }

  .scontrino-cliente__notify-hint {
    margin: 0;
    font-size: 0.78rem;
    color: var(--fest-text-muted);
    line-height: 1.35;
  }

  .scontrino-cliente__btn-notify {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    width: 100%;
    padding: 0.85rem 1.15rem;
    border: none;
    border-radius: 0.85rem;
    font-size: 1rem;
    font-weight: 700;
    color: #fff;
    background: linear-gradient(135deg, var(--fest-orange) 0%, var(--fest-orange-soft) 100%);
    box-shadow: 0 4px 16px rgba(245, 130, 32, 0.32);
    transition: box-shadow 0.15s ease, transform 0.15s ease;
  }

  .scontrino-cliente__btn-notify:hover:not(:disabled) {
    color: #fff;
    box-shadow: 0 6px 20px rgba(245, 130, 32, 0.4);
    transform: translateY(-1px);
  }

  .scontrino-cliente__btn-notify:disabled {
    opacity: 0.7;
  }

  .scontrino-cliente__success {
    margin: 0;
    padding: 0.75rem 1rem;
    border-radius: 0.85rem;
    background: rgba(34, 197, 94, 0.12);
    border: 1px solid rgba(34, 197, 94, 0.25);
    color: #15803d;
    font-size: 0.875rem;
    font-weight: 600;
    text-align: center;
  }

  .scontrino-cliente__back {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    margin-top: 1.25rem;
    padding: 0.55rem 1rem;
    border-radius: 0.65rem;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--fest-navy);
    background: rgba(255, 255, 255, 0.85);
    border: 1px solid rgba(21, 38, 92, 0.12);
    text-decoration: none;
    transition: background 0.15s ease, border-color 0.15s ease;
  }

  .scontrino-cliente__back:hover {
    color: var(--fest-navy);
    background: #fff;
    border-color: rgba(21, 38, 92, 0.22);
  }
</style>
@endsection

@section('content')
<div class="scontrino-cliente__bg" aria-hidden="true">
  <div class="scontrino-cliente__bg-stripe"></div>
</div>

<div class="scontrino-cliente">
  <div class="scontrino-cliente__card">
    <div class="scontrino-cliente__header">
      <img
        src="{{ asset('assets/img/logo_fest_L.png') }}"
        alt="{{ config('app.name') }}"
        class="scontrino-cliente__logo"
      >
      <p class="scontrino-cliente__eyebrow mb-0">
        <i class="bx bx-receipt"></i> Il tuo scontrino
      </p>
      <span class="scontrino-cliente__venue-name">{{ $ordine->attivita->nome ?? 'Attività' }}</span>
      <div class="scontrino-cliente__meta">
        <span>#{{ $ordine->id }}</span>
        <span>&middot;</span>
        <span>{{ $ordine->created_at ? $ordine->created_at->format('d/m/Y H:i') : '' }}</span>
        <span class="scontrino-cliente__badge scontrino-cliente__badge--{{ $ordine->stato === 'pagato' ? 'ok' : 'pending' }}">
          {{ ucfirst($ordine->stato ?? 'Non disponibile') }}
        </span>
      </div>
    </div>

    <div class="scontrino-cliente__info-grid">
      <div>
        <span class="scontrino-cliente__info-label">Cliente</span>
        <span class="scontrino-cliente__info-value">{{ $ordine->info['cliente'] ?? '—' }}</span>
      </div>
      <div>
        <span class="scontrino-cliente__info-label">Tavolo</span>
        <span class="scontrino-cliente__info-value">{{ $ordine->info['tavolo'] ?? '—' }}</span>
      </div>
    </div>

    @if(!empty($ordine->note))
      <div class="scontrino-cliente__note">
        <i class="bx bx-info-circle"></i>
        <div>
          <strong>Note</strong><br>
          {{ $ordine->note }}
        </div>
      </div>
    @endif

    <hr class="scontrino-cliente__divider">

    <p class="scontrino-cliente__section-title">Cosa hai ordinato</p>

    <ul class="scontrino-cliente__items">
      @forelse($ordine->righe_ordine as $riga)
        <li class="scontrino-cliente__item">
          <div class="scontrino-cliente__qty">
            {{ $riga->quantita }}×
            <small>&euro; {{ number_format($riga->piatto->prezzo ?? 0, 2, ',', '.') }}</small>
          </div>
          <div class="scontrino-cliente__item-name">
            {{ $riga->piatto->nome ?? 'Prodotto' }}
            @if(!empty($riga->note))
              <span class="scontrino-cliente__item-note">
                <i class="bx bx-chat"></i> {{ $riga->note }}
              </span>
            @endif
          </div>
          <div class="scontrino-cliente__item-price">
            &euro; {{ number_format(($riga->piatto->prezzo ?? 0) * $riga->quantita, 2, ',', '.') }}
          </div>
        </li>
      @empty
        <li class="scontrino-cliente__empty">Nessun prodotto nell'ordine.</li>
      @endforelse
    </ul>

    <div class="scontrino-cliente__total">
      <span class="scontrino-cliente__total-label">Totale ordine</span>
      <span class="scontrino-cliente__total-value">&euro; {{ number_format($ordine->prezzo ?? 0, 2, ',', '.') }}</span>
    </div>
  </div>

  <div class="scontrino-cliente__card scontrino-cliente__actions">
    <button type="button" id="btn-notificami" class="scontrino-cliente__btn-notify" data-ordine-id="{{ $ordine->id }}">
      <i class="bx bx-bell"></i> Avvisami quando è pronto
    </button>
    <p class="scontrino-cliente__notify-hint">Ricevi una notifica sul telefono appena l'ordine è pronto.</p>

    <div id="msg-successo" class="scontrino-cliente__success d-none">
      Notifiche attive — puoi chiudere questa pagina.
    </div>
  </div>

  <div class="text-center">
    <a href="{{ url()->previous() }}" class="scontrino-cliente__back">
      <i class="bx bx-arrow-back"></i> Torna indietro
    </a>
  </div>
</div>
@endsection

@section('page-script')
<script>
  window.firebaseConfig = {
    apiKey: @json(config('services.firebase.web_api_key')),
    projectId: @json(config('services.firebase.project_id')),
    messagingSenderId: @json(config('services.firebase.messaging_sender_id')),
    appId: @json(config('services.firebase.app_id')),
  };
  window.firebaseVapidKey = @json(config('services.firebase.vapid_key'));
  window.subscribeUrl = @json(route('riga-ordine-notifica.salva-fcm-token'));
</script>
@vite(['resources/js/ordine-notifica.js'])
@endsection
