@extends('layouts.app')
@section('titulo','Usuario')
@section('contenido')
@php $editando = (bool) $registro->id; @endphp
<h4><i class="fa fa-user-plus"></i> {{ $editando?'Editar':'Nuevo' }} usuario</h4>
<form method="POST" action="{{ $editando ? route('usuarios.update',$registro) : route('usuarios.store') }}" class="card p-3">
  @csrf @if($editando) @method('PUT') @endif
  <div class="row g-3">
    <div class="col-md-3"><label class="form-label small">Usuario *</label>
      <input name="usuario" value="{{ old('usuario',$registro->usuario) }}" class="form-control" required></div>
    <div class="col-md-3"><label class="form-label small">DNI</label>
      <input name="dni" value="{{ old('dni',$registro->dni) }}" class="form-control"></div>
    <div class="col-md-3"><label class="form-label small">Teléfono</label>
      <input name="telefono" value="{{ old('telefono',$registro->telefono) }}" 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 class="col-md-6"><label class="form-label small">Nombres *</label>
      <input name="nombres" value="{{ old('nombres',$registro->nombres) }}" class="form-control" required></div>
    <div class="col-md-6"><label class="form-label small">Apellidos</label>
      <input name="apellidos" value="{{ old('apellidos',$registro->apellidos) }}" class="form-control"></div>
    <div class="col-md-6"><label class="form-label small">Cargo</label>
      <input name="cargo" value="{{ old('cargo',$registro->cargo) }}" class="form-control"></div>
    <div class="col-md-6"><label class="form-label small">Email (opcional)</label>
      <input type="email" name="email" value="{{ old('email',$registro->email) }}" class="form-control"></div>
    <div class="col-md-6"><label class="form-label small">Contraseña {{ $editando?'(dejar vacío para no cambiar)':'*' }}</label>
      <input type="password" name="password" class="form-control" {{ $editando?'':'required' }} minlength="6" autocomplete="new-password"></div>
    <div class="col-md-6"><label class="form-label small">Clave de firma {{ $editando?'(opcional)':'(opcional)' }}</label>
      <input type="password" name="clave_firma" class="form-control" minlength="3" autocomplete="new-password"
             placeholder="Clave personal para firmar órdenes"></div>
    <div class="col-md-6"><label class="form-label small">Rol</label>
      <select name="rol" class="form-select">
        @foreach($roles as $r)
          <option value="{{ $r->name }}" @selected(old('rol',$registro->roles->first()?->name)===$r->name)>{{ $r->name }}</option>
        @endforeach
      </select></div>
  </div>
  <div class="mt-3 text-end">
    <a href="{{ route('usuarios.index') }}" class="btn btn-outline-secondary">Cancelar</a>
    <button class="btn btn-primario"><i class="fa fa-save"></i> Guardar</button>
  </div>
</form>
@endsection
