<x-admin-layout>
    <x-slot name="header">
        <div class="flex justify-between items-center">
            <div>
                <h2 class="font-semibold text-lg text-gray-800 dark:text-gray-200 leading-tight">
                    <i class="fas fa-tachometer-alt mr-2"></i>Editează Utilizator: {{ $user->name }}
                </h2>
                <p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
                    <a href="{{ route('dashboard') }}" class="text-blue-600 dark:text-blue-400 hover:underline">Dashboard</a>
                    <span class="mx-2">/</span>
                    <a href="{{ route('admin.dashboard') }}" class="text-blue-600 dark:text-blue-400 hover:underline">Admin</a>
                    <span class="mx-2">/</span>
                    <a href="{{ route('admin.users.index') }}" class="text-blue-600 dark:text-blue-400 hover:underline">Utilizatori</a>
                    <span class="mx-2">/</span>
                    <span>Editare</span>
                </p>
            </div>
            <a href="{{ route('admin.users.index') }}" class="bg-gray-600 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded-lg transition">
                <i class="fas fa-arrow-left mr-2"></i>Înapoi
            </a>
        </div>
    </x-slot>

    <div class="py-6">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <form method="POST" action="{{ route('admin.users.update', $user) }}" x-data="{ userType: '{{ $user->user_type ?? 'individual' }}' }">
                @csrf
                @method('PUT')

                <div class="space-y-6">
                    <!-- Informații Utilizator -->
                    <div class="bg-white dark:bg-gray-800 shadow-sm rounded-lg">
                        <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
                            <h3 class="text-lg font-medium text-gray-900 dark:text-gray-100">
                                <i class="fas fa-user mr-2 text-blue-500"></i>
                                Informații Utilizator
                            </h3>
                        </div>
                        <div class="p-6">
                            <div class="flex items-center mb-4">
                                <div class="w-16 h-16 bg-gradient-to-br from-blue-500 to-purple-600 rounded-full flex items-center justify-center mr-4">
                                    <span class="text-white text-xl font-bold">
                                        {{ strtoupper(substr($user->name, 0, 1)) }}
                                    </span>
                                </div>
                                <div>
                                    <h4 class="text-lg font-semibold text-gray-900 dark:text-gray-100">{{ $user->name }}</h4>
                                    <p class="text-sm text-gray-600 dark:text-gray-400">{{ $user->email }}</p>
                                </div>
                            </div>

                            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mt-6">
                                <!-- Status -->
                                <div class="flex flex-col">
                                    <span class="text-sm text-gray-600 dark:text-gray-400 mb-2">Status</span>
                                    @if($user->is_active)
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800 self-start">
                                            <i class="fas fa-check-circle mr-1"></i> Activ
                                        </span>
                                    @else
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800 self-start">
                                            <i class="fas fa-times-circle mr-1"></i> Inactiv
                                        </span>
                                    @endif
                                </div>

                                <!-- Rol -->
                                <div class="flex flex-col">
                                    <span class="text-sm text-gray-600 dark:text-gray-400 mb-2">Rol</span>
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium self-start
                                        {{ $user->role === 'admin' ? 'bg-red-100 text-red-800' : ($user->role === 'reseller' ? 'bg-purple-100 text-purple-800' : 'bg-blue-100 text-blue-800') }}">
                                        @if($user->role === 'admin')
                                            <i class="fas fa-crown mr-1"></i> Administrator
                                        @elseif($user->role === 'reseller')
                                            <i class="fas fa-store mr-1"></i> Reseller
                                        @else
                                            <i class="fas fa-user mr-1"></i> Utilizator
                                        @endif
                                    </span>
                                </div>

                                <!-- Tip Cont -->
                                <div class="flex flex-col">
                                    <span class="text-sm text-gray-600 dark:text-gray-400 mb-2">Tip cont</span>
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800 self-start">
                                        @if($user->user_type === 'company')
                                            <i class="fas fa-building mr-1"></i> Companie
                                        @else
                                            <i class="fas fa-user mr-1"></i> Personal
                                        @endif
                                    </span>
                                </div>

                                <!-- Data Creării -->
                                <div class="flex flex-col">
                                    <span class="text-sm text-gray-600 dark:text-gray-400 mb-2">Membru din</span>
                                    <span class="text-sm font-medium text-gray-900 dark:text-gray-100">
                                        {{ $user->created_at->format('d.m.Y') }}
                                    </span>
                                </div>
                            </div>

                            <!-- Plan -->
                            @if($user->plan)
                                <div class="mt-6 bg-blue-50 dark:bg-blue-900/20 rounded-lg p-4 inline-flex items-center">
                                    <i class="fas fa-layer-group text-blue-500 mr-2"></i>
                                    <div>
                                        <p class="text-sm font-medium text-blue-900 dark:text-blue-100">{{ $user->plan->name }}</p>
                                        <p class="text-xs text-blue-700 dark:text-blue-300">Plan actual</p>
                                    </div>
                                </div>
                            @endif
                        </div>
                    </div>
                            <!-- Informații de Bază -->
                            <div class="bg-white dark:bg-gray-800 shadow-sm rounded-lg">
                                <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
                                    <h3 class="text-lg font-medium text-gray-900 dark:text-gray-100">
                                        <i class="fas fa-user-cog mr-2 text-blue-500"></i>
                                        Informații de bază
                                    </h3>
                                </div>
                                <div class="p-6 space-y-6">
                                    <!-- Tip Utilizator -->
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">
                                            Tip cont *
                                        </label>
                                        <div class="grid grid-cols-2 gap-3">
                                            <label class="relative">
                                                <input type="radio" name="user_type" value="individual" x-model="userType"
                                                       class="sr-only" {{ old('user_type', $user->user_type) === 'individual' ? 'checked' : '' }}>
                                                <div :class="userType === 'individual' ? 'border-blue-500 bg-blue-50 dark:bg-blue-900/20' : 'border-gray-300 dark:border-gray-600'"
                                                     class="border-2 rounded-lg p-3 cursor-pointer hover:border-blue-300 transition-colors">
                                                    <div class="flex items-center">
                                                        <i class="fas fa-user mr-2 text-blue-500"></i>
                                                        <span class="text-sm font-medium">Persoană fizică</span>
                                                    </div>
                                                </div>
                                            </label>

                                            <label class="relative">
                                                <input type="radio" name="user_type" value="company" x-model="userType"
                                                       class="sr-only" {{ old('user_type', $user->user_type) === 'company' ? 'checked' : '' }}>
                                                <div :class="userType === 'company' ? 'border-blue-500 bg-blue-50 dark:bg-blue-900/20' : 'border-gray-300 dark:border-gray-600'"
                                                     class="border-2 rounded-lg p-3 cursor-pointer hover:border-blue-300 transition-colors">
                                                    <div class="flex items-center">
                                                        <i class="fas fa-building mr-2 text-blue-500"></i>
                                                        <span class="text-sm font-medium">Persoană juridică</span>
                                                    </div>
                                                </div>
                                            </label>
                                        </div>
                                        @error('user_type')
                                            <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                        @enderror
                                    </div>

                                    <!-- Nume Complet -->
                                    <div>
                                        <label for="name" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                            <i class="fas fa-user mr-1"></i>
                                            Nume Complet Afișat *
                                        </label>
                                        <input type="text" name="name" id="name" value="{{ old('name', $user->name) }}" required
                                               class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300">
                                        @error('name')
                                            <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                        @enderror
                                    </div>

                                    <!-- Email -->
                                    <div>
                                        <label for="email" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                            <i class="fas fa-envelope mr-1"></i>
                                            Email *
                                        </label>
                                        <input type="email" name="email" id="email" value="{{ old('email', $user->email) }}" required
                                               class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300">
                                        @error('email')
                                            <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                        @enderror
                                    </div>
                                </div>
                            </div>

                            <!-- Informații Personale -->
                            <div class="bg-white dark:bg-gray-800 shadow-sm rounded-lg">
                                <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
                                    <h3 class="text-lg font-medium text-gray-900 dark:text-gray-100">
                                        <i class="fas fa-id-card mr-2 text-green-500"></i>
                                        Informații personale
                                    </h3>
                                </div>
                                <div class="p-6 space-y-6">
                                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                        <!-- Nume -->
                                        <div>
                                            <label for="last_name" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Nume *
                                            </label>
                                            <input type="text" name="last_name" id="last_name" value="{{ old('last_name', $user->last_name) }}"
                                                   class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300">
                                            @error('last_name')
                                                <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                            @enderror
                                        </div>

                                        <!-- Prenume -->
                                        <div>
                                            <label for="first_name" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                Prenume *
                                            </label>
                                            <input type="text" name="first_name" id="first_name" value="{{ old('first_name', $user->first_name) }}"
                                                   class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300">
                                            @error('first_name')
                                                <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                            @enderror
                                        </div>
                                    </div>

                                    <!-- Telefon -->
                                    <div>
                                        <label for="phone" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                            Telefon *
                                        </label>
                                        <input type="tel" name="phone" id="phone" value="{{ old('phone', $user->phone) }}"
                                               class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300">
                                        @error('phone')
                                            <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                        @enderror
                                    </div>

                                    <!-- Câmpuri Companie -->
                                    <div x-show="userType === 'company' || '{{ $user->user_type }}' === 'company'"
                                         x-transition:enter="transition ease-out duration-200"
                                         x-transition:enter-start="opacity-0 transform scale-95"
                                         x-transition:enter-end="opacity-100 transform scale-100">

                                        <div class="border-t border-gray-200 dark:border-gray-700 pt-6 mt-6">
                                            <h4 class="text-base font-medium text-gray-900 dark:text-gray-100 mb-4">
                                                <i class="fas fa-building mr-2 text-blue-500"></i>
                                                Informații companie
                                            </h4>

                                            <div class="space-y-4">
                                                <!-- CUI -->
                                                <div>
                                                    <label for="company_cui" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                        CUI *
                                                    </label>
                                                    <input type="text" name="company_cui" id="company_cui" value="{{ old('company_cui', $user->company_cui) }}"
                                                           placeholder="000000000000" class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300">
                                                    @error('company_cui')
                                                        <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                                    @enderror
                                                </div>

                                                <!-- Nume Companie -->
                                                <div>
                                                    <label for="company_name" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                        Nume companie *
                                                    </label>
                                                    <input type="text" name="company_name" id="company_name" value="{{ old('company_name', $user->company_name) }}"
                                                           class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300">
                                                    @error('company_name')
                                                        <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                                    @enderror
                                                </div>

                                                <!-- Registrul Comerțului -->
                                                <div>
                                                    <label for="company_reg_com" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                                        Nr. Reg. Com.
                                                    </label>
                                                    <input type="text" name="company_reg_com" id="company_reg_com" value="{{ old('company_reg_com', $user->company_reg_com) }}"
                                                           placeholder="J40/123456/2020" class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300">
                                                    @error('company_reg_com')
                                                        <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                                    @enderror
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- Parolă -->
                            <div class="bg-white dark:bg-gray-800 shadow-sm rounded-lg">
                                <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
                                    <h3 class="text-lg font-medium text-gray-900 dark:text-gray-100">
                                        <i class="fas fa-lock mr-2 text-yellow-500"></i>
                                        Schimbare parolă
                                    </h3>
                                </div>
                                <div class="p-6 space-y-6">
                                    <!-- Password (Optional) -->
                                    <div>
                                        <label for="password" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                            <i class="fas fa-lock mr-1"></i>
                                            Parolă Nouă (opțional)
                                        </label>
                                        <input type="password" name="password" id="password"
                                               class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300"
                                               placeholder="Lasă gol pentru a păstra parola actuală">
                                        @error('password')
                                            <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                        @enderror
                        </div>

                        <!-- Confirm Password -->
                        <div class="mb-6">
                            <label for="password_confirmation" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                <i class="fas fa-lock mr-1"></i>
                                Confirmă Parola Nouă
                            </label>
                            <input type="password" name="password_confirmation" id="password_confirmation" class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300">
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                            <!-- Role -->
                            <div>
                                <label for="role" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    <i class="fas fa-user-tag mr-1"></i>
                                    Rol
                                </label>
                                <select name="role" id="role" required class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300" onchange="toggleResellerPlan()">
                                    <option value="user" {{ old('role', $user->role) == 'user' ? 'selected' : '' }}>Utilizator</option>
                                    <option value="reseller" {{ old('role', $user->role) == 'reseller' ? 'selected' : '' }}>Reseller</option>
                                    <option value="admin" {{ old('role', $user->role) == 'admin' ? 'selected' : '' }}>Administrator</option>
                                </select>
                                @error('role')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- Plan (doar pentru utilizatori normali) -->
                            <div id="plan-field" style="display: {{ old('role', $user->role) == 'user' ? 'block' : 'none' }};">
                                <label for="plan_id" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    <i class="fas fa-box mr-1"></i>
                                    Pachet
                                </label>
                                <select name="plan_id" id="plan_id" class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300">
                                    <option value="">Selectează pachetul</option>
                                    @foreach($plans as $plan)
                                        <option value="{{ $plan->id }}" {{ old('plan_id', $user->plan_id) == $plan->id ? 'selected' : '' }}>
                                            {{ $plan->name }} @if($plan->price > 0) ({{ number_format($plan->price, 2) }} RON) @else (GRATUIT) @endif
                                        </option>
                                    @endforeach
                                </select>
                                @error('plan_id')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>

                        <!-- Reseller Plan (visible doar pentru reseller) -->
                        <div id="reseller-plan-field" class="mb-6" style="display: {{ old('role', $user->role) == 'reseller' ? 'block' : 'none' }};">
                            <label for="reseller_plan_id" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                Pachet Reseller <span class="text-red-500">*</span>
                            </label>
                            <select name="reseller_plan_id"
                                    id="reseller_plan_id"
                                    class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300">
                                <option value="">Selectează pachetul reseller</option>
                                @foreach($resellerPlans as $resellerPlan)
                                    <option value="{{ $resellerPlan->id }}" {{ old('reseller_plan_id', $user->reseller_plan_id) == $resellerPlan->id ? 'selected' : '' }}>
                                        {{ $resellerPlan->name }} - {{ $resellerPlan->max_clients }} clienți ({{ number_format($resellerPlan->price, 2) }} RON/lună)
                                    </option>
                                @endforeach
                            </select>
                            @error('reseller_plan_id')
                                <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                            @enderror
                            <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                                <i class="fas fa-info-circle mr-1"></i>
                                Pachetul reseller determină câți clienți poate adăuga reseller-ul
                            </p>
                        </div>

                        <!-- Is Active -->
                        <div class="mb-6">
                            <label class="flex items-center">
                                <input type="checkbox" name="is_active" value="1" {{ old('is_active', $user->is_active) ? 'checked' : '' }} class="rounded border-gray-300 dark:border-gray-700 dark:bg-gray-900 text-indigo-600 shadow-sm focus:ring-indigo-500">
                                <span class="ml-2 text-sm text-gray-700 dark:text-gray-300">
                                    <i class="fas fa-check-circle mr-1 text-green-600"></i>
                                    Cont activ
                                </span>
                            </label>
                        </div>

                        <!-- Info Box -->
                        <div class="bg-blue-50 dark:bg-blue-900/30 border border-blue-200 dark:border-blue-800 rounded-lg p-4 mb-6">
                            <div class="flex">
                                <div class="flex-shrink-0">
                                    <i class="fas fa-info-circle text-blue-600 dark:text-blue-400"></i>
                                </div>
                                <div class="ml-3">
                                    <p class="text-sm text-blue-800 dark:text-blue-200">
                                        <strong>Statistici utilizator:</strong><br>
                                        @if($user->role === 'reseller')
                                            Clienți: <strong>{{ $user->clients()->count() }}</strong> / {{ $user->resellerPlan ? $user->resellerPlan->max_clients : 'N/A' }}<br>
                                            Clienți activi: <strong>{{ $user->clients()->where('is_active', true)->count() }}</strong><br>
                                        @else
                                            Zone DNS: <strong>{{ $user->dnsZones->count() + \App\Models\DnsZoneAssignment::where('user_id', $user->id)->count() }}</strong>
                                            ({{ $user->dnsZones->count() }} locale, {{ \App\Models\DnsZoneAssignment::where('user_id', $user->id)->count() }} externe)<br>
                                            @if($user->plan)
                                                Înregistrări DNS: <strong>{{ $user->getTotalRecordsCount() }}</strong> / {{ $user->plan->subdomains_limit }}<br>
                                            @endif
                                        @endif
                                        Cont creat la: <strong>{{ $user->created_at->format('d.m.Y H:i') }}</strong>
                                    </p>
                                </div>
                            </div>
                        </div>

                    <div class="flex items-center justify-between">
                        <a href="{{ route('admin.users.index') }}" class="bg-gray-600 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded-lg transition">
                            <i class="fas fa-arrow-left mr-2"></i>
                            Înapoi
                        </a>
                        <button type="submit" class="bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-2 px-4 rounded-lg transition">
                            <i class="fas fa-save mr-2"></i>
                            Actualizează Utilizatorul
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script>
        function toggleResellerPlan() {
            const role = document.getElementById('role').value;
            const planField = document.getElementById('plan-field');
            const planSelect = document.getElementById('plan_id');
            const resellerPlanField = document.getElementById('reseller-plan-field');
            const resellerPlanSelect = document.getElementById('reseller_plan_id');

            if (role === 'reseller') {
                // Ascunde plan normal, arată reseller plan
                planField.style.display = 'none';
                planSelect.required = false;

                resellerPlanField.style.display = 'block';
                resellerPlanSelect.required = true;
            } else if (role === 'user') {
                // Arată plan normal, ascunde reseller plan
                planField.style.display = 'block';
                planSelect.required = true;

                resellerPlanField.style.display = 'none';
                resellerPlanSelect.required = false;
            } else {
                // Admin - ascunde ambele
                planField.style.display = 'none';
                planSelect.required = false;

                resellerPlanField.style.display = 'none';
                resellerPlanSelect.required = false;
            }
        }

        // Rulează la încărcarea paginii pentru a seta starea corectă
        document.addEventListener('DOMContentLoaded', function() {
            toggleResellerPlan();
        });
    </script>
</x-admin-layout>

