orderBy('name') ->get(); } public function mount(): void { // Setze default auf die erste einladbare Rolle $firstRole = $this->getPartnerRoles()->first(); $this->roleId = $firstRole?->id; } public function sendInvitation(): void { $availableRoleIds = $this->getPartnerRoles()->pluck('id')->toArray(); $this->validate([ 'companyName' => 'required|string|max:255', 'contactFirstName' => 'nullable|string|max:255', 'contactLastName' => 'nullable|string|max:255', 'roleId' => 'required|exists:roles,id|in:' . implode(',', $availableRoleIds), 'email' => 'required|email|max:255|unique:users,email', ], [ 'companyName.required' => __('Bitte geben Sie einen Firmennamen ein.'), 'companyName.max' => __('Der Firmenname darf maximal 255 Zeichen lang sein.'), 'contactFirstName.max' => __('Der Vorname darf maximal 255 Zeichen lang sein.'), 'contactLastName.max' => __('Der Nachname darf maximal 255 Zeichen lang sein.'), 'roleId.required' => __('Bitte wählen Sie einen Partner-Typ aus.'), 'roleId.exists' => __('Der gewählte Partner-Typ ist ungültig.'), 'email.required' => __('Bitte geben Sie eine E-Mail-Adresse ein.'), 'email.email' => __('Bitte geben Sie eine gültige E-Mail-Adresse ein.'), 'email.max' => __('Die E-Mail-Adresse darf maximal 255 Zeichen lang sein.'), 'email.unique' => __('Diese E-Mail-Adresse ist bereits als Benutzer registriert.'), ]); // Prüfe ob bereits eine aktive Einladung existiert $existingInvitation = PartnerInvitation::where('email', $this->email) ->pending() ->first(); if ($existingInvitation) { $this->addError('email', __('Es existiert bereits eine aktive Einladung für diese E-Mail-Adresse.')); return; } // Erstelle Einladung $invitation = PartnerInvitation::create([ 'company_name' => $this->companyName, 'contact_first_name' => $this->contactFirstName ?: null, 'contact_last_name' => $this->contactLastName ?: null, 'role_id' => $this->roleId, 'email' => $this->email, 'token' => PartnerInvitation::generateToken(), 'status' => 'pending', 'expires_at' => now()->addDays(7), // 7 Tage gültig 'invited_by' => auth()->id(), ]); // Generiere Einladungs-URL $invitationUrl = route('partner.invitation.accept', ['token' => $invitation->token]); // Sende E-Mail try { Mail::to($this->email)->send(new PartnerInvitationMail($invitation, $invitationUrl)); $this->lastInvitation = $invitation; $this->showSuccessMessage = true; // Reset Form $this->reset(['companyName', 'contactFirstName', 'contactLastName', 'roleId', 'email']); // Setze default Rolle wieder $firstRole = $this->getPartnerRoles()->first(); $this->roleId = $firstRole?->id; session()->flash('message', __('Einladung erfolgreich versendet!')); } catch (\Exception $e) { $this->addError('email', __('Fehler beim Versenden der E-Mail: ') . $e->getMessage()); } } public function with(): array { return [ 'partnerRoles' => $this->getPartnerRoles(), 'recentInvitations' => PartnerInvitation::with(['invitedBy', 'role']) ->latest() ->take(10) ->get(), 'pendingCount' => PartnerInvitation::pending()->count(), 'acceptedCount' => PartnerInvitation::where('status', 'accepted')->count(), 'expiredCount' => PartnerInvitation::expired()->count(), ]; } }; ?>