<x-layouts.admin :title="'Edit User: ' . $user->first_name . ' | Admin Suite'" activePage="admin-users">
    <div class="w-full">
        <div class="flex items-center gap-4 mb-10">
            <a href="{{ route('admin.users.index') }}"
                class="material-symbols-outlined text-on-surface-variant hover:text-primary transition-colors">arrow_back</a>
            <div>
                <h2 class="font-serif text-3xl text-on-surface">Edit User</h2>
                <p class="text-on-surface-variant text-sm mt-1">{{ $user->first_name }} {{ $user->last_name }} &middot;
                    {{ $user->email }}</p>
            </div>
        </div>

        <form method="POST" action="{{ route('admin.users.update', $user) }}" class="space-y-10">
            @csrf
            @method('PUT')

            <div class="bg-surface-container-lowest p-8 space-y-6">
                <h3 class="font-label text-lg font-bold text-on-surface">Profile Information</h3>

                <div class="grid grid-cols-2 gap-6">
                    <div>
                        <label
                            class="block text-xs font-label uppercase tracking-widest text-on-surface-variant mb-2">First
                            Name</label>
                        <input type="text" name="first_name" value="{{ old('first_name', $user->first_name) }}"
                            class="w-full bg-surface-container-high border-none rounded-md px-4 py-3 text-sm focus:ring-2 focus:ring-secondary transition-all">
                        @error('first_name')
                            <p class="text-xs text-error mt-1">{{ $message }}</p>
                        @enderror
                    </div>
                    <div>
                        <label
                            class="block text-xs font-label uppercase tracking-widest text-on-surface-variant mb-2">Last
                            Name</label>
                        <input type="text" name="last_name" value="{{ old('last_name', $user->last_name) }}"
                            class="w-full bg-surface-container-high border-none rounded-md px-4 py-3 text-sm focus:ring-2 focus:ring-secondary transition-all">
                        @error('last_name')
                            <p class="text-xs text-error mt-1">{{ $message }}</p>
                        @enderror
                    </div>
                </div>

                <div>
                    <label
                        class="block text-xs font-label uppercase tracking-widest text-on-surface-variant mb-2">Email</label>
                    <input type="email" name="email" value="{{ old('email', $user->email) }}"
                        class="w-full bg-surface-container-high border-none rounded-md px-4 py-3 text-sm focus:ring-2 focus:ring-secondary transition-all">
                    @error('email')
                        <p class="text-xs text-error mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div class="grid grid-cols-2 gap-6">
                    <div>
                        <label
                            class="block text-xs font-label uppercase tracking-widest text-on-surface-variant mb-2">Phone</label>
                        <input type="text" name="phone" value="{{ old('phone', $user->phone) }}"
                            class="w-full bg-surface-container-high border-none rounded-md px-4 py-3 text-sm focus:ring-2 focus:ring-secondary transition-all">
                        @error('phone')
                            <p class="text-xs text-error mt-1">{{ $message }}</p>
                        @enderror
                    </div>
                    <div>
                        <label
                            class="block text-xs font-label uppercase tracking-widest text-on-surface-variant mb-2">Date
                            of Birth</label>
                        <input type="date" name="dob" value="{{ old('dob', $user->dob) }}"
                            class="w-full bg-surface-container-high border-none rounded-md px-4 py-3 text-sm focus:ring-2 focus:ring-secondary transition-all">
                        @error('dob')
                            <p class="text-xs text-error mt-1">{{ $message }}</p>
                        @enderror
                    </div>
                </div>

                <div>
                    <label
                        class="block text-xs font-label uppercase tracking-widest text-on-surface-variant mb-2">Address</label>
                    <textarea name="address" rows="2"
                        class="w-full bg-surface-container-high border-none rounded-md px-4 py-3 text-sm focus:ring-2 focus:ring-secondary transition-all">{{ old('address', $user->address) }}</textarea>
                    @error('address')
                        <p class="text-xs text-error mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label
                        class="block text-xs font-label uppercase tracking-widest text-on-surface-variant mb-2">Country</label>
                    <input type="text" name="country" value="{{ old('country', $user->country) }}"
                        class="w-full bg-surface-container-high border-none rounded-md px-4 py-3 text-sm focus:ring-2 focus:ring-secondary transition-all">
                    @error('country')
                        <p class="text-xs text-error mt-1">{{ $message }}</p>
                    @enderror
                </div>
            </div>

            <div class="bg-surface-container-lowest p-8 space-y-6">
                <h3 class="font-label text-lg font-bold text-on-surface">Account Controls</h3>

                <div class="grid grid-cols-2 gap-6">
                    <div>
                        <label
                            class="block text-xs font-label uppercase tracking-widest text-on-surface-variant mb-2">Status</label>
                        <select name="status"
                            class="w-full bg-surface-container-high border-none rounded-md px-4 py-3 text-sm focus:ring-2 focus:ring-secondary transition-all">
                            @foreach (\App\Enums\UserStatus::cases() as $status)
                                <option value="{{ $status->value }}"
                                    {{ old('status', $user->status->value) === $status->value ? 'selected' : '' }}>
                                    {{ ucfirst($status->value) }}
                                </option>
                            @endforeach
                        </select>
                        @error('status')
                            <p class="text-xs text-error mt-1">{{ $message }}</p>
                        @enderror
                    </div>
                    <div>
                        <label
                            class="block text-xs font-label uppercase tracking-widest text-on-surface-variant mb-2">Type</label>
                        <select name="type"
                            class="w-full bg-surface-container-high border-none rounded-md px-4 py-3 text-sm focus:ring-2 focus:ring-secondary transition-all">
                            @foreach (\App\Enums\UserType::cases() as $type)
                                <option value="{{ $type->value }}"
                                    {{ old('type', $user->type->value) === $type->value ? 'selected' : '' }}>
                                    {{ ucfirst($type->value) }}
                                </option>
                            @endforeach
                        </select>
                        @error('type')
                            <p class="text-xs text-error mt-1">{{ $message }}</p>
                        @enderror
                    </div>
                </div>
            </div>

            <div class="flex justify-between items-center">
                <button type="submit"
                    class="bg-gradient-to-r from-primary to-primary-container text-on-primary px-8 py-3 rounded-md text-sm font-label font-medium flex items-center gap-2">
                    <span class="material-symbols-outlined text-sm">save</span>
                    Update User
                </button>
            </div>
        </form>

        <div class="mt-12 pt-8 border-t border-error/20">
            <div class="bg-error-container/30 rounded-lg p-6 flex items-center justify-between">
                <div>
                    <h4 class="text-sm font-label font-bold text-error">Danger Zone</h4>
                    <p class="text-xs text-on-surface-variant mt-1">Permanently delete this user and all associated data. This action cannot be undone.</p>
                </div>
                <form method="POST" action="{{ route('admin.users.destroy', $user) }}"
                    onsubmit="return confirm('Are you sure you want to permanently delete this user? This cannot be undone.')">
                    @csrf
                    @method('DELETE')
                    <button type="submit"
                        class="bg-error text-on-error px-6 py-2.5 rounded-md text-xs font-label font-bold uppercase tracking-widest flex items-center gap-2 hover:opacity-90 transition-opacity">
                        <span class="material-symbols-outlined text-sm">delete_forever</span>
                        Delete User
                    </button>
                </form>
            </div>
        </div>
    </div>
</x-layouts.admin>
