@php
  $searchText = implode(' ', array_filter([
    $device['nome'] ?? '',
    $device['attivita_nome'] ?? '',
    $device['ubicazione'] ?? '',
    $device['tipo'] ?? '',
    (string) ($device['id'] ?? ''),
  ]));
  $attivitaId = $device['attivita_id'] ?? null;
  $attivitaLabel = $device['attivita_nome'] ?? 'Senza attività';
  $editUrl = ($device['is_punto_vendita'] ?? false) && ($puoModificarePv ?? false)
    ? route('punto-vendita.edit', ['punto_vendita_id' => $device['id']])
    : null;
@endphp

<div class="col-sm-6 col-lg-4 col-xl-3">
  <div
    class="card md-device-card h-100"
    data-tipo="{{ $device['tipo'] ?? '_altro' }}"
    data-attivita-id="{{ $attivitaId ?? '0' }}"
    data-attivo="{{ !empty($device['is_attivo']) ? '1' : '0' }}"
    data-search="{{ $searchText }}"
  >
    <div class="card-body d-flex flex-column">
      <div class="d-flex align-items-start justify-content-between gap-2 mb-2">
        <div class="d-flex align-items-center gap-2 min-w-0">
          <span class="avatar avatar-sm">
            <span class="avatar-initial rounded bg-label-primary">
              <i class="{{ $tipoMeta['icon'] ?? 'bx bx-devices' }}"></i>
            </span>
          </span>
          <div class="min-w-0">
            <h6 class="mb-0 text-truncate" title="{{ $device['nome'] }}">{{ $device['nome'] }}</h6>
            <small class="text-muted">#{{ $device['id'] }}</small>
          </div>
        </div>
        @if(!empty($device['is_attivo']))
          <span class="badge bg-label-success flex-shrink-0">Attivo</span>
        @else
          <span class="badge bg-label-danger flex-shrink-0">Non attivo</span>
        @endif
      </div>

      <ul class="list-unstyled small mb-3 flex-grow-1">
        <li class="mb-2 d-flex align-items-start gap-2">
          <i class="bx bx-category-alt text-muted mt-1"></i>
          <span class="text-truncate" title="{{ $attivitaLabel }}">{{ $attivitaLabel }}</span>
        </li>
        <li class="mb-2 d-flex align-items-start gap-2">
          <i class="bx bx-map-pin text-muted mt-1"></i>
          <span>{{ $device['ubicazione'] ?: '—' }}</span>
        </li>
        @if($device['is_punto_vendita'] ?? false)
          <li class="d-flex align-items-start gap-2">
            <i class="bx bx-link text-muted mt-1"></i>
            <span>
              @if(!empty($device['occupato']))
                <span class="badge bg-label-info">Abbinato</span>
              @else
                <span class="badge bg-label-secondary">Libero</span>
              @endif
            </span>
          </li>
        @endif
      </ul>

      @if($editUrl)
        <a href="{{ $editUrl }}" class="btn btn-sm btn-label-primary w-100">
          <i class="bx bx-edit-alt me-1"></i> Gestisci
        </a>
      @endif
    </div>
  </div>
</div>
