when($this->search, fn($q, $search) => $q->where('name', 'like', "%{$search}%") ->orWhere('email', 'like', "%{$search}%") ) ->when($this->roleFilter, fn($q, $role) => $q->whereHas('roles', fn($roleQuery) => $roleQuery->where('name', $role) ) ) ->when($this->parentPartnerFilter, fn($q, $parentPartnerId) => $q->whereHas('partner', fn($partnerQuery) => $partnerQuery->where('parent_partner_id', $parentPartnerId) ) ) ->when($this->brandFilter, fn($q, $brand) => $q->whereHas('partner', fn($partnerQuery) => $partnerQuery->where('brand', $brand) ) ) ->when($this->setupStatusFilter === 'completed', fn($q) => $q->whereHas('partner', fn($partnerQuery) => $partnerQuery->whereNotNull('setup_completed_at') ) ) ->when($this->setupStatusFilter === 'pending', fn($q) => $q->whereHas('partner', fn($partnerQuery) => $partnerQuery->whereNull('setup_completed_at') ) ) ->when($this->setupStatusFilter === 'no_partner', fn($q) => $q->whereNull('partner_id') ); // Finde alle Partner, die als parent_partner_id verwendet werden $parentPartnerIds = \App\Models\Partner::whereNotNull('parent_partner_id') ->distinct() ->pluck('parent_partner_id'); $availableParentPartners = \App\Models\Partner::whereIn('id', $parentPartnerIds) ->orderBy('company_name') ->get(); // Finde alle verfügbaren Brands $availableBrands = \App\Models\Partner::whereNotNull('brand') ->distinct() ->pluck('brand') ->sort(); // Zähle User mit abgeschlossenem Setup $setupCompletedCount = User::whereHas('partner', function($q) { $q->whereNotNull('setup_completed_at'); })->count(); return [ 'users' => $query->orderBy($this->sortField, $this->sortDirection)->paginate(15), 'totalUsers' => User::count(), 'verifiedUsers' => User::whereNotNull('email_verified_at')->count(), 'setupCompletedUsers' => $setupCompletedCount, 'availableRoles' => \Spatie\Permission\Models\Role::orderBy('name')->get(), 'availableParentPartners' => $availableParentPartners, 'availableBrands' => $availableBrands, 'selectedUser' => $this->selectedUserId ? User::find($this->selectedUserId) : null, 'editUser' => $this->editUserId ? User::find($this->editUserId) : null, 'viewUser' => $this->viewUserId ? User::with(['partner.brand', 'roles'])->find($this->viewUserId) : null, ]; } public function sortBy(string $field): void { if ($this->sortField === $field) { $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'; } else { $this->sortField = $field; $this->sortDirection = 'asc'; } } public function updatingSearch(): void { $this->resetPage(); } public function updatingRoleFilter(): void { $this->resetPage(); } public function updatingParentPartnerFilter(): void { $this->resetPage(); } public function updatingBrandFilter(): void { $this->resetPage(); } public function updatingSetupStatusFilter(): void { $this->resetPage(); } public function openRoleModal(int $userId): void { $user = User::with('roles')->findOrFail($userId); $this->selectedUserId = $userId; $this->selectedRoles = $user->roles->pluck('name')->toArray(); $this->showRoleModal = true; } public function saveRoles(): void { if (!$this->selectedUserId) { return; } $user = User::findOrFail($this->selectedUserId); $user->syncRoles($this->selectedRoles); $this->showRoleModal = false; $this->selectedUserId = null; $this->selectedRoles = []; // Optional: Flash message session()->flash('message', __('Roles updated successfully!')); } public function closeRoleModal(): void { $this->showRoleModal = false; $this->selectedUserId = null; $this->selectedRoles = []; } public function openEditModal(int $userId): void { $user = User::findOrFail($userId); $this->editUserId = $userId; $this->userName = $user->name; $this->displayName = $user->display_name ?? ''; $this->userEmail = $user->email; $this->emailVerified = !is_null($user->email_verified_at); $this->showEditModal = true; } public function saveUser(): void { if (!$this->editUserId) { return; } $user = User::with('roles')->findOrFail($this->editUserId); // Prüfe ob User eine Rolle hat, die display_name erfordert $requiresDisplayName = $user->roles->whereIn('name', ['Broker', 'Retailer', 'Manufacturer'])->isNotEmpty(); $rules = [ 'userName' => 'required|string|max:255', 'displayName' => $requiresDisplayName ? 'required|string|max:255' : 'nullable|string|max:255', 'userEmail' => 'required|email|max:255|unique:users,email,' . $this->editUserId, ]; $messages = [ 'displayName.required' => __('Name für die Zuordnung von Kunden ist für Makler, Händler und Hersteller erforderlich.'), ]; $this->validate($rules, $messages); $user->update([ 'name' => $this->userName, 'display_name' => $this->displayName ?: null, 'email' => $this->userEmail, 'email_verified_at' => $this->emailVerified ? ($user->email_verified_at ?? now()) : null, ]); $this->showEditModal = false; $this->reset(['editUserId', 'userName', 'displayName', 'userEmail', 'emailVerified']); session()->flash('message', __('User updated successfully!')); } public function closeEditModal(): void { $this->showEditModal = false; $this->reset(['editUserId', 'userName', 'displayName', 'userEmail', 'emailVerified']); } public function openViewModal(int $userId): void { $this->viewUserId = $userId; $this->showViewModal = true; } public function closeViewModal(): void { $this->showViewModal = false; $this->viewUserId = null; } public function deleteUser(int $userId): void { $user = User::findOrFail($userId); // Prüfe auf Verknüpfungen if ($user->hasDependencies()) { // Anonymisiere statt zu löschen $user->anonymize(); session()->flash('message', __('User wurde anonymisiert, da Verknüpfungen existieren.')); } else { // Soft Delete $user->delete(); session()->flash('message', __('User deleted successfully!')); } // Schließe ggf. offene Modals $this->showRoleModal = false; $this->showEditModal = false; $this->reset(['selectedUserId', 'editUserId']); } public function loginAsUser(int $userId): void { $currentUser = Auth::user(); $targetUser = User::findOrFail($userId); // Speichere den aktuellen Admin-User in der Session session(['impersonate_from' => $currentUser->id]); // Logge als Ziel-User ein Auth::login($targetUser); // Weiterleitung zum Dashboard $this->redirect(route('dashboard'), navigate: false); } }; ?>