<x-app-layout>

    <x-module-hero title="Edit User: {{ $user->name }}" description="Update user information, roles, and account status" icon="fa-user-edit">
        <x-slot name="backButton">
            <a href="{{ route('users.show', $user) }}" class="inline-flex items-center gap-1.5 text-[#475569] hover:text-[#1e293b] text-sm transition">
                <i class="fas fa-arrow-left text-xs"></i> Back to User Profile
            </a>
        </x-slot>
        <x-slot name="tabs">
            @can('view users')
                <a href="{{ route('users.index') }}"
                   class="module-hero-tab {{ request()->routeIs('users.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                          inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                          md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                    <i class="fas fa-list text-sm"></i> All Users
                </a>
            @endcan
            @can('view users')
                <a href="{{ route('users.show', $user) }}"
                   class="module-hero-tab {{ request()->routeIs('users.show') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                          inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                          md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                    <i class="fas fa-eye text-sm"></i> View Profile
                </a>
            @endcan
            @can('edit users')
                <a href="{{ route('users.edit', $user) }}"
                   class="module-hero-tab {{ request()->routeIs('users.edit') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                          inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                          md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                    <i class="fas fa-edit text-sm"></i> Edit User
                </a>
            @endcan
            @hasRole('Super Manager')
                @if(Route::has('roles.index'))
                    <a href="{{ route('roles.index') }}"
                       class="module-hero-tab {{ request()->routeIs('roles.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                              inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                              md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                        <i class="fas fa-user-tag text-sm"></i> Roles
                    </a>
                @endif
                @if(Route::has('admin.themes.index'))
                    <a href="{{ route('admin.themes.index') }}"
                       class="module-hero-tab {{ request()->routeIs('admin.themes.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                              inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                              md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                        <i class="fas fa-palette text-sm"></i> Themes
                    </a>
                @endif
            @endhasRole
        </x-slot>
        <x-slot name="actions">
            <div class="flex items-center gap-3 px-1 py-2">
                <div class="w-10 h-10 rounded-full bg-gradient-to-br from-purple-500 to-pink-500 flex items-center justify-center text-white font-bold text-sm">
                    {{ strtoupper(substr($user->name, 0, 2)) }}
                </div>
                <div class="text-[#1e293b]">
                    <p class="font-semibold text-sm">{{ $user->name }}</p>
                    <p class="text-xs text-[#475569]">{{ $user->email }}</p>
                </div>
            </div>
        </x-slot>
    </x-module-hero>

    <div class="p-6">

        @if($errors->any())
            <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg">
                <div class="flex items-start gap-2">
                    <i class="fas fa-exclamation-circle mt-0.5"></i>
                    <div>
                        <p class="font-semibold mb-1">Please correct the following errors:</p>
                        <ul class="list-disc list-inside space-y-0.5 text-sm">
                            @foreach($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                </div>
            </div>
        @endif

        <div class="bg-white rounded-lg shadow-sm overflow-hidden">
            <div class="p-6">

                <form method="POST" action="{{ route('users.update', $user) }}" x-data="{ isSubmitting: false }" @submit="isSubmitting = true">
                    @csrf
                    @method('PUT')

                    {{-- Basic Information --}}
                    <div class="mb-6">
                        <h3 class="text-base font-semibold text-gray-800 border-b border-gray-200 pb-2 mb-4">
                            <i class="fas fa-user-circle mr-2 text-blue-600"></i> Basic Information
                        </h3>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">
                                    Full Name <span class="text-red-500">*</span>
                                </label>
                                <input type="text" name="name" value="{{ old('name', $user->name) }}" required
                                    class="w-full px-3 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm @error('name') border-red-400 @enderror" />
                                @error('name')
                                    <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                                @enderror
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">
                                    Email Address <span class="text-red-500">*</span>
                                </label>
                                <input type="email" name="email" value="{{ old('email', $user->email) }}" required
                                    class="w-full px-3 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm @error('email') border-red-400 @enderror" />
                                @error('email')
                                    <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>
                    </div>

                    {{-- Change Password (Optional) --}}
                    <div class="mb-6">
                        <h3 class="text-base font-semibold text-gray-800 border-b border-gray-200 pb-2 mb-4">
                            <i class="fas fa-lock mr-2 text-purple-600"></i> Change Password
                            <span class="text-xs font-normal text-gray-500 ml-2">(optional — leave blank to keep current)</span>
                        </h3>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">New Password</label>
                                <input type="password" name="password"
                                    class="w-full px-3 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm @error('password') border-red-400 @enderror"
                                    placeholder="Minimum 8 characters" />
                                @error('password')
                                    <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                                @enderror
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Confirm New Password</label>
                                <input type="password" name="password_confirmation"
                                    class="w-full px-3 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm"
                                    placeholder="Re-enter new password" />
                            </div>
                        </div>
                    </div>

                    {{-- Role Assignment --}}
                    <div class="mb-6">
                        <h3 class="text-base font-semibold text-gray-800 border-b border-gray-200 pb-2 mb-4">
                            <i class="fas fa-user-tag mr-2 text-orange-600"></i> Role Assignment
                            <span class="text-red-500">*</span>
                        </h3>
                        <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3">
                            @foreach($roles as $role)
                                <label class="flex items-center gap-3 p-3 border-2 border-gray-200 rounded-lg hover:border-orange-400 hover:bg-orange-50 transition cursor-pointer">
                                    <input type="checkbox" name="roles[]" value="{{ $role->name }}"
                                        {{ in_array($role->name, old('roles', $user->roles->pluck('name')->toArray())) ? 'checked' : '' }}
                                        class="w-4 h-4 text-orange-600 border-gray-300 rounded focus:ring-orange-500" />
                                    <div>
                                        <span class="text-sm font-medium text-gray-900">{{ $role->name }}</span>
                                        @if($role->permissions->count() > 0)
                                            <p class="text-xs text-gray-500">{{ $role->permissions->count() }} permissions</p>
                                        @endif
                                    </div>
                                </label>
                            @endforeach
                        </div>
                        @error('roles')
                            <p class="mt-2 text-xs text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    {{-- Account Status --}}
                    <div class="mb-6">
                        <h3 class="text-base font-semibold text-gray-800 border-b border-gray-200 pb-2 mb-4">
                            <i class="fas fa-toggle-on mr-2 text-green-600"></i> Account Status
                        </h3>
                        <label class="flex items-center gap-3 p-3 border-2 border-gray-200 rounded-lg hover:border-green-400 hover:bg-green-50 transition cursor-pointer w-fit">
                            <input type="checkbox" name="is_active" value="1"
                                {{ old('is_active', $user->is_active) ? 'checked' : '' }}
                                class="w-4 h-4 text-green-600 border-gray-300 rounded focus:ring-green-500" />
                            <div>
                                <span class="text-sm font-medium text-gray-900">Active Account</span>
                                <p class="text-xs text-gray-500">User can log in and access the system</p>
                            </div>
                        </label>
                        @if($user->id === auth()->id())
                            <div class="mt-3 p-3 bg-yellow-50 border border-yellow-200 rounded-lg text-xs text-yellow-800">
                                <i class="fas fa-exclamation-triangle mr-1"></i>
                                <strong>Warning:</strong> You are editing your own account. Be careful not to deactivate yourself or remove all your roles.
                            </div>
                        @endif
                    </div>

                    {{-- Account Info (read-only) --}}
                    <div class="mb-6 p-4 bg-gray-50 rounded-lg border border-gray-200">
                        <h3 class="text-sm font-semibold text-gray-700 mb-3">
                            <i class="fas fa-info-circle mr-1 text-gray-500"></i> Account Information
                        </h3>
                        <dl class="grid grid-cols-1 sm:grid-cols-2 gap-3 text-sm">
                            <div>
                                <dt class="text-xs text-gray-500">Account Created</dt>
                                <dd class="font-medium text-gray-800">{{ $user->created_at->format('F j, Y') }} <span class="text-gray-400 text-xs">({{ $user->created_at->diffForHumans() }})</span></dd>
                            </div>
                            <div>
                                <dt class="text-xs text-gray-500">Last Updated</dt>
                                <dd class="font-medium text-gray-800">{{ $user->updated_at->format('F j, Y') }} <span class="text-gray-400 text-xs">({{ $user->updated_at->diffForHumans() }})</span></dd>
                            </div>
                            <div>
                                <dt class="text-xs text-gray-500">Email Verified</dt>
                                <dd>
                                    @if($user->email_verified_at)
                                        <span class="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-semibold text-green-800 bg-green-100 rounded-full">
                                            <i class="fas fa-check-circle"></i> Verified
                                        </span>
                                    @else
                                        <span class="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-semibold text-yellow-800 bg-yellow-100 rounded-full">
                                            <i class="fas fa-exclamation-circle"></i> Not Verified
                                        </span>
                                    @endif
                                </dd>
                            </div>
                            <div>
                                <dt class="text-xs text-gray-500">Current Status</dt>
                                <dd>
                                    @if($user->is_active)
                                        <span class="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-semibold text-green-800 bg-green-100 rounded-full">
                                            <i class="fas fa-check-circle"></i> Active
                                        </span>
                                    @else
                                        <span class="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-semibold text-red-800 bg-red-100 rounded-full">
                                            <i class="fas fa-times-circle"></i> Inactive
                                        </span>
                                    @endif
                                </dd>
                            </div>
                        </dl>
                    </div>

                    {{-- Form Actions --}}
                    <div class="flex flex-col sm:flex-row items-center justify-between gap-3 pt-4 border-t border-gray-200">
                        <a href="{{ route('users.index') }}" class="w-full sm:w-auto px-5 py-2.5 bg-gray-500 text-white rounded-lg hover:bg-gray-600 transition font-medium text-sm text-center">
                            <i class="fas fa-times mr-1"></i> Cancel
                        </a>
                        <button type="submit" :disabled="isSubmitting"
                            class="w-full sm:w-auto px-6 py-2.5 bg-[#1a4731] text-white rounded-lg hover:bg-[#276749] transition font-medium text-sm disabled:opacity-50">
                            <i x-show="!isSubmitting" class="fas fa-save mr-1"></i>
                            <i x-show="isSubmitting" class="fas fa-spinner fa-spin mr-1"></i>
                            <span x-text="isSubmitting ? 'Updating...' : 'Update User'"></span>
                        </button>
                    </div>

                </form>
            </div>
        </div>
    </div>

</x-app-layout>
