@foreach($chunk->values() as $rowIndex => $item)
@php
$registro = $item['registro'];
$qrImagenDataUri = $item['qrImagenDataUri'];
$rowTop = $marginTop + ($rowIndex * $rowHeight);
$qrLeft = $marginLeft + $halfWidth + ($halfWidth - $qrSize) / 2;
$qrTop = $rowTop + ($rowHeight - $qrSize) / 2;
@endphp
{{-- Diseño del gafete (mitad izquierda) --}}
@include('pdfs.partials.gafete_page', compact('registro', 'plantilla', 'qrImagenDataUri', 'page'))
{{-- QR en fondo blanco (mitad derecha) --}}

@endforeach
@endforeach
@elseif(($formatoImpresion ?? '4_por_hoja') === '1_por_hoja')
@php
$paperWidth = (float) ($page['width'] ?? 612);
$paperHeight = (float) ($page['height'] ?? 792);
$usableWidth = max(1.0, $paperWidth - $marginLeft - $marginRight);
$usableHeight = max(1.0, $paperHeight - $marginTop - $marginBottom);
$badgeWidth = (float) ($page['width'] ?? $paperWidth);
$badgeHeight = (float) ($page['height'] ?? $paperHeight);
$scale = min($usableWidth / max($badgeWidth, 1), $usableHeight / max($badgeHeight, 1));
$renderedWidth = $badgeWidth * $scale;
$renderedHeight = $badgeHeight * $scale;
$offsetLeft = $marginLeft + max(0, ($usableWidth - $renderedWidth) / 2);
$offsetTop = $marginTop + max(0, ($usableHeight - $renderedHeight) / 2);
@endphp
@foreach($items as $item)
@php
$registro = $item['registro'];
$qrImagenDataUri = $item['qrImagenDataUri'];
@endphp
@include('pdfs.partials.gafete_page', compact('registro', 'plantilla', 'qrImagenDataUri', 'page'))
@endforeach
@else
@php
$paperWidth = 612.0;
$paperHeight = 792.0;
$usableWidth = max(1.0, $paperWidth - $marginLeft - $marginRight);
$usableHeight = max(1.0, $paperHeight - $marginTop - $marginBottom);
$slotWidth = $usableWidth / 2;
$slotHeight = $usableHeight / 2;
$badgeWidth = (float) ($page['width'] ?? $slotWidth);
$badgeHeight = (float) ($page['height'] ?? $slotHeight);
$scale = min($slotWidth / max($badgeWidth, 1), $slotHeight / max($badgeHeight, 1));
$renderedWidth = $badgeWidth * $scale;
$renderedHeight = $badgeHeight * $scale;
$offsetLeft = max(0, ($slotWidth - $renderedWidth) / 2);
$offsetTop = max(0, ($slotHeight - $renderedHeight) / 2);
@endphp
@foreach($items->chunk(4) as $chunk)
@foreach($chunk->values() as $slotIndex => $item)
@php
$registro = $item['registro'];
$qrImagenDataUri = $item['qrImagenDataUri'];
$row = intdiv($slotIndex, 2);
$col = $slotIndex % 2;
$slotLeft = $marginLeft + ($col * $slotWidth);
$slotTop = $marginTop + ($row * $slotHeight);
@endphp
@include('pdfs.partials.gafete_page', compact('registro', 'plantilla', 'qrImagenDataUri', 'page'))
@endforeach
@endforeach
@endif