168 lines
7.0 KiB
PHP
168 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\TenantResource\Pages;
|
|
use App\Models\Tenant;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
use Filament\Actions;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Form;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Toggle;
|
|
use Filament\Forms\Components\KeyValue;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\DateTimePicker;
|
|
use Filament\Tables\Columns\IconColumn;
|
|
use App\Filament\Resources\TenantResource\RelationManagers\PurchasesRelationManager;
|
|
use Filament\Resources\RelationManagers\RelationGroup;
|
|
use UnitEnum;
|
|
use BackedEnum;
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
class TenantResource extends Resource
|
|
{
|
|
protected static ?string $model = Tenant::class;
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-building-office';
|
|
protected static UnitEnum|string|null $navigationGroup = null;
|
|
|
|
public static function getNavigationGroup(): UnitEnum|string|null
|
|
{
|
|
return __('admin.nav.platform');
|
|
}
|
|
protected static ?int $navigationSort = 10;
|
|
|
|
public static function form(Schema $form): Schema
|
|
{
|
|
return $form->schema([
|
|
TextInput::make('name')
|
|
->label(__('admin.tenants.fields.name'))
|
|
->required()
|
|
->maxLength(255),
|
|
TextInput::make('slug')
|
|
->label(__('admin.tenants.fields.slug'))
|
|
->required()
|
|
->unique(ignoreRecord: true)
|
|
->maxLength(255),
|
|
TextInput::make('contact_email')
|
|
->label(__('admin.tenants.fields.contact_email'))
|
|
->email()
|
|
->required()
|
|
->maxLength(255),
|
|
TextInput::make('event_credits_balance')
|
|
->label(__('admin.tenants.fields.event_credits_balance'))
|
|
->numeric()
|
|
->default(0),
|
|
Select::make('subscription_tier')
|
|
->label(__('admin.tenants.fields.subscription_tier'))
|
|
->options([
|
|
'free' => 'Free',
|
|
'starter' => 'Starter (€4.99/mo)',
|
|
'pro' => 'Pro (€14.99/mo)',
|
|
'agency' => 'Agency (€19.99/mo)',
|
|
'lifetime' => 'Lifetime (€49.99)'
|
|
])
|
|
->default('free'),
|
|
DateTimePicker::make('subscription_expires_at')
|
|
->label(__('admin.tenants.fields.subscription_expires_at')),
|
|
TextInput::make('total_revenue')
|
|
->label(__('admin.tenants.fields.total_revenue'))
|
|
->prefix('€')
|
|
->numeric()
|
|
->step(0.01)
|
|
->readOnly(),
|
|
Toggle::make('is_active')
|
|
->label(__('admin.tenants.fields.is_active'))
|
|
->default(true),
|
|
Toggle::make('is_suspended')
|
|
->label(__('admin.tenants.fields.is_suspended'))
|
|
->default(false),
|
|
KeyValue::make('features')
|
|
->label(__('admin.tenants.fields.features'))
|
|
->keyLabel(__('admin.common.key'))
|
|
->valueLabel(__('admin.common.value')),
|
|
])->columns(2);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('id')->sortable(),
|
|
Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
|
|
Tables\Columns\TextColumn::make('slug')->searchable(),
|
|
Tables\Columns\TextColumn::make('contact_email'),
|
|
Tables\Columns\TextColumn::make('event_credits_balance')
|
|
->label(__('admin.common.credits'))
|
|
->badge()
|
|
->color(fn ($state) => $state < 5 ? 'warning' : 'success'),
|
|
Tables\Columns\TextColumn::make('subscription_tier')
|
|
->badge()
|
|
->color(fn (string $state): string => match($state) {
|
|
'free' => 'gray',
|
|
'starter' => 'info',
|
|
'pro' => 'success',
|
|
'agency' => 'warning',
|
|
'lifetime' => 'danger',
|
|
}),
|
|
Tables\Columns\TextColumn::make('total_revenue')
|
|
->money('EUR')
|
|
->sortable(),
|
|
Tables\Columns\IconColumn::make('is_active')
|
|
->boolean()
|
|
->color(fn (bool $state): string => $state ? 'success' : 'danger'),
|
|
Tables\Columns\TextColumn::make('last_activity_at')->since()->label(__('admin.common.last_activity')),
|
|
Tables\Columns\TextColumn::make('created_at')->dateTime()->toggleable(isToggledHiddenByDefault: true),
|
|
])
|
|
->filters([])
|
|
->actions([
|
|
Actions\EditAction::make(),
|
|
Actions\Action::make('add_credits')
|
|
->label('Credits hinzufügen')
|
|
->icon('heroicon-o-plus')
|
|
->form([
|
|
Forms\Components\TextInput::make('credits')->numeric()->required()->minValue(1),
|
|
Forms\Components\Textarea::make('reason')->label('Grund')->rows(3),
|
|
])
|
|
->action(function (Tenant $record, array $data) {
|
|
$record->increment('event_credits_balance', $data['credits']);
|
|
\App\Models\EventPurchase::create([
|
|
'tenant_id' => $record->id,
|
|
'package_id' => 'manual_adjustment',
|
|
'credits_added' => $data['credits'],
|
|
'price' => 0,
|
|
'platform' => 'manual',
|
|
'transaction_id' => null,
|
|
'reason' => $data['reason'],
|
|
]);
|
|
}),
|
|
Actions\Action::make('suspend')
|
|
->label('Suspendieren')
|
|
->color('danger')
|
|
->requiresConfirmation()
|
|
->action(fn (Tenant $record) => $record->update(['is_suspended' => true])),
|
|
Actions\Action::make('export')
|
|
->label('Daten exportieren')
|
|
->icon('heroicon-o-arrow-down-tray')
|
|
->url(fn (Tenant $record) => Route::has('admin.tenants.export') ? route('admin.tenants.export', $record) : null)
|
|
->visible(fn () => Route::has('admin.tenants.export'))
|
|
->openUrlInNewTab(),
|
|
])
|
|
->bulkActions([
|
|
Actions\DeleteBulkAction::make(),
|
|
]);
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListTenants::route('/'),
|
|
'view' => Pages\ViewTenant::route('/{record}'),
|
|
'edit' => Pages\EditTenant::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|