@extends('layouts.app')

@section('title', 'Nuevo Usuario')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="mb-0"><i class="bi bi-person-plus me-2"></i>Nuevo Usuario</h5>
    <a href="{{ route('users.index') }}" class="btn btn-outline-secondary btn-sm">
        <i class="bi bi-arrow-left me-1"></i> Volver
    </a>
</div>

<div class="card" style="max-width:600px">
    <div class="card-header"><i class="bi bi-person-fill me-1"></i> Datos del Usuario</div>
    <div class="card-body">
        <form action="{{ route('users.store') }}" method="POST">
            @csrf

            <div class="mb-3">
                <label class="form-label fw-semibold">Nombre completo <span class="text-danger">*</span></label>
                <input type="text" name="name" class="form-control @error('name') is-invalid @enderror"
                       value="{{ old('name') }}" required>
                @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
            </div>

            <div class="mb-3">
                <label class="form-label fw-semibold">Correo electrónico <span class="text-danger">*</span></label>
                <input type="email" name="email" class="form-control @error('email') is-invalid @enderror"
                       value="{{ old('email') }}" required>
                @error('email')<div class="invalid-feedback">{{ $message }}</div>@enderror
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label fw-semibold">Contraseña <span class="text-danger">*</span></label>
                    <input type="password" name="password" class="form-control @error('password') is-invalid @enderror"
                           required minlength="6">
                    @error('password')<div class="invalid-feedback">{{ $message }}</div>@enderror
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label fw-semibold">Confirmar contraseña <span class="text-danger">*</span></label>
                    <input type="password" name="password_confirmation" class="form-control" required minlength="6">
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label fw-semibold">Rol <span class="text-danger">*</span></label>
                <select name="rol" class="form-select @error('rol') is-invalid @enderror" required>
                    <option value="">— Seleccione —</option>
                    <option value="empleado"   {{ old('rol') == 'empleado'   ? 'selected' : '' }}>Empleado</option>
                    <option value="supervisor" {{ old('rol') == 'supervisor' ? 'selected' : '' }}>Supervisor</option>
                    <option value="admin"      {{ old('rol') == 'admin'      ? 'selected' : '' }}>Administrador</option>
                </select>
                <div class="form-text">
                    <b>Empleado:</b> solo ve y diligencia su propia hoja. 
                    <b>Supervisor:</b> aprueba hojas y ve reportes. 
                    <b>Admin:</b> acceso total.
                </div>
                @error('rol')<div class="invalid-feedback">{{ $message }}</div>@enderror
            </div>

            <div class="mb-3">
                <label class="form-label fw-semibold">Vincular a empleado</label>
                <select name="empleado_id" class="form-select @error('empleado_id') is-invalid @enderror">
                    <option value="">— Sin vincular (admin/supervisor) —</option>
                    @foreach($empleados as $emp)
                        <option value="{{ $emp->id }}" {{ old('empleado_id') == $emp->id ? 'selected' : '' }}>
                            {{ $emp->nombre }} — {{ $emp->cedula }}
                        </option>
                    @endforeach
                </select>
                <div class="form-text">Los empleados deben estar vinculados para ver su hoja de tiempo.</div>
                @error('empleado_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
            </div>

            <div class="mb-4">
                <div class="form-check form-switch">
                    <input class="form-check-input" type="checkbox" name="activo" id="activo" value="1"
                           {{ old('activo', '1') ? 'checked' : '' }}>
                    <label class="form-check-label" for="activo">Usuario activo</label>
                </div>
            </div>

            <div class="d-flex gap-2">
                <button type="submit" class="btn btn-primary">
                    <i class="bi bi-check-lg me-1"></i> Crear Usuario
                </button>
                <a href="{{ route('users.index') }}" class="btn btn-outline-secondary">Cancelar</a>
            </div>
        </form>
    </div>
</div>
@endsection
