<x-app-layout>
    <x-slot name="title">
        <div class="d-flex align-items-center gap-2">
            <span class="text-secondary fw-normal">Costos / </span> Consolidado Mensual
        </div>
    </x-slot>

    <div class="row mb-4">
        <div class="col-12">
            <div class="card border-0 shadow-sm rounded-4 mb-4">
                <div class="card-body p-4 border-bottom">
                    <form action="{{ route('costos.consolidado') }}" method="GET" class="row g-3 align-items-end">
                        <div class="col-md-3">
                            <label class="form-label small fw-bold text-muted text-uppercase mb-1">Mes de Análisis</label>
                            <input type="month" name="mes" class="form-control rounded-pill bg-light border-light-subtle" value="{{ $mes }}" required>
                        </div>
                        <div class="col-md-auto">
                            <button type="submit" class="btn btn-primary rounded-pill fw-bold px-4 shadow-sm">
                                <i class="bi bi-calendar-event"></i> Comparar Sucursales
                            </button>
                        </div>
                    </form>
                </div>
                
                <div class="card-body p-4">
                    <div class="row g-4">
                        @forelse($data as $columna)
                            <div class="col-md-6 col-xl-4">
                                <div class="border rounded-4 p-4 h-100 position-relative bg-white shadow-sm transition-hover">
                                    <div class="d-flex justify-content-between align-items-start mb-4">
                                        <div>
                                            <h5 class="fw-bold mb-1 text-primary">{{ $columna['sucursal']->nombre }}</h5>
                                            <span class="badge bg-light text-secondary border rounded-pill px-3 py-1">
                                                {{ $columna['sesiones'] }} turnos de caja
                                            </span>
                                        </div>
                                        <div class="bg-primary bg-opacity-10 p-2 rounded-circle">
                                            <i class="bi bi-shop fs-4 text-primary"></i>
                                        </div>
                                    </div>

                                    <div class="d-flex flex-column gap-3">
                                        <!-- Ingresos Netos de Caja -->
                                        <div class="d-flex align-items-center justify-content-between p-3 rounded-3 bg-success bg-opacity-10">
                                            <div class="d-flex align-items-center gap-2">
                                                <i class="bi bi-graph-up-arrow text-success"></i>
                                                <span class="text-muted fw-bold small text-uppercase">Ingresos Ventas</span>
                                            </div>
                                            <span class="fw-bold fs-5 text-success">
                                                + {{ \App\Models\Configuracion::getParaSucursal('moneda_simbolo', $columna['sucursal']->id, 'S/.') }} {{ number_format($columna['ingresos_caja'], 2) }}
                                            </span>
                                        </div>

                                        <!-- Egresos Directos Mensuales -->
                                        <div class="d-flex align-items-center justify-content-between p-3 rounded-3 bg-danger bg-opacity-10">
                                            <div class="d-flex align-items-center gap-2">
                                                <i class="bi bi-graph-down-arrow text-danger"></i>
                                                <span class="text-muted fw-bold small text-uppercase">Egresos Caja</span>
                                            </div>
                                            <span class="fw-bold text-danger">
                                                - {{ \App\Models\Configuracion::getParaSucursal('moneda_simbolo', $columna['sucursal']->id, 'S/.') }} {{ number_format($columna['egresos_caja'], 2) }}
                                            </span>
                                        </div>

                                        <!-- Compras por Proveedor (Inventario) -->
                                        <div class="d-flex align-items-center justify-content-between p-3 rounded-3 bg-warning bg-opacity-10">
                                            <div class="d-flex align-items-center gap-2">
                                                <i class="bi bi-boxes text-warning" style="filter: brightness(0.8)"></i>
                                                <span class="text-muted fw-bold small text-uppercase">Logística/Almacén</span>
                                            </div>
                                            <span class="fw-bold text-warning" style="filter: brightness(0.8)">
                                                - {{ \App\Models\Configuracion::getParaSucursal('moneda_simbolo', $columna['sucursal']->id, 'S/.') }} {{ number_format($columna['compras_mes'], 2) }}
                                            </span>
                                        </div>
                                        
                                        @php
                                            $neto = $columna['ingresos_caja'] - $columna['egresos_caja'] - $columna['compras_mes'];
                                        @endphp
                                        
                                        <div class="mt-2 pt-3 border-top d-flex justify-content-between align-items-center">
                                            <span class="fw-bold text-dark text-uppercase small">Flujo Neto Estimado</span>
                                            <span class="fw-bold fs-4 {{ $neto >= 0 ? 'text-success' : 'text-danger' }}">
                                                {{ $neto >= 0 ? '+' : '-' }} {{ \App\Models\Configuracion::getParaSucursal('moneda_simbolo', $columna['sucursal']->id, 'S/.') }} {{ number_format(abs($neto), 2) }}
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        @empty
                            <div class="col-12 text-center py-5">
                                <i class="bi bi-bar-chart text-light" style="font-size: 4rem;"></i>
                                <h5 class="fw-bold text-muted mt-3">No hay datos financieros para el periodo indicado.</h5>
                                <p class="text-secondary small">Asegúrate de que haya aperturas de caja o ingresos a almacén registrados.</p>
                            </div>
                        @endforelse
                    </div>

                </div>
            </div>
        </div>
    </div>
</x-app-layout>