Files

158 lines
5.6 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Filament\Resources\Miscellaneous\AlertLogResource;
use App\Models\Miscellaneous\AlertLog;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables\Columns\BadgeColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Cache;
class AlertLogResource extends Resource
{
#[\Override]
protected static ?string $model = AlertLog::class;
#[\Override]
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-bell';
#[\Override]
protected static string|\UnitEnum|null $navigationGroup = 'Commandocentrum';
#[\Override]
protected static ?string $navigationLabel = 'Alert Database';
protected static ?string $title = 'Alert Database';
#[\Override]
protected static ?string $slug = 'monitoring/alerts';
#[\Override]
public static function canCreate(): bool
{
return false;
}
#[\Override]
public static function table(Table $table): Table
{
return $table
->defaultSort('created_at', 'desc')
->columns([
TextColumn::make('type')
->label('Type')
->formatStateUsing(fn (string $state): string => ucfirst(str_replace('_', ' ', $state)))
->searchable(),
BadgeColumn::make('severity')
->label('Severity')
->colors([
'info' => 'info',
'warning' => 'warning',
'error' => 'error',
'danger' => 'critical',
])
->formatStateUsing(fn (string $state): string => ucfirst($state)),
TextColumn::make('message')
->label('Message')
->limit(50)
->tooltip(fn (AlertLog $record): ?string => $record->message),
TextColumn::make('created_at')
->label('Time')
->dateTime('d M Y, H:i:s')
->sortable(),
BadgeColumn::make('is_read')
->label('Status')
->colors([
'secondary' => false,
'success' => true,
])
->formatStateUsing(fn (bool $state): string => $state ? 'Read' : 'Unread'),
BadgeColumn::make('sent_via_email')
->label('Email')
->colors([
'success' => true,
'secondary' => false,
])
->formatStateUsing(fn (bool $state): string => $state ? 'Yes' : 'No'),
BadgeColumn::make('sent_via_discord')
->label('Discord')
->colors([
'success' => true,
'secondary' => false,
])
->formatStateUsing(fn (bool $state): string => $state ? 'Yes' : 'No'),
])
->filters([
SelectFilter::make('severity')
->options([
'info' => 'Info',
'warning' => 'Warning',
'error' => 'Error',
'critical' => 'Critical',
]),
TernaryFilter::make('is_read')
->label('Read Status'),
])
->recordActions([
Action::make('mark_read')
->label('Mark as Read')
->action(fn (AlertLog $record) => $record->markAsRead())
->visible(fn (AlertLog $record) => ! $record->is_read)
->icon('heroicon-o-check'),
Action::make('delete')
->label('Delete')
->action(fn (AlertLog $record) => $record->delete())
->icon('heroicon-o-trash')
->color('danger')
->requiresConfirmation(),
])
->toolbarActions([
Action::make('mark_all_read')
->label('Mark All as Read')
->action(fn () => AlertLog::where('is_read', false)->update(['is_read' => true, 'read_at' => now()]))
->icon('heroicon-o-check-circle'),
Action::make('delete_all')
->label('Delete All Logs')
->action(fn () => AlertLog::truncate())
->icon('heroicon-o-trash')
->color('danger')
->requiresConfirmation(),
Action::make('clear_all_logs')
->label('Leeg Alle Logs (Systeem)')
->icon('heroicon-o-trash')
->color('gray')
->action(function () {
Cache::flush();
AlertLog::truncate();
Notification::make()
->success()
->title('🗑️ Alle Logs Geleegd!')
->body('Alle logs zijn gewist.')
->send();
})
->requiresConfirmation(),
]);
}
#[\Override]
public static function getPages(): array
{
return [
'index' => Pages\ListAlertLogs::route('/'),
];
}
}