@extends('layouts.app')

@section('title', 'Reportes - Inventario')

@section('content')
    @php($hasInventarioRows = $rows->count() > 0)
    <div class="report-shell">
        <div>
            <h1 class="h3 mb-1">Reportes</h1>
            <p class="text-muted-brand mb-0">Modulo migrado desde el sistema legado con filtros y exportacion optimizados.</p>
        </div>

        @include('reportes.nav', ['moduleKey' => 'inventario'])

        <div class="report-card">
            <form method="GET" action="{{ route('reportes.inventario') }}" class="report-filters-form row g-3 align-items-end">
                <div class="col-12 col-lg-3">
                    <label for="tipoBienes" class="form-label">Tipo de Bienes</label>
                    <select name="tipoBienes" id="tipoBienes" class="form-select">
                        <option value="0" {{ $filters['tipoBienes'] === 0 ? 'selected' : '' }}>Todos</option>
                        @foreach ($typeOptions as $id => $label)
                            <option value="{{ $id }}" {{ $filters['tipoBienes'] === (int) $id ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>

                <div class="col-12 col-lg-3">
                    <label for="tipoBusqueda" class="form-label">Tipo de Busqueda</label>
                    <select name="tipoBusqueda" id="tipoBusqueda" class="form-select">
                        @foreach ($searchTypeLabels as $id => $label)
                            <option value="{{ $id }}" {{ $filters['tipoBusqueda'] === (int) $id ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>

                <div class="col-12 col-lg-4">
                    <label for="busqueda" class="form-label">Buscar</label>
                    <input type="text" name="busqueda" id="busqueda" class="form-control" value="{{ $filters['busqueda'] }}" placeholder="Descripcion, SAP, codigo o estante">
                </div>

                <div class="col-12">
                    <div class="filters-actions">
                        <select class="form-select form-select-sm" name="per_page" onchange="this.form.submit()" aria-label="Resultados por pagina" style="min-width: 7.5rem; max-width: 7.5rem;">
                            @foreach ([10, 50, 100] as $pageSize)
                                <option value="{{ $pageSize }}" @selected(($perPage ?? 10) === $pageSize)>
                                    {{ $pageSize }} / pag.
                                </option>
                            @endforeach
                        </select>
                        <button type="submit" class="btn btn-brand">Buscar</button>
                        <a href="{{ route('reportes.inventario', ['per_page' => $perPage ?? 10]) }}" class="btn btn-outline-secondary">Limpiar filtros</a>
                        @if ($hasInventarioRows)
                            <a href="{{ route('reportes.inventario.print', request()->query()) }}" target="_blank" class="btn btn-outline-primary">Vista PDF</a>
                        @else
                            <button type="button" class="btn btn-outline-primary" disabled title="No hay datos para imprimir">Vista PDF</button>
                        @endif
                        @if ($hasInventarioRows)
                            <a href="{{ route('reportes.inventario.export', request()->query()) }}" class="btn btn-excel">Exportar Excel</a>
                        @else
                            <button type="button" class="btn btn-excel" disabled title="No hay datos para exportar">Exportar Excel</button>
                        @endif
                    </div>
                </div>
            </form>

            @if ($searchError)
                <div class="alert alert-warning mt-3 mb-0">{{ $searchError }}</div>
            @endif
        </div>

        <div class="summary-grid">
            <div class="summary-item">
                <div class="label">Cantidad Total Almacenada</div>
                <div class="value">{{ number_format($totals['almacenado'], 0, ',', '.') }}</div>
            </div>
            <div class="summary-item">
                <div class="label">Cantidad Total en Stock</div>
                <div class="value">{{ number_format($totals['stock'], 0, ',', '.') }}</div>
            </div>
            <div class="summary-item">
                <div class="label">Cantidad Total Entregada</div>
                <div class="value">{{ number_format($totals['entregado'], 0, ',', '.') }}</div>
            </div>
        </div>

        <div class="report-card">
            <div class="mt-3">
                {{ $rows->links() }}
            </div>
            
            <div class="table-wrap report-inventario-table-wrap">
                <table class="table table-sm align-middle report-inventario-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>{{ ($rows->firstItem() ?? 1) + $loop->index }}</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" class="text-center text-muted-brand py-4">No se encontraron coincidencias.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <div class="mt-3">
                {{ $rows->links() }}
            </div>
        </div>
    </div>
@endsection

@push('styles')
    <style>
        .report-card {
            padding: 1.5rem 1.5rem 1.5rem 1.5rem;
        }
        .report-inventario-table-wrap {
            padding: 0.5rem 0;
        }
        .report-inventario-table {
            min-width: 1420px;
            table-layout: auto;
        }
        .report-inventario-table th,
        .report-inventario-table td {
            white-space: nowrap;
            word-break: normal;
            overflow-wrap: normal;
            vertical-align: top;
        }
        .report-inventario-table .cell-description {
            min-width: 18rem;
            max-width: 26rem;
            white-space: normal;
            word-break: normal;
            overflow-wrap: break-word;
        }
        .report-inventario-table .col-estante {
            min-width: 9rem;
        }
        .report-inventario-table .col-fila {
            min-width: 6rem;
        }
        .report-inventario-table .col-costo,
        .report-inventario-table .col-peso {
            min-width: 6rem;
        }
    </style>
@endpush
