@extends('layouts.app')
@section('titulo','Paros de planta')
@section('contenido')
<div class="d-flex flex-wrap justify-content-between align-items-center mb-3">
  <h4><i class="fa fa-circle-stop"></i> Paros de planta</h4>
  <div>
    <a href="{{ route('programacion.index') }}" class="btn btn-outline-secondary"><i class="fa fa-calendar-days"></i> Programación</a>
    <a href="{{ route('paros.create') }}" class="btn btn-primario"><i class="fa fa-plus"></i> Nuevo paro</a>
  </div>
</div>

@if($activos->count())
<div class="card mb-3 border-danger">
  <div class="card-header bg-danger text-white"><strong><i class="fa fa-triangle-exclamation"></i> Paros ACTIVOS ({{ $activos->count() }})</strong></div>
  <div class="table-responsive"><table class="table table-sm mb-0 align-middle">
    <thead><tr>
      <th>#</th><th>Desde</th><th>Días</th><th>Motivo</th><th>Descripción</th><th>Por</th><th class="text-end" style="width:300px;">Acciones</th>
    </tr></thead><tbody>
    @foreach($activos as $p)
      <tr>
        <td>{{ $p->id }}</td>
        <td>{{ $p->fecha_inicio->format('d/m/Y') }}</td>
        <td><span class="badge text-bg-warning">{{ $p->fecha_inicio->diffInDays(now()) }} d</span></td>
        <td><strong>{{ $p->motivo }}</strong></td>
        <td><small>{{ $p->descripcion }}</small></td>
        <td><small>{{ optional($p->usuario)->usuario }}</small></td>
        <td class="text-end">
          <form method="POST" action="{{ route('paros.cerrar', $p) }}" class="d-flex gap-1 justify-content-end" onsubmit="return confirm('¿Cerrar y desplazar OT?');">
            @csrf
            <input type="date" name="fecha_fin" value="{{ now()->toDateString() }}" class="form-control form-control-sm" style="max-width:140px;" required>
            <button class="btn btn-sm btn-primario"><i class="fa fa-check"></i> Cerrar</button>
            <a href="{{ route('paros.edit', $p) }}" class="btn btn-sm btn-outline-primary"><i class="fa fa-pen"></i></a>
            <form method="POST" action="{{ route('paros.destroy', $p) }}" class="d-inline" onsubmit="return confirm('¿Eliminar?');">
              @csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger"><i class="fa fa-trash"></i></button>
            </form>
          </form>
        </td>
      </tr>
    @endforeach
    </tbody></table></div>
</div>
@endif

<div class="card">
  <div class="card-header"><strong><i class="fa fa-clock-rotate-left"></i> Histórico</strong></div>
  <div class="table-responsive"><table class="table table-sm mb-0">
    <thead><tr><th>#</th><th>Desde</th><th>Hasta</th><th>Días</th><th>Motivo</th><th>OT afectadas</th><th>Por</th></tr></thead>
    <tbody>
    @forelse($historial as $p)
      <tr>
        <td>{{ $p->id }}</td>
        <td>{{ $p->fecha_inicio->format('d/m/Y') }}</td>
        <td>{{ $p->fecha_fin?->format('d/m/Y') }}</td>
        <td><strong>{{ $p->duracion_dias }}</strong></td>
        <td>{{ $p->motivo }}</td>
        <td>{{ $p->ots_afectadas }}</td>
        <td><small>{{ optional($p->usuario)->usuario }}</small></td>
      </tr>
    @empty
      <tr><td colspan="7" class="text-muted text-center py-4">Sin paros registrados</td></tr>
    @endforelse
    </tbody>
  </table></div>
</div>
<div class="mt-2">{{ $historial->links() }}</div>
@endsection
