query( Appeal::query() ->where('status', 'confirmed') ->where('is_accepting_donations', true) ->where(function ($q) { $q->where(function ($q2) { // Needs outreach: £0 raised, 7+ days old $q2->where('amount_raised', 0) ->where('created_at', '<', now()->subDays(7)) ->where('created_at', '>', now()->subDays(90)); // Not ancient }) ->orWhere(function ($q2) { // Almost there: 80%+ of target $q2->where('amount_raised', '>', 0) ->whereRaw('amount_raised >= amount_to_raise * 0.8') ->whereRaw('amount_raised < amount_to_raise'); }) ->orWhere(function ($q2) { // New this week $q2->where('created_at', '>=', now()->subDays(7)); }); }) ->with('user') ->orderByRaw(" CASE WHEN amount_raised > 0 AND amount_raised >= amount_to_raise * 0.8 AND amount_raised < amount_to_raise THEN 1 WHEN created_at >= NOW() - INTERVAL '7 days' THEN 2 WHEN amount_raised = 0 AND created_at < NOW() - INTERVAL '7 days' THEN 3 ELSE 4 END ASC ") ) ->columns([ TextColumn::make('priority') ->label('') ->getStateUsing(function (Appeal $a) { $raised = $a->amount_raised; $target = $a->amount_to_raise; $age = $a->created_at?->diffInDays(now()) ?? 0; if ($raised > 0 && $raised >= $target * 0.8 && $raised < $target) return '🟡 Almost there'; if ($age <= 7) return '🆕 New'; if ($raised == 0) return '🔴 Needs help'; return '—'; }) ->badge() ->color(function (Appeal $a) { $raised = $a->amount_raised; $target = $a->amount_to_raise; $age = $a->created_at?->diffInDays(now()) ?? 0; if ($raised > 0 && $raised >= $target * 0.8) return 'warning'; if ($age <= 7) return 'info'; return 'danger'; }), TextColumn::make('name') ->label('Fundraiser') ->limit(35) ->weight('bold') ->description(fn (Appeal $a) => $a->user?->name ? 'by ' . $a->user->name : ''), TextColumn::make('progress') ->label('Progress') ->getStateUsing(function (Appeal $a) { $raised = $a->amount_raised / 100; $target = $a->amount_to_raise / 100; $pct = $target > 0 ? round($raised / $target * 100) : 0; return '£' . number_format($raised, 0) . ' / £' . number_format($target, 0) . " ({$pct}%)"; }), TextColumn::make('created_at') ->label('Created') ->since(), ]) ->actions([ Action::make('view') ->label('Open') ->icon('heroicon-o-arrow-right') ->url(fn (Appeal $a) => AppealResource::getUrl('edit', ['record' => $a])) ->color('gray'), Action::make('email') ->label('Email Owner') ->icon('heroicon-o-envelope') ->url(fn (Appeal $a) => $a->user?->email ? 'mailto:' . $a->user->email : null) ->visible(fn (Appeal $a) => (bool) $a->user?->email) ->openUrlInNewTab() ->color('info'), ]) ->paginated([5, 10]) ->emptyStateHeading('All fundraisers are doing well!') ->emptyStateDescription('No fundraisers need attention right now.') ->emptyStateIcon('heroicon-o-face-smile'); } }