@extends('layouts.app')

@section('title', 'Nuevo Usuario')
@section('page-title', 'Crear Nuevo Usuario')

@section('breadcrumb')
<li class="breadcrumb-item"><a href="{{ route('web.usuarios.index') }}">Usuarios</a></li>
<li class="breadcrumb-item active">Nuevo</li>
@endsection

@push('styles')
<link href="{{ asset('assets/css/mantenimientos.css') }}" rel="stylesheet">
<style>
    .usuario-form-container {
        max-width: 900px;
        margin: 0 auto;
    }

    .form-card {
        background: #fff;
        border-radius: 12px;
        border: 1px solid #e2e8f0;
        overflow: hidden;
        margin-bottom: 1.5rem;
    }

    .form-card-header {
        padding: 1.25rem 1.5rem;
        background: #f8fafc;
        border-bottom: 1px solid #e2e8f0;
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }

    .form-card-header i {
        width: 36px;
        height: 36px;
        background: #1a3a5c;
        color: #fff;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 0.9rem;
    }

    .form-card-header h5 {
        margin: 0;
        color: #1a3a5c;
        font-weight: 600;
        font-size: 1rem;
    }

    .form-card-header span {
        font-size: 0.8rem;
        color: #64748b;
        margin-top: 0.125rem;
    }

    .form-card-body {
        padding: 1.5rem;
    }

    .form-grid {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 1.25rem;
    }

    @media (max-width: 768px) {
        .form-grid {
            grid-template-columns: 1fr;
        }
    }

    .form-grid .full-width {
        grid-column: 1 / -1;
    }

    .form-label-custom {
        font-size: 0.8rem;
        font-weight: 600;
        color: #475569;
        margin-bottom: 0.375rem;
        display: block;
    }

    .form-label-custom .required {
        color: #ef4444;
        margin-left: 0.25rem;
    }

    .form-control-custom {
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        padding: 0.625rem 0.875rem;
        font-size: 0.9rem;
        transition: border-color 0.2s, box-shadow 0.2s;
    }

    .form-control-custom:focus {
        border-color: #1a3a5c;
        box-shadow: 0 0 0 3px rgba(26, 58, 92, 0.1);
        outline: none;
    }

    .form-control-custom.is-invalid {
        border-color: #ef4444;
    }

    .input-group-custom {
        position: relative;
    }

    .input-group-custom i {
        position: absolute;
        left: 0.875rem;
        top: 50%;
        transform: translateY(-50%);
        color: #94a3b8;
        font-size: 0.85rem;
        z-index: 2;
    }

    .input-group-custom .form-control-custom {
        padding-left: 2.5rem;
    }

    .password-wrapper {
        position: relative;
    }

    .password-toggle {
        position: absolute;
        right: 0.875rem;
        top: 50%;
        transform: translateY(-50%);
        background: none;
        border: none;
        color: #94a3b8;
        cursor: pointer;
        padding: 0;
        z-index: 2;
    }

    .password-toggle:hover {
        color: #64748b;
    }

    .perfil-option {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        padding: 0.875rem 1rem;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.2s;
        margin-bottom: 0.5rem;
    }

    .perfil-option:hover {
        background: #f8fafc;
        border-color: #cbd5e1;
    }

    .perfil-option.selected {
        background: rgba(26, 58, 92, 0.04);
        border-color: #1a3a5c;
    }

    .perfil-option input[type="radio"] {
        display: none;
    }

    .perfil-check {
        width: 20px;
        height: 20px;
        border-radius: 50%;
        border: 2px solid #e2e8f0;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }

    .perfil-option.selected .perfil-check {
        background: #1a3a5c;
        border-color: #1a3a5c;
    }

    .perfil-option.selected .perfil-check::after {
        content: '';
        width: 8px;
        height: 8px;
        background: #fff;
        border-radius: 50%;
    }

    .perfil-info h6 {
        margin: 0;
        font-size: 0.9rem;
        color: #1a3a5c;
        font-weight: 600;
    }

    .perfil-info span {
        font-size: 0.75rem;
        color: #64748b;
    }

    .perfil-accesos {
        display: flex;
        gap: 0.5rem;
        margin-left: auto;
    }

    .acceso-mini {
        font-size: 0.65rem;
        padding: 0.25rem 0.5rem;
        border-radius: 12px;
        font-weight: 600;
    }

    .acceso-mini.web {
        background: rgba(59, 130, 246, 0.1);
        color: #2563eb;
    }

    .acceso-mini.app {
        background: rgba(16, 185, 129, 0.1);
        color: #059669;
    }

    .form-actions {
        display: flex;
        gap: 0.75rem;
        justify-content: flex-end;
        padding: 1.25rem 1.5rem;
        background: #f8fafc;
        border-top: 1px solid #e2e8f0;
    }

    .password-requirements {
        background: #fefce8;
        border: 1px solid #fef08a;
        border-radius: 8px;
        padding: 0.875rem;
        margin-top: 0.75rem;
    }

    .password-requirements h6 {
        margin: 0 0 0.5rem;
        font-size: 0.75rem;
        color: #854d0e;
        font-weight: 600;
    }

    .password-requirements ul {
        margin: 0;
        padding-left: 1.25rem;
        font-size: 0.75rem;
        color: #a16207;
    }

    .password-requirements li {
        margin-bottom: 0.25rem;
    }
</style>
@endpush

@section('content')
<div class="usuario-form-container">
    <form action="{{ route('web.usuarios.store') }}" method="POST">
        @csrf

        {{-- Información Personal --}}
        <div class="form-card">
            <div class="form-card-header">
                <i class="fas fa-user"></i>
                <div>
                    <h5>Información Personal</h5>
                    <span>Datos básicos del usuario</span>
                </div>
            </div>
            <div class="form-card-body">
                <div class="form-grid">
                    <div>
                        <label class="form-label-custom">
                            Nombre <span class="required">*</span>
                        </label>
                        <div class="input-group-custom">
                            <i class="fas fa-user"></i>
                            <input type="text" name="usu_nombre"
                                   class="form-control form-control-custom @error('usu_nombre') is-invalid @enderror"
                                   value="{{ old('usu_nombre') }}" required placeholder="Ej: Juan Carlos">
                        </div>
                        @error('usu_nombre')
                        <div class="invalid-feedback d-block">{{ $message }}</div>
                        @enderror
                    </div>

                    <div>
                        <label class="form-label-custom">
                            Apellido <span class="required">*</span>
                        </label>
                        <div class="input-group-custom">
                            <i class="fas fa-user"></i>
                            <input type="text" name="usu_apellido"
                                   class="form-control form-control-custom @error('usu_apellido') is-invalid @enderror"
                                   value="{{ old('usu_apellido') }}" required placeholder="Ej: Pérez García">
                        </div>
                        @error('usu_apellido')
                        <div class="invalid-feedback d-block">{{ $message }}</div>
                        @enderror
                    </div>

                    <div>
                        <label class="form-label-custom">
                            Cédula <span class="required">*</span>
                        </label>
                        <div class="input-group-custom">
                            <i class="fas fa-id-card"></i>
                            <input type="text" name="usu_cedula"
                                   class="form-control form-control-custom @error('usu_cedula') is-invalid @enderror"
                                   value="{{ old('usu_cedula') }}" required placeholder="Ej: 1712345678"
                                   maxlength="13">
                        </div>
                        @error('usu_cedula')
                        <div class="invalid-feedback d-block">{{ $message }}</div>
                        @enderror
                    </div>

                    <div>
                        <label class="form-label-custom">
                            Fecha de Nacimiento
                        </label>
                        <div class="input-group-custom">
                            <i class="fas fa-calendar"></i>
                            <input type="date" name="usu_fecha_nac"
                                   class="form-control form-control-custom @error('usu_fecha_nac') is-invalid @enderror"
                                   value="{{ old('usu_fecha_nac') }}">
                        </div>
                        @error('usu_fecha_nac')
                        <div class="invalid-feedback d-block">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="full-width">
                        <label class="form-label-custom">
                            Correo Electrónico <span class="required">*</span>
                        </label>
                        <div class="input-group-custom">
                            <i class="fas fa-envelope"></i>
                            <input type="email" name="usu_email"
                                   class="form-control form-control-custom @error('usu_email') is-invalid @enderror"
                                   value="{{ old('usu_email') }}" required placeholder="Ej: usuario@empresa.com">
                        </div>
                        @error('usu_email')
                        <div class="invalid-feedback d-block">{{ $message }}</div>
                        @enderror
                    </div>
                </div>
            </div>
        </div>

        {{-- Credenciales de Acceso --}}
        <div class="form-card">
            <div class="form-card-header">
                <i class="fas fa-key"></i>
                <div>
                    <h5>Credenciales de Acceso</h5>
                    <span>Usuario y contraseña para el sistema</span>
                </div>
            </div>
            <div class="form-card-body">
                <div class="form-grid">
                    <div>
                        <label class="form-label-custom">
                            Nombre de Usuario <span class="required">*</span>
                        </label>
                        <div class="input-group-custom">
                            <i class="fas fa-at"></i>
                            <input type="text" name="usu_usuario"
                                   class="form-control form-control-custom @error('usu_usuario') is-invalid @enderror"
                                   value="{{ old('usu_usuario') }}" required placeholder="Ej: jperez"
                                   autocomplete="off">
                        </div>
                        @error('usu_usuario')
                        <div class="invalid-feedback d-block">{{ $message }}</div>
                        @enderror
                    </div>

                    <div></div>

                    <div>
                        <label class="form-label-custom">
                            Contraseña <span class="required">*</span>
                        </label>
                        <div class="input-group-custom password-wrapper">
                            <i class="fas fa-lock"></i>
                            <input type="password" name="password" id="password"
                                   class="form-control form-control-custom @error('password') is-invalid @enderror"
                                   required minlength="8" placeholder="Mínimo 8 caracteres"
                                   autocomplete="new-password">
                            <button type="button" class="password-toggle" onclick="togglePassword('password')">
                                <i class="fas fa-eye"></i>
                            </button>
                        </div>
                        @error('password')
                        <div class="invalid-feedback d-block">{{ $message }}</div>
                        @enderror
                    </div>

                    <div>
                        <label class="form-label-custom">
                            Confirmar Contraseña <span class="required">*</span>
                        </label>
                        <div class="input-group-custom password-wrapper">
                            <i class="fas fa-lock"></i>
                            <input type="password" name="password_confirmation" id="password_confirmation"
                                   class="form-control form-control-custom"
                                   required minlength="8" placeholder="Repita la contraseña"
                                   autocomplete="new-password">
                            <button type="button" class="password-toggle" onclick="togglePassword('password_confirmation')">
                                <i class="fas fa-eye"></i>
                            </button>
                        </div>
                    </div>

                    <div class="full-width">
                        <div class="password-requirements">
                            <h6><i class="fas fa-info-circle me-1"></i> Requisitos de contraseña</h6>
                            <ul>
                                <li>Mínimo 8 caracteres</li>
                                <li>Se recomienda incluir letras mayúsculas, minúsculas y números</li>
                                <li>El usuario deberá cambiar la contraseña en su primer inicio de sesión</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        {{-- Perfil y Permisos --}}
        <div class="form-card">
            <div class="form-card-header">
                <i class="fas fa-shield-alt"></i>
                <div>
                    <h5>Perfil de Acceso</h5>
                    <span>Seleccione el perfil que define los permisos del usuario</span>
                </div>
            </div>
            <div class="form-card-body">
                @error('per_id')
                <div class="alert alert-danger mb-3">{{ $message }}</div>
                @enderror

                <div class="perfiles-list">
                    @foreach($perfiles as $perfil)
                    <label class="perfil-option {{ old('per_id') == $perfil->per_id ? 'selected' : '' }}"
                           id="perfil-label-{{ $perfil->per_id }}">
                        <input type="radio" name="per_id" value="{{ $perfil->per_id }}"
                               {{ old('per_id') == $perfil->per_id ? 'checked' : '' }}
                               onchange="selectPerfil({{ $perfil->per_id }})" required>
                        <span class="perfil-check"></span>
                        <div class="perfil-info">
                            <h6>{{ $perfil->per_nombre }}</h6>
                            <span>{{ $perfil->per_descripcion ?? 'Sin descripción' }}</span>
                        </div>
                        <div class="perfil-accesos">
                            @if($perfil->per_acceso_web)
                            <span class="acceso-mini web">WEB</span>
                            @endif
                            @if($perfil->per_acceso_app)
                            <span class="acceso-mini app">APP</span>
                            @endif
                        </div>
                    </label>
                    @endforeach
                </div>
            </div>

            <div class="form-actions">
                <a href="{{ route('web.usuarios.index') }}" class="btn btn-outline-secondary">
                    <i class="fas fa-times me-1"></i> Cancelar
                </a>
                <button type="submit" class="btn btn-success">
                    <i class="fas fa-user-plus me-1"></i> Crear Usuario
                </button>
            </div>
        </div>
    </form>
</div>
@endsection

@push('scripts')
<script>
function togglePassword(inputId) {
    const input = document.getElementById(inputId);
    const icon = input.parentElement.querySelector('.password-toggle i');

    if (input.type === 'password') {
        input.type = 'text';
        icon.classList.remove('fa-eye');
        icon.classList.add('fa-eye-slash');
    } else {
        input.type = 'password';
        icon.classList.remove('fa-eye-slash');
        icon.classList.add('fa-eye');
    }
}

function selectPerfil(perfilId) {
    document.querySelectorAll('.perfil-option').forEach(el => {
        el.classList.remove('selected');
    });
    document.getElementById('perfil-label-' + perfilId).classList.add('selected');
}
</script>
@endpush
