@extends('layouts.app')
@section('titulo','Plan')
@section('contenido')
@php $editando = (bool) $plan->id; @endphp
<h4><i class="fa fa-clipboard-list"></i> {{ $editando?'Editar':'Nuevo' }} plan ({{ ucfirst($plan->clase) }})</h4>
<form method="POST" action="{{ $editando ? route('planes.update',$plan) : route('planes.store') }}" class="card p-3 mb-3">
  @csrf @if($editando) @method('PUT') @endif
  <div class="row g-3">
    <div class="col-md-3"><label class="form-label small">Código</label>
      <input name="codigo" value="{{ old('codigo',$plan->codigo) }}" class="form-control" placeholder="Auto si se deja vacío"></div>
    <div class="col-md-6"><label class="form-label small">Nombre *</label>
      <input name="nombre" value="{{ old('nombre',$plan->nombre) }}" class="form-control" required></div>
    <div class="col-md-3"><label class="form-label small">Clase *</label>
      <select name="clase" class="form-select" required>
        <option value="preventivo" @selected(old('clase',$plan->clase)==='preventivo')>Preventivo</option>
        <option value="correctivo" @selected(old('clase',$plan->clase)==='correctivo')>Correctivo</option>
      </select></div>
    <div class="col-md-3"><label class="form-label small">Área</label>
      <select name="area_id" class="form-select"><option value="">—</option>
        @foreach($areas as $a)<option value="{{ $a->id }}" @selected(old('area_id',$plan->area_id)==$a->id)>{{ $a->nombre }}</option>@endforeach
      </select></div>
    <div class="col-md-3"><label class="form-label small">Sistema</label>
      <select name="sistema_id" class="form-select"><option value="">—</option>
        @foreach($sistemas as $s)<option value="{{ $s->id }}" @selected(old('sistema_id',$plan->sistema_id)==$s->id)>{{ $s->nombre }}</option>@endforeach
      </select></div>
    <div class="col-md-3"><label class="form-label small">Equipo</label>
      <select name="equipo_id" class="form-select"><option value="">—</option>
        @foreach($equipos as $e)<option value="{{ $e->id }}" @selected(old('equipo_id',$plan->equipo_id)==$e->id)>{{ $e->codigo }} — {{ $e->nombre }}</option>@endforeach
      </select></div>
    <div class="col-md-3"><label class="form-label small">Inicio</label>
      <input type="date" name="fecha_inicio" value="{{ old('fecha_inicio',optional($plan->fecha_inicio)->format('Y-m-d')) }}" class="form-control"></div>
    <div class="col-md-3"><label class="form-label small">Fin</label>
      <input type="date" name="fecha_fin" value="{{ old('fecha_fin',optional($plan->fecha_fin)->format('Y-m-d')) }}" class="form-control"></div>
    <div class="col-md-12"><label class="form-label small">Descripción</label>
      <textarea name="descripcion" class="form-control" rows="2">{{ old('descripcion',$plan->descripcion) }}</textarea></div>
    <div class="col-md-12"><label class="form-label small">Observaciones</label>
      <textarea name="observaciones" class="form-control" rows="2">{{ old('observaciones',$plan->observaciones) }}</textarea></div>
    <div class="col-md-3 d-flex align-items-end">
      <div class="form-check form-switch">
        <input type="hidden" name="activo" value="0">
        <input type="checkbox" name="activo" value="1" class="form-check-input" {{ old('activo',$plan->activo??1)?'checked':'' }}>
        <label class="form-check-label">Activo</label>
      </div>
    </div>
  </div>
  <div class="mt-3 text-end">
    <a href="{{ route('planes.index',['clase'=>$plan->clase]) }}" class="btn btn-outline-secondary">Volver</a>
    <button class="btn btn-primario"><i class="fa fa-save"></i> Guardar</button>
  </div>
</form>

@if($editando)
  <div class="card">
    <div class="card-header bg-light d-flex justify-content-between"><strong><i class="fa fa-list"></i> Ítems del plan</strong>
      <button class="btn btn-sm btn-primario" data-bs-toggle="collapse" data-bs-target="#nuevoItem">
        <i class="fa fa-plus"></i> Agregar ítem</button>
    </div>
    <div id="nuevoItem" class="collapse">
      <form method="POST" action="{{ route('planes.items.store', $plan) }}" class="p-3 border-bottom">
        @csrf
        @include('planes.partials.item_form', ['item' => null])
        <div class="text-end mt-2"><button class="btn btn-primario btn-sm">Agregar</button></div>
      </form>
    </div>
    <div class="table-responsive">
      <table class="table table-sm mb-0 align-middle">
        <thead><tr>
          <th>#</th><th>Descripción</th><th>Componente</th><th>Cant.</th><th>Frec.</th>
          <th class="text-center">L</th><th class="text-center">I</th><th class="text-center">Lu</th><th class="text-center">A</th>
          <th class="text-center">PM360</th><th class="text-center">PM720</th><th class="text-center">PM4320</th><th class="text-center">PM8640</th>
          <th>Tipo</th><th>Resp.</th><th class="text-end">Costo est.</th><th></th>
        </tr></thead>
        <tbody>
        @forelse($plan->items as $it)
          <tr>
            <td>{{ $it->numero_item }}</td>
            <td>{{ Str::limit($it->descripcion, 60) }}</td>
            <td><small>{{ $it->codigo_componente ?? optional($it->componente)->codigo }}</small></td>
            <td>{{ $it->cantidad }}</td>
            <td>{{ optional($it->frecuencia)->codigo }}</td>
            @foreach(['lila_l_limpieza','lila_i_inspeccion','lila_l_lubricacion','lila_a_ajuste',
                      'pm_360','pm_720','pm_4320','pm_8640'] as $f)
              <td class="text-center">@if($it->$f)<i class="fa fa-check text-success"></i>@endif</td>
            @endforeach
            <td><small>{{ optional($it->tipoMantenimiento)->codigo }}</small></td>
            <td><small>{{ optional($it->responsable)->usuario }}</small></td>
            <td class="text-end">{{ number_format($it->costo_estimado, 2) }}</td>
            <td class="text-end">
              <a href="#" class="btn btn-sm btn-outline-primary" data-bs-toggle="collapse" data-bs-target="#editItem{{ $it->id }}"><i class="fa fa-pen"></i></a>
              <form method="POST" action="{{ route('planes.items.destroy',[$plan,$it]) }}" 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>
            </td>
          </tr>
          <tr id="editItem{{ $it->id }}" class="collapse"><td colspan="17" class="bg-light">
            <form method="POST" action="{{ route('planes.items.update',[$plan,$it]) }}">@csrf @method('PUT')
              @include('planes.partials.item_form', ['item' => $it])
              <div class="text-end mt-2"><button class="btn btn-sm btn-primario">Guardar</button></div>
            </form>
          </td></tr>
        @empty
          <tr><td colspan="17" class="text-center text-muted py-3">Sin ítems</td></tr>
        @endforelse
        </tbody>
      </table>
    </div>
  </div>
@endif
@endsection
