Files
2026-05-09 17:32:17 +02:00

261 lines
8.7 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Filament\Pages\Radio;
use App\Models\RadioGiveaway;
use Filament\Actions\Action;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
final class GiveawayManagement extends Page implements HasForms
{
use InteractsWithForms;
#[\Override]
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-gift';
#[\Override]
protected static string|\UnitEnum|null $navigationGroup = 'Radio';
#[\Override]
protected static ?string $navigationLabel = null;
#[\Override]
public static function getNavigationLabel(): string
{
return __('Giveaways');
}
#[\Override]
protected static ?string $title = 'Winacties Beheer';
#[\Override]
protected string $view = 'filament.pages.radio.giveaway-management';
/** @var array<string, mixed> */
public array $data = [];
public RadioGiveaway $currentGiveaway;
public function mount(): void
{
$this->fillForm();
}
protected function fillForm(): void
{
$giveaways = RadioGiveaway::orderBy('created_at', 'desc')->get();
$this->data['giveaways'] = $giveaways->toArray();
$this->data['active_giveaway'] = $giveaways->firstWhere('is_active', true)?->id;
}
public function form(Schema $schema): Schema
{
return $schema
->components([
Section::make('Nieuwe Winactie Aanmaken')
->description('Maak een nieuwe winactie aan voor luisteraars')
->columns(1)
->headerActions([
Action::make('create_giveaway')
->label('Winactie Aanmaken')
->action('createGiveaway')
->color('success'),
])
->schema([
TextInput::make('giveaway.title')
->label('Titel')
->required()
->placeholder('Bijv. Win VIP voor 1 maand!'),
Textarea::make('giveaway.description')
->label('Beschrijving')
->rows(3)
->placeholder('Beschrijf de winactie en de prijs...'),
TextInput::make('giveaway.prize')
->label('Prijs')
->required()
->placeholder('Bijv. VIP Lifetime'),
TextInput::make('giveaway.prize_value')
->label('Prijs Waarde (EUR)')
->numeric()
->placeholder('Bijv. 25.00'),
DateTimePicker::make('giveaway.starts_at')
->label('Start Datum/Tijd')
->required()
->default(now()),
DateTimePicker::make('giveaway.ends_at')
->label('Eind Datum/Tijd')
->required()
->default(now()->addDays(7)),
]),
Section::make('Actieve Winacties')
->description('Beheer lopende winacties')
->columns(1)
->schema([
Select::make('selected_giveaway')
->label('Selecteer Winactie')
->options(RadioGiveaway::where('is_active', true)->pluck('title', 'id'))
->placeholder('Kies een winactie...')
->live()
->afterStateUpdated(function ($state) {
if ($state) {
$this->currentGiveaway = RadioGiveaway::find($state);
}
}),
]),
Section::make('Acties')
->description('Winactie beheer')
->columns(2)
->schema([
Action::make('export_participants')
->label('Deelnemers Exporteren')
->action('exportParticipants')
->color('info'),
Action::make('pick_winner')
->label('Winnaar Trekken')
->action('pickWinner')
->color('warning')
->requiresConfirmation(),
Action::make('end_giveaway')
->label('Winactie Beëindigen')
->action('endGiveaway')
->color('danger')
->requiresConfirmation(),
]),
Section::make('Afgelopen Winacties')
->description('Bekijk resultaten van afgelopen winacties')
->columns(1)
->schema([
Select::make('ended_giveaway')
->label('Selecteer Afgelopen Winactie')
->options(RadioGiveaway::where('is_ended', true)->pluck('title', 'id'))
->placeholder('Kies een winactie...'),
]),
])
->statePath('data');
}
public function createGiveaway(): void
{
$data = $this->data['giveaway'] ?? [];
$giveaway = RadioGiveaway::create([
'title' => $data['title'],
'description' => $data['description'] ?? '',
'prize' => $data['prize'],
'prize_value' => $data['prize_value'] ?? 0,
'starts_at' => $data['starts_at'] ?? now(),
'ends_at' => $data['ends_at'] ?? now()->addDays(7),
'is_active' => true,
'is_ended' => false,
'winner_announced' => false,
]);
$this->fillForm();
Notification::make()
->success()
->title('Winactie Aangemaakt')
->body("Winactie '{$giveaway->title}' is succesvol aangemaakt!")
->send();
}
public function pickWinner(): void
{
$giveawayId = $this->data['selected_giveaway'] ?? null;
if (! $giveawayId) {
Notification::make()
->warning()
->title('Geen Winactie Geselecteerd')
->body('Selecteer eerst een winactie.')
->send();
return;
}
$giveaway = RadioGiveaway::find($giveawayId);
if (! $giveaway) {
Notification::make()
->danger()
->title('Winactie Niet Gevonden')
->body('De geselecteerde winactie bestaat niet meer.')
->send();
return;
}
$participants = $giveaway->participants()->with('user')->get();
if ($participants->isEmpty()) {
Notification::make()
->warning()
->title('Geen Deelnemers')
->body('Er zijn nog geen deelnemers voor deze winactie.')
->send();
return;
}
$winner = $participants->random();
$giveaway->announceWinner($winner->user);
Notification::make()
->success()
->title('Winnaar Gekozen!')
->body("Gefeliciteerd {$winner->user->username}! Je hebt {$giveaway->prize} gewonnen!")
->send();
$this->fillForm();
}
public function endGiveaway(): void
{
$giveawayId = $this->data['selected_giveaway'] ?? null;
if (! $giveawayId) {
Notification::make()
->warning()
->title('Geen Winactie Geselecteerd')
->body('Selecteer eerst een winactie.')
->send();
return;
}
$giveaway = RadioGiveaway::find($giveawayId);
$giveaway->update(['is_ended' => true, 'is_active' => false]);
$this->fillForm();
Notification::make()
->success()
->title('Winactie Beëindigd')
->body("Winactie '{$giveaway->title}' is beëindigd.")
->send();
}
public function exportParticipants(): void
{
Notification::make()
->info()
->title('Export Gestart')
->body('Deelnemerslijst wordt gedownload...')
->send();
}
}