components([ Section::make('Profile')->columns(2)->schema([ TextInput::make('name') ->required() ->maxLength(255), TextInput::make('email') ->email() ->required() ->maxLength(255), TextInput::make('postcode') ->maxLength(8), Select::make('preferred_fuel_type') ->label('Preferred fuel type') ->options(collect(FuelType::cases()) ->mapWithKeys(fn (FuelType $t) => [$t->value => $t->label()]) ->all()) ->required(), ]), Section::make('Access')->columns(2)->schema([ Toggle::make('is_admin') ->label('Admin') ->helperText('Grants access to this admin panel.'), DateTimePicker::make('email_verified_at') ->label('Email verified at'), ]), Section::make('Subscription')->columns(2)->schema([ Select::make('tier') ->label('Tier') ->options([ PlanTier::Free->value => 'Free', PlanTier::Basic->value => 'Basic', PlanTier::Plus->value => 'Plus', PlanTier::Pro->value => 'Pro', ]) ->required() ->live() ->dehydrated(false) ->afterStateHydrated(fn (Select $component, ?User $record) => $component ->state($record ? PlanFeatures::for($record)->tier() : PlanTier::Free->value)), Select::make('cadence') ->label('Billing Cadence') ->options([ 'monthly' => 'Monthly', 'annual' => 'Annual', ]) ->default('monthly') ->dehydrated(false) ->visible(fn (callable $get): bool => ($get('tier') ?? '') !== PlanTier::Free->value) ->helperText('Only applies when assigning a paid tier. Real Stripe subscriptions are not modified.'), ]), Section::make('Security')->columns(2)->schema([ DateTimePicker::make('two_factor_confirmed_at') ->label('2FA confirmed at') ->disabled(), TextInput::make('password') ->label('Set new password') ->password() ->revealable() ->minLength(8) ->dehydrated(fn (?string $state): bool => filled($state)) ->dehydrateStateUsing(fn (string $state): string => bcrypt($state)) ->helperText('Leave blank to keep current password.') ->afterStateHydrated(fn (TextInput $component) => $component->state(null)), ]), Section::make('Billing')->columns(3)->schema([ TextInput::make('stripe_id') ->label('Stripe customer ID') ->disabled(), TextInput::make('pm_type') ->label('Payment method') ->disabled(), TextInput::make('pm_last_four') ->label('Card last 4') ->disabled(), ]), Section::make('Timestamps')->columns(2)->schema([ DateTimePicker::make('created_at')->disabled(), DateTimePicker::make('updated_at')->disabled(), ]), ]); } public static function table(Table $table): Table { return $table ->columns([ TextColumn::make('name')->searchable()->sortable(), TextColumn::make('email')->searchable()->sortable(), TextColumn::make('postcode')->placeholder('—'), TextColumn::make('tier') ->label('Tier') ->state(fn (User $record): string => PlanFeatures::for($record)->tier()) ->badge() ->colors([ 'gray' => 'free', 'primary' => 'basic', 'warning' => 'plus', 'success' => 'pro', ]), IconColumn::make('is_admin') ->label('Admin') ->boolean(), TextColumn::make('created_at') ->dateTime('d M Y') ->sortable(), ]) ->defaultSort('created_at', 'desc') ->filters([ TernaryFilter::make('is_admin') ->label('Admins only'), ]) ->recordActions([ EditAction::make(), DeleteAction::make(), ]); } /** * Cancel any existing admin-granted subscription, then (if a paid tier * was requested) insert a fresh synthetic active subscription row. */ public static function applyTier(User $user, string $tier, string $cadence): void { $hasRealStripeSubscription = $user->subscriptions() ->where('stripe_id', 'not like', 'admin_%') ->whereIn('stripe_status', ['active', 'trialing', 'past_due']) ->exists(); if ($hasRealStripeSubscription) { throw new \RuntimeException( "User {$user->email} has an active Stripe subscription — modify it through the Stripe dashboard, not the admin panel." ); } $user->subscriptions()->where('stripe_id', 'like', 'admin_%')->delete(); if ($tier === PlanTier::Free->value) { self::bustPlanCache($user); return; } $priceId = config("services.stripe.prices.{$tier}.{$cadence}") ?: "price_admin_{$tier}_{$cadence}"; $planColumn = $cadence === 'annual' ? 'stripe_price_id_annual' : 'stripe_price_id_monthly'; $plan = Plan::where('name', $tier)->first(); if ($plan && empty($plan->{$planColumn})) { $plan->update([$planColumn => $priceId]); } $user->subscriptions()->create([ 'type' => 'default', 'stripe_id' => 'admin_'.Str::uuid(), 'stripe_status' => 'active', 'stripe_price' => $priceId, 'quantity' => 1, ]); self::bustPlanCache($user); } protected static function bustPlanCache(User $user): void { if (Cache::supportsTags()) { Cache::tags(['plans'])->flush(); } else { Cache::forget("plan_for_user_{$user->id}"); } } public static function getPages(): array { return [ 'index' => ListUsers::route('/'), 'edit' => EditUser::route('/{record}/edit'), ]; } }