<?php
    $details = $record->details ?? collect();
    $details = $details instanceof \Illuminate\Support\Collection ? $details : collect($details);

    $getWarehouseId = fn ($detail): int => (int) ($detail->warehouse_assigned ?? $detail->idSucursal ?? $detail->id_sucursal ?? 0);
    $getWarehouseRaw = fn ($detail) => $detail->warehouse_assigned ?? $detail->idSucursal ?? $detail->id_sucursal ?? null;

    $warehouseIds = $details
        ->map(fn ($detail) => $getWarehouseId($detail))
        ->filter(fn (int $id) => $id > 0)
        ->unique()
        ->values();

    $warehouseNamesById = $warehouseIds->isNotEmpty()
        ? \App\Models\Universal\UniversalSucursal::query()
            ->whereIn('id_sucursal', $warehouseIds)
            ->pluck('name_sucursal', 'id_sucursal')
            ->all()
        : [];
?>

<div class="space-y-3">
    <div class="text-sm text-gray-600">
        Referencia: <span class="font-medium"><?php echo e($record->order_reference); ?></span>
    </div>

    <div class="overflow-x-auto rounded-lg border border-gray-200 bg-white">
        <table class="min-w-full text-sm">
            <thead class="bg-gray-50">
                <tr class="text-left text-gray-600">
                    <th class="px-3 py-2 font-medium">Código</th>
                    <th class="px-3 py-2 font-medium">Descripción</th>
                    <th class="px-3 py-2 font-medium">ID producto</th>
                    <th class="px-3 py-2 font-medium text-right">Cantidad</th>
                    <th class="px-3 py-2 font-medium text-right">Total pagado</th>
                    <th class="px-3 py-2 font-medium">Sucursal asignada</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200">
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php $__empty_1 = true; $__currentLoopData = $details; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $detail): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr class="align-top">
                        <td class="px-3 py-2 whitespace-nowrap"><?php echo e($detail->product_reference ?? '-'); ?></td>
                        <td class="px-3 py-2"><?php echo e($detail->product_name ?? '-'); ?></td>
                        <td class="px-3 py-2 whitespace-nowrap"><?php echo e($detail->product_id ?? $detail->product_reference ?? '-'); ?></td>
                        <td class="px-3 py-2 text-right whitespace-nowrap"><?php echo e($detail->product_quantity ?? 0); ?></td>
                        <td class="px-3 py-2 text-right whitespace-nowrap"><?php echo e(\Illuminate\Support\Number::currency((float) ($detail->total_priceTI ?? 0), 'CRC')); ?></td>
                        <td class="px-3 py-2 whitespace-nowrap">
                            <?php
                                $warehouseId = $getWarehouseId($detail);
                                $warehouseRaw = $getWarehouseRaw($detail);
                            ?>
                            <?php echo e($warehouseNamesById[$warehouseId] ?? ($warehouseRaw ?? '-')); ?>

                        </td>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr>
                        <td class="px-3 py-4 text-gray-600" colspan="6">Sin detalle de compra.</td>
                    </tr>
                <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php /**PATH /home/orderflowpro/public_html/resources/views/filament/orders/modals/purchase-details.blade.php ENDPATH**/ ?>