<?php
use App\Models\Role;
use Illuminate\Support\Facades\Auth;
?>
@php
$configData = Helper::appClasses();
$cucine = $cucine ?? collect();
@endphp

@extends('layouts/layoutMaster')

@section('title', 'Kiosk')

@section('vendor-style')
@vite([
'resources/assets/vendor/libs/datatables-bs5/datatables.bootstrap5.scss',
'resources/assets/vendor/libs/datatables-responsive-bs5/responsive.bootstrap5.scss',
'resources/assets/vendor/libs/datatables-buttons-bs5/buttons.bootstrap5.scss',
'resources/assets/vendor/libs/flatpickr/flatpickr.scss',
'resources/assets/vendor/libs/@form-validation/form-validation.scss',
'resources/assets/vendor/scss/pages/front-page-payment.scss',
])
@endsection

<!-- Vendor Scripts -->
@section('vendor-script')
@vite([
'resources/assets/vendor/libs/moment/moment.js',
'resources/assets/vendor/libs/flatpickr/flatpickr.js',
'resources/assets/vendor/libs/@form-validation/popular.js',
'resources/assets/vendor/libs/@form-validation/bootstrap5.js',
'resources/assets/vendor/libs/@form-validation/auto-focus.js',
'resources/assets/vendor/libs/@form-validation/popular.js',
'resources/assets/vendor/libs/@form-validation/bootstrap5.js',
'resources/assets/vendor/libs/@form-validation/auto-focus.js',
'resources/assets/vendor/libs/datatables-bs5/datatables-bootstrap5.js'
])
@endsection

@section('pageTitle')
<div class="d-flex flex-column">
  <h4 class="mb-1"> 
    <i class="bx bx-store"></i> <span class="text-primary">{{ $punto_vendita->nome }}</span>
    <span class="text-muted fs-6 fw-normal">· Kiosk</span>
  </h4>
</div>
@endsection

@section('content')
<style>
  div.upload button:first-child{
    display: none !important;
  }

.bx-chef-hat{
  --svg: url("data:image/svg+xml,%3csvg width='24' height='24' fill='currentColor' viewBox='0 0 24 24' transform='' xmlns='http://www.w3.org/2000/svg'%3e%3c!--Boxicons v3.0.8 https://boxicons.com %7c License https://docs.boxicons.com/free--%3e%3cpath d='M17.13 5.54C16.33 3.42 14.32 2 12 2S7.67 3.42 6.87 5.54A5.506 5.506 0 0 0 2 11c0 2.07 1.18 3.95 3 4.88V18c0 .55.45 1 1 1h12c.55 0 1-.45 1-1v-2.12c1.82-.93 3-2.81 3-4.88 0-2.82-2.13-5.15-4.87-5.46m.53 8.75c-.4.14-.67.52-.67.94v1.78H7v-1.78c0-.42-.27-.8-.67-.94-1.4-.5-2.33-1.82-2.33-3.28 0-1.93 1.57-3.5 3.42-3.5.04 0 .14.01.18.02.49 0 .9-.31 1-.78.36-1.61 1.76-2.73 3.41-2.73s3.05 1.12 3.41 2.73c.1.47.51.81 1 .78.06 0 .12 0 .09-.01 1.93 0 3.5 1.57 3.5 3.5 0 1.47-.94 2.79-2.33 3.28ZM5 20h14v2H5z'%3e%3c/path%3e%3c/svg%3e");
}
.list-group-item{
  border: none !important;
}

  /* Area piatti scrollabile: richiede altezza massima + min-h-0 sulla catena flex */
  .kiosk-main-card {
    max-height: calc(100vh - 9rem);
    min-height: 0;
  }
  .kiosk-piatti-scroll {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-gutter: stable;
    padding-right: 0.25rem;
  }
  @media (max-width: 1199.98px) {
    .kiosk-main-card {
      max-height: none;
    }
    .kiosk-piatti-scroll {
      max-height: min(70vh, 32rem);
    }
  }
</style>

@include('_partials.status', ['success' => session('success'), 'error' => session('error')])

