@extends('partials.layout')

@section('title', 'Reportes')
@section('subtitle', 'Saldos iniciales/finales por manguera con precio por turno')

@section('back')
    <a class="btn" href="{{ route('dashboard') }}">← Volver</a>
@endsection

@section('content')
    <form method="GET" action="{{ route('reportes.index') }}" style="display:grid;grid-template-columns:repeat(5,minmax(0,1fr));gap:12px;align-items:end">
        <div>
            <label style="display:block;font-weight:700;margin-bottom:6px">Fecha desde</label>
            <input type="date" name="fecha_desde" value="{{ $fechaDesde }}"
                   style="width:100%;padding:10px;border:1px solid #e2e8f0;border-radius:10px">
        </div>
        <div>
            <label style="display:block;font-weight:700;margin-bottom:6px">Fecha hasta</label>
            <input type="date" name="fecha_hasta" value="{{ $fechaHasta }}"
                   style="width:100%;padding:10px;border:1px solid #e2e8f0;border-radius:10px">
        </div>
        <div>
            <label style="display:block;font-weight:700;margin-bottom:6px">Turno</label>
            <select name="id_turno" style="width:100%;padding:10px;border:1px solid #e2e8f0;border-radius:10px">
                <option value="">Todos</option>
                @foreach($turnos as $t)
                    <option value="{{ $t->id_turno }}" {{ (string)$idTurno === (string)$t->id_turno ? 'selected' : '' }}>
                        {{ $t->turnos ?? $t->nombre ?? ('Turno #' . $t->id_turno) }}
                    </option>
                @endforeach
            </select>
        </div>
        <div>
            <label style="display:block;font-weight:700;margin-bottom:6px">Usuario</label>
            <select name="id_usuario" style="width:100%;padding:10px;border:1px solid #e2e8f0;border-radius:10px" {{ !empty($esOperador) ? 'disabled' : '' }}>
                @if(empty($esOperador))
                    <option value="">Todos</option>
                @endif
                @foreach($usuarios as $u)
                    <option value="{{ $u->id_usuario }}" {{ (string)$idUsuario === (string)$u->id_usuario ? 'selected' : '' }}>
                        {{ ($u->nombres ?? $u->name ?? 'Usuario') }} {{ $u->apellidos ?? '' }}
                    </option>
                @endforeach
            </select>
            @if(!empty($esOperador))
                <input type="hidden" name="id_usuario" value="{{ $idUsuario }}">
            @endif
        </div>
        <div style="display:flex;gap:8px;flex-wrap:wrap">
            <button class="btn primary" type="submit">Filtrar</button>
            <a class="btn" href="{{ route('reportes.index') }}">Hoy</a>
            <a class="btn" href="{{ route('reportes.export', request()->query()) }}">Exportar Excel</a>
            <a class="btn" href="{{ route('reportes.pdf', request()->query()) }}">Descargar PDF</a>
        </div>
    </form>

    <div style="margin-top:16px;overflow:auto;border:1px solid #e2e8f0;border-radius:12px">
        <table style="width:100%;border-collapse:collapse;min-width:980px">
            <thead style="background:#f8fafc">
                <tr>
                    <th style="text-align:left;padding:10px;border-bottom:1px solid #e2e8f0">Fecha</th>
                    <th style="text-align:left;padding:10px;border-bottom:1px solid #e2e8f0">Manguera</th>
                    <th style="text-align:left;padding:10px;border-bottom:1px solid #e2e8f0">Tipo combustible</th>
                    <th style="text-align:left;padding:10px;border-bottom:1px solid #e2e8f0">Turno</th>
                    <th style="text-align:left;padding:10px;border-bottom:1px solid #e2e8f0">Usuario</th>
                    <th style="text-align:right;padding:10px;border-bottom:1px solid #e2e8f0">Saldo inicial</th>
                    <th style="text-align:right;padding:10px;border-bottom:1px solid #e2e8f0">Saldo final</th>
                    <th style="text-align:right;padding:10px;border-bottom:1px solid #e2e8f0">Galones Consumidos</th>
                    <th style="text-align:right;padding:10px;border-bottom:1px solid #e2e8f0">Precio galón</th>
                    <th style="text-align:right;padding:10px;border-bottom:1px solid #e2e8f0">Total</th>
                </tr>
            </thead>
            <tbody>
                @forelse($items as $it)
                    <tr>
                        <td style="padding:10px;border-bottom:1px solid #f1f5f9">
                            {{ optional($it['fecha'])->format('d/m/Y') }}
                        </td>
                        <td style="padding:10px;border-bottom:1px solid #f1f5f9">
                            {{ $it['manguera']?->cara?->estacion?->nombre ?? '-' }}
                            - {{ $it['manguera']?->cara?->nombre ?? '-' }}
                            - MANGUERA #{{ $it['manguera']?->numero_manguera ?? '-' }}
                        </td>
                        <td style="padding:10px;border-bottom:1px solid #f1f5f9">
                            {{ $it['manguera']?->tipoCombustible?->nombre ?? '-' }}
                        </td>
                        <td style="padding:10px;border-bottom:1px solid #f1f5f9">
                            {{ $it['turno']?->turnos ?? $it['turno']?->nombre ?? '-' }}
                        </td>
                        <td style="padding:10px;border-bottom:1px solid #f1f5f9">
                            {{ $it['usuario_nombre'] ?? '-' }}
                        </td>
                        <td style="padding:10px;border-bottom:1px solid #f1f5f9;text-align:right">
                            {{ is_null($it['saldo_inicial']) ? '-' : number_format((float)$it['saldo_inicial'], 3, '.', '') }}
                        </td>
                        <td style="padding:10px;border-bottom:1px solid #f1f5f9;text-align:right">
                            {{ is_null($it['saldo_final']) ? '-' : number_format((float)$it['saldo_final'], 3, '.', '') }}
                        </td>
                        <td style="padding:10px;border-bottom:1px solid #f1f5f9;text-align:right">
                            {{ is_null($it['galones_vendidos']) ? '-' : number_format((float)$it['galones_vendidos'], 3) }}
                        </td>
                        <td style="padding:10px;border-bottom:1px solid #f1f5f9;text-align:right">
                            {{ is_null($it['precio_galon']) || (float)$it['precio_galon'] <= 0 ? '-' : number_format((float)$it['precio_galon'], 3) }}
                        </td>
                        <td style="padding:10px;border-bottom:1px solid #f1f5f9;text-align:right">
                            {{ is_null($it['total_registro']) ? '-' : number_format((float)$it['total_registro'], 2) }}
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="10" style="padding:14px;text-align:center;color:#64748b">
                            No hay datos para el rango seleccionado.
                        </td>
                    </tr>
                @endforelse
            </tbody>
            <tfoot>
                <tr style="background:#f8fafc">
                    <td colspan="9" style="padding:12px;border-top:1px solid #e2e8f0;text-align:right;font-weight:700">
                        TOTAL GENERAL
                    </td>
                    <td style="padding:12px;border-top:1px solid #e2e8f0;text-align:right;font-weight:700">
                        {{ number_format((float)($totalGeneralVentas ?? 0), 2) }}
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>

    @if(!empty($idTurno))
        @if(!empty($mostrarGastosDiferencias) && $gastosDiferencias->isNotEmpty())
            <div style="margin-top:16px;border:1px solid #e2e8f0;border-radius:12px;padding:14px;background:#fff">
                <h3 style="margin:0 0 8px;font-size:15px">Gastos registrados</h3>
                <div style="overflow:auto;border:1px solid #e2e8f0;border-radius:10px;background:#fff">
                    <table style="width:100%;border-collapse:collapse;min-width:760px">
                        <thead style="background:#f8fafc">
                            <tr>
                                <th style="text-align:left;padding:10px;border-bottom:1px solid #e2e8f0">Fecha</th>
                                <th style="text-align:left;padding:10px;border-bottom:1px solid #e2e8f0">Observación</th>
                                <th style="text-align:left;padding:10px;border-bottom:1px solid #e2e8f0">Combustible</th>
                                <th style="text-align:right;padding:10px;border-bottom:1px solid #e2e8f0">Galones</th>
                                <th style="text-align:right;padding:10px;border-bottom:1px solid #e2e8f0">Precio</th>
                                <th style="text-align:right;padding:10px;border-bottom:1px solid #e2e8f0">Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($gastosDiferencias as $gasto)
                                <tr>
                                    <td style="padding:10px;border-bottom:1px solid #f1f5f9">{{ optional($gasto->created_at)->format('d/m/Y H:i') }}</td>
                                    <td style="padding:10px;border-bottom:1px solid #f1f5f9">{{ $gasto->observacion ?? '-' }}</td>
                                    <td style="padding:10px;border-bottom:1px solid #f1f5f9">{{ $gasto->tipoCombustible->nombre ?? '-' }}</td>
                                    <td style="padding:10px;border-bottom:1px solid #f1f5f9;text-align:right">{{ number_format((float) ($gasto->cantidad_galones ?? 0), 3) }}</td>
                                    <td style="padding:10px;border-bottom:1px solid #f1f5f9;text-align:right">{{ number_format((float) ($gasto->precio ?? 0), 2) }}</td>
                                    <td style="padding:10px;border-bottom:1px solid #f1f5f9;text-align:right">{{ number_format((float) ($gasto->total ?? 0), 2) }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                        <tfoot>
                            <tr style="background:#f8fafc">
                                <td colspan="3" style="padding:12px;border-top:1px solid #e2e8f0;text-align:right;font-weight:700">TOTAL GASTOS</td>
                                <td style="padding:12px;border-top:1px solid #e2e8f0;text-align:right;font-weight:700">{{ number_format((float) $gastosDiferencias->sum('cantidad_galones'), 3) }}</td>
                                <td style="padding:12px;border-top:1px solid #e2e8f0;text-align:right;font-weight:700">{{ number_format((float) $gastosDiferencias->sum('precio'), 2) }}</td>
                                <td style="padding:12px;border-top:1px solid #e2e8f0;text-align:right;font-weight:700">{{ number_format((float) $gastosDiferencias->sum('total'), 2) }}</td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        @endif

        <div style="margin-top:16px;border:1px solid #e2e8f0;border-radius:12px;padding:14px;background:#f8fafc">
            <div style="display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px 16px">
                <div><strong>Total de efectivo:</strong> {{ number_format((float)($totalesCierre['efectivo'] ?? 0), 2) }}</div>
                <div><strong>Total Crédito:</strong> {{ number_format((float)($totalesCierre['credito'] ?? 0), 2) }}</div>
                <div><strong>Total Visa:</strong> {{ number_format((float)($totalesCierre['visa'] ?? 0), 2) }}</div>
                <div><strong>Total Descuentos:</strong> {{ number_format((float)($totalesCierre['descuentos'] ?? 0), 2) }}</div>
                <div><strong>Total Diferencia:</strong> {{ number_format((float)($totalesCierre['diferencia'] ?? 0), 2) }}</div>
            </div>
        </div>
    @endif
@endsection
