@extends('layouts.app')
@section('titulo','Repuesto')
@section('contenido')
@php $editando = (bool) $registro->id; @endphp
<h4><i class="fa fa-box"></i> {{ $editando?'Editar':'Nuevo' }} repuesto</h4>
<form method="POST" action="{{ $editando ? route('repuestos.update',$registro) : route('repuestos.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',$registro->codigo) }}" class="form-control" required></div>
    <div class="col-md-6"><label class="form-label small">Nombre *</label>
      <input name="nombre" value="{{ old('nombre',$registro->nombre) }}" class="form-control" required></div>
    <div class="col-md-3"><label class="form-label small">Unidad</label>
      <select name="unidad_id" class="form-select"><option value="">—</option>
        @foreach($unidades as $u)<option value="{{ $u->id }}" @selected(old('unidad_id',$registro->unidad_id)==$u->id)>{{ $u->nombre }}</option>@endforeach
      </select></div>
    <div class="col-md-12"><label class="form-label small">Descripción</label>
      <textarea name="descripcion" class="form-control" rows="2">{{ old('descripcion',$registro->descripcion) }}</textarea></div>
    <div class="col-md-3"><label class="form-label small">Stock</label>
      <input type="number" step="0.001" name="stock" value="{{ old('stock',$registro->stock) }}" class="form-control"></div>
    <div class="col-md-3"><label class="form-label small">Stock mínimo</label>
      <input type="number" step="0.001" name="stock_minimo" value="{{ old('stock_minimo',$registro->stock_minimo) }}" class="form-control"></div>
    <div class="col-md-3"><label class="form-label small">Costo unitario</label>
      <input type="number" step="0.01" name="costo_unitario" value="{{ old('costo_unitario',$registro->costo_unitario) }}" class="form-control"></div>
    <div class="col-md-3"><label class="form-label small">Ubicación</label>
      <input name="ubicacion" value="{{ old('ubicacion',$registro->ubicacion) }}" class="form-control"></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',$registro->activo??1)?'checked':'' }}>
        <label class="form-check-label">Activo</label>
      </div>
    </div>
  </div>
  <div class="mt-3 text-end">
    <a href="{{ route('repuestos.index') }}" class="btn btn-outline-secondary">Cancelar</a>
    <button class="btn btn-primario"><i class="fa fa-save"></i> Guardar</button>
  </div>
</form>

@if($editando)
<form method="POST" action="{{ route('repuestos.entrada',$registro) }}" class="card p-3">@csrf
  <h6><i class="fa fa-truck-arrow-right"></i> Registrar entrada de stock</h6>
  <div class="row g-2">
    <div class="col-md-3"><input type="number" step="0.001" name="cantidad" class="form-control" placeholder="Cantidad" required></div>
    <div class="col-md-3"><input type="number" step="0.01" name="costo_unitario" class="form-control" placeholder="Costo unitario" required></div>
    <div class="col-md-4"><input name="motivo" class="form-control" placeholder="Motivo / OC"></div>
    <div class="col-md-2"><button class="btn btn-acento w-100"><i class="fa fa-plus"></i> Entrada</button></div>
  </div>
</form>
@endif
@endsection