<div class="row g-4 justify-content-center mt-0">
  <div class="bg-white col-12 col-xl-8 d-flex flex-row flex-nowrap align-items-stretch p-5 rounded-5 shadow kiosk-main-card gap-3 gap-xl-4 min-h-0">

  <!-- menu -->
  <div class="col-7 d-flex flex-column align-items-stretch min-h-0 pe-2 pe-xl-3">
    <span class="fs-2 fw-bolder text-primary text-uppercase flex-shrink-0">Ciao 😊</span>
    <span class="fs-5 text-muted text-primary flex-shrink-0 mb-3">seleziona un piatto per iniziare</span>

    <!-- cucine + piatti: solo questa zona scorre -->
    <div class="kiosk-piatti-scroll">
    <div class="d-flex flex-column gap-2 w-100">
      @foreach($cucine as $cucina)
        <ul class="list-group list-group-flush mt-5">
          <li class="list-group-item fw-bolder text-primary">
          <span>{{ $cucina->nome }}</span>
        </li>
          @foreach($cucina->piatti as $piatto)
            <li class="list-group-item">
              <div
                class="cassa-piatto-card card border shadow-sm h-100"
                tabindex="0"
                role="button"
                data-piatto-id="{{ $piatto->id }}"
                data-piatto-nome="{{ $piatto->nome }}"
                data-piatto-prezzo="{{ $piatto->prezzo }}"
                data-cucina-id="{{ $cucina->id }}"
                aria-pressed="false"
                style="cursor:pointer;"
              >
                <div class="cassa-piatto-body card-body d-flex flex-column px-3 py-2">
                  <div class="d-flex align-items-center mb-2 gap-2">
                    <div class="cassa-piatto-thumb-wrap rounded bg-light d-flex justify-content-center align-items-center me-2" style="width:52px; height:52px;">
                      <span class="cassa-piatto-thumb-placeholder"><i class="bx bx-dish text-secondary fs-2"></i></span>
                    </div>
                    <div class="flex-grow-1 min-w-0">
                      <div class="cassa-piatto-title text-truncate text-primary">{{ $piatto->nome }}</div>
                      <div class="cassa-piatto-id">{{ $piatto->descrizione }}</div>
                    </div>
                  </div>
                  <div class="mt-1 d-flex justify-content-between align-items-end">
                    <span class="cassa-piatto-price">{{ number_format($piatto->prezzo, 2, ',', '.') }} €</span>
                    {{-- Allergeni/Placeholder --}}
                    <div class="cassa-piatto-allergeni flex-wrap d-flex gap-1">
                      @if($piatto->allergeni && count($piatto->allergeni))
                        @foreach($piatto->allergeni as $allergene)
                          <span class="cassa-allerg-pill">{{ $allergene->sigla ?? $allergene->nome }}</span>
                        @endforeach
                      @else
                        <span class="cassa-allerg-placeholder">Nessun allergene</span>
                      @endif
                    </div>
                  </div>
                </div>
              </div>
            </li>
          @endforeach
        </ul>
      @endforeach
    </div>
    </div>
  </div>

  

  <!-- il mio ordine -->

<div class="col-5 d-flex flex-column min-h-0 ps-2 ps-xl-3">
  <span class="fs-2 fw-bolder text-primary text-uppercase flex-shrink-0">Il mio ordine</span>
  
  <div>
    <h4 class="mb-0 text-primary fw-bold border-bottom border-primary border-radius-0" id="totale_carrello" name="totale_carrello"> 
      @if($ordine->totale > 0)
        € {{ number_format($ordine->totale, 2, ',', '.') }}
      @else
      € {{ number_format($ordine->righe_ordine->sum('prezzo'), 2, ',', ',') }}
      @endif

    </h4>
    <small class="mb-0 text-primary">Totale</small>
  </div>
  
  <div class="flex-grow-1 min-h-0 overflow-auto">
    {{ $dataTable_rigaordine->table() }}
  </div>
  <div class="mt-3 d-flex gap-2">
    <button class="btn btn-danger w-100" id="btn-annulla" onclick="azzeraCarrello()">Svuota carrello</button>
    <a href="{{ route('punto-vendita.kiosk.riepilogo', ['ordine_id' => $ordine->id]) }}" class="btn btn-success w-100" id="btn-checkout" onclick="checkout()">Vai al pagamento </a>
  </div>
</div>

  </div>
</div>

@endsection

@section('page-script')


{{ $dataTable_rigaordine->scripts(attributes: ['type' => 'module']) }}
<script>

function avanzamento(button) {
  // Trova il genitore (parent) con classe 'kiosk-step' partendo dal button (o altro riferimento)
  let current = button.closest('.kiosk-step');
  if (current) {
    // Nasconde tutti gli step
    document.querySelectorAll('.kiosk-step').forEach(el => {
      el.classList.remove('active');
      el.style.display = 'none';
    });

    // Trova il prossimo fratello visibile con classe 'kiosk-step', oppure passa al primo se non c'è
    let sibling = current.nextElementSibling;
    while (sibling && !sibling.classList.contains('kiosk-step')) {
      sibling = sibling.nextElementSibling;
    }
    if (!sibling) {
      // Se non ci sono altri step dopo, rimani sull'ultimo (o opzionalmente puoi tornare al primo)
      sibling = current;
    }
    sibling.classList.add('active');
    sibling.style.display = 'block';
  }
}

</script>

@include('punto_vendita.cassa._partials.scripts')

@endsection
