@extends('partials.layout')

@section('title', 'Registros de Salida')
@section('subtitle', 'Historial de despachos de combustible')

@section('actions')
    <a class="btn primary" href="{{ route('registros_salidas.create') }}">Nuevo Despacho</a>
@endsection

@section('content')
    <form method="GET" action="{{ route('registros_salidas.index') }}" style="display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:10px;margin-bottom:14px">
        <div>
            <label style="display:block;font-weight:700;margin-bottom:6px">Fecha desde</label>
            <input type="date" name="fecha_desde" value="{{ $fechaDesde ?? date('Y-m-d') }}" 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 ?? date('Y-m-d') }}" style="width:100%;padding:10px;border:1px solid #e2e8f0;border-radius:10px">
        </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">
                <option value="">-- Todos --</option>
                @foreach($usuarios as $u)
                    <option value="{{ $u->id_usuario }}" {{ (string)($usuarioId ?? '') === (string)$u->id_usuario ? 'selected' : '' }}>
                        {{ $u->usuario ?? $u->name ?? $u->email }}
                    </option>
                @endforeach
            </select>
        </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)($turnoId ?? '') === (string)$t->id_turno ? 'selected' : '' }}>
                        {{ $t->turnos }}
                    </option>
                @endforeach
            </select>
        </div>
        <div style="grid-column:1/-1;display:flex;gap:8px;align-items:end">
            <button class="btn primary" type="submit">Filtrar</button>
            <a class="btn" href="{{ route('registros_salidas.index') }}">Limpiar</a>
        </div>
    </form>

    <div style="overflow-x:auto">
        <table style="width:100%;border-collapse:collapse;margin-top:10px">
            <thead>
                <tr style="background:#f1f5f9;text-align:left">
                    <th style="padding:12px;border-bottom:1px solid #e2e8f0">ID</th>
                    <th style="padding:12px;border-bottom:1px solid #e2e8f0">Fecha</th>
                    <th style="padding:12px;border-bottom:1px solid #e2e8f0">Usuario</th>
                    <th style="padding:12px;border-bottom:1px solid #e2e8f0">Turno</th>
                    <th style="padding:12px;border-bottom:1px solid #e2e8f0">ISLA / LADO / MANGUERA</th>

                    <th style="padding:12px;border-bottom:1px solid #e2e8f0">Saldo final</th>


                    <th style="padding:12px;border-bottom:1px solid #e2e8f0">Acciones</th>
                </tr>
            </thead>
            <tbody>
                @php
                    $userColorMap = [];
                    $userColorPalette = [
                        '#ef4444', '#f97316', '#eab308', '#22c55e', '#10b981',
                        '#06b6d4', '#3b82f6', '#6366f1', '#8b5cf6', '#d946ef',
                        '#ec4899', '#14b8a6', '#84cc16', '#f59e0b', '#0ea5e9'
                    ];
                    $userColorIndex = 0;
                @endphp

                @foreach($items as $item)
                    @php
                        $username = $item->usuario?->usuario ?? $item->usuario?->name ?? '-';
                        if (!isset($userColorMap[$username])) {
                            $userColorMap[$username] = $userColorPalette[$userColorIndex % count($userColorPalette)];
                            $userColorIndex++;
                        }
                        $userColor = $userColorMap[$username];
                    @endphp
                    <tr style="border-bottom:1px solid #e2e8f0">
                        <td style="padding:12px">{{ $item->id_salida }}</td>
                        <td style="padding:12px">{{ optional($item->fecha_inicio)->format('d/m/Y') }}</td>
                        <td style="padding:12px">
                            <span style="display:inline-block;padding:4px 10px;border-radius:999px;background:{{ $userColor }};color:#fff;font-weight:700;font-size:12px;line-height:1.2">
                                {{ $username }}
                            </span>
                        </td>
                        <td style="padding:12px">{{ $item->turno?->turnos ?? '-' }}</td>
                        <td style="padding:12px">{{ $item->manguera?->cara?->estacion?->nombre ?? '-' }} | {{ $item->manguera?->cara?->lado ?? $item->manguera?->cara?->nombre ?? '-' }} | M-{{ $item->manguera?->numero_manguera ?? '-' }}</td>

                        <td style="padding:12px">{{ $item->saldo_final ?? '-' }}</td>


                        <td style="padding:12px;display:flex;gap:8px">
                            <a class="btn" href="{{ route('registros_salidas.edit', $item->id_salida) }}" style="padding:6px 10px;font-size:13px">Editar</a>
                            @if(auth()->user()->rol === 'ADMIN')
                                <form action="{{ route('registros_salidas.destroy', $item->id_salida) }}" method="POST" onsubmit="return confirm('¿Eliminar registro?')">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn" style="padding:6px 10px;font-size:13px;background:#fee2e2;color:#991b1b;border-color:#fecaca">Eliminar</button>
                                </form>
                            @endif
                        </td>
                    </tr>
                @endforeach

                @if($items->isEmpty())
                    <tr>
                        <td colspan="7" style="padding:24px;text-align:center;color:#64748b">No hay registros de salida.</td>
                    </tr>
                @endif
            </tbody>
        </table>
    </div>
@endsection

