@extends('layouts.app')
@section('titulo','Historial horómetro')
@section('contenido')
<div class="d-flex flex-wrap justify-content-between align-items-center mb-3">
  <h4><i class="fa fa-clock-rotate-left"></i> {{ $equipo->codigo }} — {{ $equipo->nombre }}</h4>
  <a href="{{ route('horometro.index') }}" class="btn btn-outline-secondary"><i class="fa fa-arrow-left"></i> Volver</a>
</div>
<div class="row mb-3">
  <div class="col-md-3"><div class="card"><div class="card-body p-2">
    <small class="text-muted">Horómetro actual</small><br>
    <span class="fs-4 fw-bold">{{ number_format((float)$equipo->horometro_actual, 2) }} h</span>
  </div></div></div>
  @foreach(['ultimo_pm_360'=>'PM 01 (360h)','ultimo_pm_720'=>'PM 02 (720h)','ultimo_pm_4320'=>'PM 03 (4320h)','ultimo_pm_8640'=>'PM 04 (8640h)'] as $col => $lbl)
    @php $base = (float)($equipo->$col ?? 0); $umbral = (int) explode('_', $col)[2]; $pendiente = $base > 0 ? (((float)$equipo->horometro_actual) - $base) : 0; $rest = $umbral - $pendiente; $color = $rest <= 0 ? 'danger' : ($rest < $umbral * 0.1 ? 'warning' : 'success'); @endphp
    @if($base > 0)
      <div class="col-md-2"><div class="card border-{{ $color }}"><div class="card-body p-2">
        <small class="text-muted">{{ $lbl }}</small><br>
        <small>Última: {{ number_format($base,0) }} h</small><br>
        <span class="badge text-bg-{{ $color }}">{{ $rest > 0 ? 'Faltan '.number_format($rest,0).' h' : 'Excedido '.number_format(-$rest,0).' h' }}</span>
      </div></div></div>
    @endif
  @endforeach
</div>

<div class="card"><div class="table-responsive"><table class="table table-hover mb-0">
<thead><tr><th>Fecha lectura</th><th class="text-end">Horómetro</th><th class="text-end">Δ (h)</th><th>Observaciones</th><th>Registró</th></tr></thead>
<tbody>
@forelse($lecturas as $l)
<tr>
  <td>{{ $l->fecha_lectura->format('d/m/Y H:i') }}</td>
  <td class="text-end">{{ number_format((float)$l->horometro, 2) }}</td>
  <td class="text-end">{{ number_format((float)$l->delta, 2) }}</td>
  <td>{{ $l->observaciones }}</td>
  <td><small>{{ optional($l->usuario)->usuario }}</small></td>
</tr>
@empty <tr><td colspan="5" class="text-muted text-center py-4">Sin lecturas registradas</td></tr> @endforelse
</tbody></table></div></div>
<div class="mt-2">{{ $lecturas->links() }}</div>
@endsection
