@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Usuario</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('userSave') }}">
                        @csrf

                        <input type="hidden" name="user_id" value="{{ $user->user_id ?? null }}" />

                        <div class="row mb-3">
                            <label for="profile_id" class="col-md-4 col-form-label text-md-end">Perfil</label>

                            <div class="col-md-6">
                                @php
                                $selected = old('profile_id') ?? $user->profile_id ?? null;
                                @endphp
                                <select name="profile_id" id="profile_id" class="form-control  @error('profile_id') is-invalid @enderror">
                                    <option value="">-- Seleccionar Perfil --</option>
                                    @foreach($profiles as $profile)
                                    <option value="{{ $profile->profile_id }}" {{ $selected == $profile->profile_id ? 'selected' : '' }}>{{ $profile->name }}</option>
                                    @endforeach
                                </select>

                                @error('profile_id')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>

                        <div class="row mb-3">
                            <label for="name" class="col-md-4 col-form-label text-md-end">Nombres</label>

                            <div class="col-md-6">
                                <input id="first_name" name="first_name" type="text" class="form-control @error('first_name') is-invalid @enderror" value="{{ old('first_name') ?? $user->first_name ?? null }}" required>

                                @error('first_name')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>

                        <div class="row mb-3">
                            <label for="name" class="col-md-4 col-form-label text-md-end">Apellidos</label>

                            <div class="col-md-6">
                                <input id="last_name" name="last_name" type="text" class="form-control @error('last_name') is-invalid @enderror" value="{{ old('last_name') ?? $user->last_name ?? null }}" required>

                                @error('last_name')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>

                        <div class="row mb-3">
                            <label for="email" class="col-md-4 col-form-label text-md-end">Email</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') ?? $user->email ?? null }}" required>

                                @error('email')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>

                        <div class="row mb-3">
                            <label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required value="{{ $user->password ?? null }}">

                                @error('password')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                                @enderror
                            </div>
                        </div>

                        <div class="row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    Guardar
                                </button>
                                <a href="{{ route('users') }}" class="btn btn-primary">Cancelar</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection