@extends('layouts.app')

@section('content')
<div class="container">

    <h2>Editar Usuario</h2>

    <form action="{{ route('users.update', $user) }}" method="POST">
        @csrf
        @method('PUT')

        <div class="mb-3">
            <label>Nombre</label>
            <input type="text" 
                   name="name" 
                   class="form-control" 
                   value="{{ old('name', $user->name) }}" 
                   required>
        </div>

        <div class="mb-3">
            <label>Email</label>
            <input type="email" 
                   name="email" 
                   class="form-control" 
                   value="{{ old('email', $user->email) }}" 
                   required>
        </div>

        <div class="mb-3">
            <label>Nueva contraseña (opcional)</label>
            <input type="password" 
                   name="password" 
                   class="form-control"
                   placeholder="Solo si deseas cambiarla">
        </div>

        <div class="mb-3">
            <label>Empresa</label>
            <select name="company_id" class="form-control" required>
                <option value="">-- Selecciona una empresa --</option>
                @foreach($companies as $company)
                    <option value="{{ $company->id }}"
                        {{ $user->company_id == $company->id ? 'selected' : '' }}>
                        {{ $company->nombre }}
                    </option>
                @endforeach
            </select>
        </div>

        <div class="mb-3">
            <label>Rol</label>
            <select name="role" class="form-control" required>
                @foreach($roles as $role)
                    <option value="{{ $role->name }}"
                        {{ $user->getRoleNames()->first() === $role->name ? 'selected' : '' }}>
                        {{ $role->name }}
                    </option>
                @endforeach
            </select>
        </div>

        <button class="btn btn-success">Actualizar Usuario</button>
        <a href="{{ route('users.index') }}" class="btn btn-secondary">Cancelar</a>
    </form>
</div>
@endsection
