@extends('layouts/guest')

@section('title', 'Saltacoda')

@section('page-meta')
<meta name="theme-color" content="#696cff">
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#696cff">
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#696cff">
<meta name="color-scheme" content="light dark">
<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('pageTitle')
<div class="d-flex flex-column">
  <h4 class="mb-1"> 
    <i class="bx bx-receipt"></i> Pre-ordine #{{ $saltacoda_ordine->codice }}
  </h4>
</div>
@endsection

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

  .misc-wrapper {
    display: block !important;
    width: 100% !important;
    min-block-size: 100vh !important;
    text-align: initial !important;
  }

  .saltacoda-preordine {
    --saltacoda-topbar-h: 72px;
    --saltacoda-bg: #f2f4fb;
    min-height: 100vh;
    min-height: 100dvh;
    width: 100vw;
    margin: -1rem -0.75rem 0;
    background: var(--saltacoda-bg);
  }

  .saltacoda-preordine-topbar {
    position: fixed;
    inset: 0 0 auto 0;
    height: var(--saltacoda-topbar-h);
    z-index: 1020;
    background: #fff;
    border-bottom: 1px solid #e7e7ef;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.65rem;
    padding: 0 1rem;
    box-shadow: 0 2px 10px rgba(16, 24, 40, 0.06);
  }

  .saltacoda-preordine-title {
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    color: #566a7f;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

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

  .saltacoda-section {
    background: #fff;
    border: 1px solid #eaebf4;
    border-radius: 1rem;
    box-shadow: 0 5px 14px rgba(24, 34, 66, 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: #8a93a5;
  }

  .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: #8a93a5;
    font-size: 0.86rem;
    margin: 0;
  }

  .saltacoda-info-value {
    color: #566a7f;
    font-weight: 600;
    font-size: 0.9rem;
    margin: 0;
    text-align: right;
  }

  .saltacoda-riga {
    border: 1px solid #ebedf6;
    border-radius: 0.85rem;
    background: #fbfcff;
    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: #566a7f;
    margin: 0;
  }

  .saltacoda-riga-prezzo {
    color: #5e63db;
    font-weight: 700;
    background: #f0f1ff;
    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: #8a93a5;
    font-size: 0.82rem;
    margin: 0;
    word-break: break-word;
  }

  .saltacoda-riga-qta {
    font-size: 0.8rem;
    font-weight: 700;
    border-radius: 999px;
    background: #eef1fb;
    color: #566a7f;
    padding: 0.16rem 0.45rem;
  }

  .saltacoda-totali {
    border-top: 1px solid #efeff6;
    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: #8a93a5;
    margin: 0;
  }

  .saltacoda-totale-value {
    margin: 0;
    color: #13b95c;
    font-weight: 700;
    font-size: 1.05rem;
  }

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

  .saltacoda-codice-label {
    margin: 0;
    font-size: 0.8rem;
    color: #8a93a5;
    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: #5e63db;
  }

  .saltacoda-codice-date {
    margin: 0.25rem 0 0;
    font-size: 0.85rem;
    color: #8a93a5;
  }

  @media (min-width: 768px) {
    .saltacoda-preordine {
      margin: -1rem auto 0;
      max-width: 680px;
      border-left: 1px solid #efeff6;
      border-right: 1px solid #efeff6;
    }
  }
</style>

<div class="saltacoda-preordine">
  <header class="saltacoda-preordine-topbar">
    <h1 class="saltacoda-preordine-title">
      Pre-ordine #{{ $saltacoda_ordine->codice ?? '—' }}
    </h1>
    <a href="{{ route('cliente.saltacoda.show', ['attivita_id' => $saltacoda_ordine->attivita_id]) }}" class="btn btn-success">
      <i class="bx bx-plus"></i> Nuovo pre-ordine
    </a>
  </header>

  <main class="saltacoda-preordine-content">
    <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>
        <p class="badge bg-primary text-uppercase">{{ $saltacoda_ordine->stato ?? '—' }}</p>
        <p class="saltacoda-codice-date">
          {{ $saltacoda_ordine->created_at ? $saltacoda_ordine->created_at->format('d/m/Y H:i') : '—' }}
        </p>
      </div>
      <div class="saltacoda-info-list">
        <!-- <div class="saltacoda-info-row">
          <p class="saltacoda-info-key">Riferimento</p>
          <p class="saltacoda-info-value">{{ $saltacoda_ordine->riferimento ?? '—' }}</p>
        </div>
        <div class="saltacoda-info-row">
          <p class="saltacoda-info-key">Stato</p>
          <p class="saltacoda-info-value">{{ $saltacoda_ordine->stato ?? '—' }}</p>
        </div> -->
      </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>
@endsection
