<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Reporte Inventario</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            color: #1f2933;
            margin: 1.5rem;
        }

        h1 {
            margin: 0 0 0.25rem;
            font-size: 1.25rem;
        }

        .meta {
            margin-bottom: 1rem;
            font-size: 0.9rem;
        }

        .meta strong {
            display: inline-block;
            min-width: 140px;
        }

        .totals {
            display: grid;
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: 0.5rem;
            margin-bottom: 1rem;
        }

        .totals div {
            border: 1px solid #d4dee8;
            padding: 0.5rem;
            font-size: 0.9rem;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 0.74rem;
            table-layout: auto;
        }

        th,
        td {
            border: 1px solid #d4dee8;
            padding: 0.25rem 0.35rem;
            text-align: left;
            vertical-align: top;
            white-space: nowrap;
            word-break: normal;
            overflow-wrap: normal;
        }

        thead th {
            background: #eef5fa;
        }

        .cell-description {
            min-width: 12rem;
            max-width: 18rem;
            white-space: normal;
            overflow-wrap: break-word;
        }

        .col-estante {
            min-width: 5.5rem;
        }

        .col-fila,
        .col-costo,
        .col-peso {
            min-width: 4rem;
        }

        @media print {
            body {
                margin: 0.6cm;
            }
        }
    </style>
</head>
<body>
    <h1>Reporte Personalizado del Registro de Inventario</h1>
    <div class="meta">
        <div><strong>Fecha:</strong> {{ now()->format('d/m/Y H:i') }}</div>
        <div><strong>Tipo Busqueda:</strong> {{ $searchTypeLabels[$filters['tipoBusqueda']] ?? 'Descripcion' }}</div>
        <div><strong>Tipo Bienes:</strong> {{ $typeLabels[$filters['tipoBienes']] ?? 'Todos' }}</div>
        <div><strong>Termino:</strong> {{ $filters['busqueda'] !== '' ? $filters['busqueda'] : '-' }}</div>
    </div>

    @if ($searchError)
        <p><strong>Advertencia:</strong> {{ $searchError }}</p>
    @endif

    <div class="totals">
        <div><strong>Cantidad Total Almacenada:</strong> {{ number_format($totals['almacenado'], 0, ',', '.') }}</div>
        <div><strong>Cantidad Total en Stock:</strong> {{ number_format($totals['stock'], 0, ',', '.') }}</div>
        <div><strong>Cantidad Total Entregada:</strong> {{ number_format($totals['entregado'], 0, ',', '.') }}</div>
    </div>

    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>SAP</th>
                <th>Codigo</th>
                <th class="cell-description">Descripcion</th>
                <th>Tipo</th>
                <th>Cant. Almac.</th>
                <th>Cant. Stock</th>
                <th>Cant. Entregada</th>
                <th class="col-estante">Estante</th>
                <th class="col-fila">Fila</th>
                <th class="col-costo">Costo</th>
                <th class="col-peso">Peso</th>
            </tr>
        </thead>
        <tbody>
            @forelse ($rows as $row)
                <tr>
                    <td>{{ $loop->iteration }}</td>
                    <td>{{ $row->sap ?? '-' }}</td>
                    <td>{{ $row->codigo ?? '-' }}</td>
                    <td class="cell-description">{{ $row->descripcion ?? '-' }}</td>
                    <td>{{ $typeLabels[(int) ($row->tipo ?? 0)] ?? 'Sin tipo' }}</td>
                    <td>{{ (int) ($row->cantidadUnidad ?? 0) }} {{ $row->unidad ?? '' }}</td>
                    <td>{{ (int) ($row->cantidadUnidadUtil ?? 0) }} {{ $row->unidadUtil ?? '' }}</td>
                    <td>{{ (int) ($row->cantidadEntregada ?? 0) }} {{ $row->unidadUtil ?? '' }}</td>
                    <td class="col-estante">{{ $row->ubicacionEstante ?? '-' }}</td>
                    <td class="col-fila">{{ $row->ubicacionFila ?? '-' }}</td>
                    <td class="col-costo">{{ $row->costo ?? '-' }}</td>
                    <td class="col-peso">{{ $row->peso ?? '-' }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="12">No se encontraron coincidencias.</td>
                </tr>
            @endforelse
        </tbody>
    </table>

    <script>
        window.print();
    </script>
</body>
</html>
