@extends('layouts/guest')

@section('title', 'La tua prenotazione')

@section('page-style')
<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;1,400&display=swap" rel="stylesheet" />
@endsection

@section('app-css-extra')
@vite(['resources/css/guest-client.css'])
@endsection

@php
  use Illuminate\Support\Facades\Storage;
  use App\Models\EventoForm;

  $evento = $prenotazione->evento;
  $galleriaRaw = json_decode($evento?->galleria ?? '[]', true);
  $galleriaItems = is_array($galleriaRaw) ? $galleriaRaw : [];
  $coverUrl = null;
  if (count($galleriaItems) > 0 && !empty($galleriaItems[0]['path'])) {
      $coverUrl = Storage::disk('eventi')->url($galleriaItems[0]['path']);
  } elseif ($evento?->copertina) {
      $coverUrl = Storage::disk('eventi')->url($evento->copertina);
  }
@endphp

@section('content')

<style>
  .guest-fest-qr-wrap svg {
    background: #15265c !important;
  }
</style>
<div class="container-xxl flex-grow-1 container-p-y p-10" style="justify-items: anchor-center;">
<div class="guest-fest guest-fest w-px-400">
  
<div class="card-body guest-fest-card-body--compact">
        {{-- Codice + stati prenotazione / pagamento --}}
        <div class="guest-fest-codice-block mb-2">
          @isset($codiceQrSvg)
            <div class="guest-fest-qr-wrap mb-2 " aria-hidden="true">
              {!! $codiceQrSvg !!}
            </div>
          @endisset
          <span class="guest-fest-codice-label">Codice prenotazione</span>
          <div class="guest-fest-codice-big text-break">{{ $prenotazione->id }} # {{ $prenotazione->codice }}</div>
          <div class="mt-3 small d-flex flex-wrap align-items-center gap-2">
            {!! view('prenotazione._partials.stato-prenotazione', ['stato' => $prenotazione->stato]) !!}
            {!! view('prenotazione._partials.stato-prenotazione', ['stato' => $prenotazione->stato_pagamento, 'label_completa' => true]) !!}
          </div>
        </div>

        {{-- Accordion Dettagli: evento (link), contatti, richieste --}}
        <div class="accordion guest-fest-accordion" id="accordionPrenotazioneDettagli">
          <div class="accordion-item border-0">
            <h2 class="accordion-header" id="headingDettagli">
              <button
                class="accordion-button collapsed guest-fest-accordion-btn rounded"
                type="button"
                data-bs-toggle="collapse"
                data-bs-target="#collapseDettagli"
                aria-expanded="false"
                aria-controls="collapseDettagli"
              >
                Dettagli
              </button>
            </h2>
            <div
              id="collapseDettagli"
              class="accordion-collapse collapse"
              aria-labelledby="headingDettagli"
              data-bs-parent="#accordionPrenotazioneDettagli"
            >
              <div class="accordion-body guest-fest-accordion-body pt-4">
                @if($evento)
                  <div class="guest-fest-accordion-event mb-3">
                    <span class="guest-fest-contact-k d-block mb-1">Evento</span>
                    <a
                      href="{{ route('evento.show', ['evento_id' => $evento->id]) }}"
                      class="guest-fest-event-external-link"
                      target="_blank"
                      rel="noopener noreferrer"
                    >
                      <span class="guest-fest-event-link-text">{{ $evento->nome }}</span>
                      <i class="bx bx-link-external ms-1 align-middle" aria-hidden="true"></i>
                    </a>
                  </div>
                @endif

                <div class="guest-fest-contact-grid mb-3">
                  <div class="guest-fest-contact-cell">
                    <span class="guest-fest-contact-k">Nome</span>
                    <span class="guest-fest-contact-v">{{ $prenotazione->nome }} {{ $prenotazione->cognome }}</span>
                  </div>
                  <div class="guest-fest-contact-cell">
                    <span class="guest-fest-contact-k">Telefono</span>
                    <span class="guest-fest-contact-v">{{ $prenotazione->telefono }}</span>
                  </div>
                  <div class="guest-fest-contact-cell">
                    <span class="guest-fest-contact-k">Email</span>
                    <span class="guest-fest-contact-v text-break">{{ $prenotazione->email }}</span>
                  </div>
                  <div class="guest-fest-contact-cell">
                    <span class="guest-fest-contact-k">Note</span>
                    <span class="guest-fest-contact-v">{{ $prenotazione->note ?: '—' }}</span>
                  </div>
                </div>

                @if($evento && $evento->form && $evento->form->count())
                  <h3 class="guest-fest-subsection-title mb-2">Richieste</h3>
                  <div class="guest-fest-table-wrap guest-fest-qa-wrap">
                    <table class="table table-sm mb-0 guest-fest-qa-table">
                      <thead class="visually-hidden">
                        <tr>
                          <th scope="col">Domanda</th>
                          <th scope="col">Risposta</th>
                          <th scope="col">Prezzo</th>
                        </tr>
                      </thead>
                      <tbody>
                      @foreach($evento->form as $form)
                        @php
                          $risposta = $prenotazione->risposte->where('evento_form_id', $form->id)->first();
                          $testoRisposta = '—';
                          if ($form->tipo === EventoForm::ELENCO) {
                              if ($risposta && $form->opzioni_form) {
                                  $testoRisposta = optional($form->opzioni_form->where('id', $risposta->risposta)->first())->label ?? ($risposta->risposta ?? '—');
                              } elseif ($risposta) {
                                  $testoRisposta = $risposta->risposta;
                              }
                          } else {
                              $testoRisposta = $risposta?->risposta ?? '—';
                          }

                          $prezzoRiga = '—';
                          if ($form->tipo === EventoForm::ELENCO && $risposta && $form->opzioni_form) {
                              $opt = $form->opzioni_form->where('id', $risposta->risposta)->first();
                              if ($opt && $opt->costo !== null && (float) $opt->costo > 0) {
                                  $prezzoRiga = number_format((float) $opt->costo, 2, ',', ' ') . ' €';
                              }
                          }
                          if ($prezzoRiga === '—' && $form->costo !== null && (float) $form->costo > 0) {
                              if ($form->tipo === EventoForm::NUMERO && $risposta && is_numeric($risposta->risposta)) {
                                  $prezzoRiga = number_format((float) $risposta->risposta * (float) $form->costo, 2, ',', ' ') . ' €';
                              } else {
                                  $prezzoRiga = number_format((float) $form->costo, 2, ',', ' ') . ' €';
                              }
                         
                          }
                        @endphp
                        <tr>
                          <td class="guest-fest-qa-q">{{ $form->label }}</td>
                          <td class="guest-fest-qa-a">{{ $form->getRisposta($prenotazione->id) }}</td>
                          <td class="guest-fest-qa-p text-nowrap">{{ $prezzoRiga }}</td>
                        </tr>
                      @endforeach
                      </tbody>
                    </table>
                  </div>
                @else
                  <p class="guest-fest-empty mb-0">Nessun campo modulo per questa prenotazione.</p>
                @endif
              </div>
            </div>
          </div>
        </div>

        <div class="guest-fest-actions">
          <a href="{{ url()->previous() }}" class="btn btn-fest-soft">
            <i class="bx bx-arrow-back me-1"></i> Indietro
          </a>
        </div>
      </div>
</div>
</div>
@endsection
