166 lines
6.4 KiB
PHP
166 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\AdResource\Pages;
|
|
use App\Models\Ad;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Components\DateTimePicker;
|
|
use Filament\Forms\Components\FileUpload;
|
|
use Filament\Forms\Components\Section;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Forms\Components\Toggle;
|
|
use Filament\Forms\Form;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Columns\IconColumn;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Table;
|
|
|
|
class AdResource extends Resource
|
|
{
|
|
protected static ?string $model = Ad::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-megaphone';
|
|
protected static ?string $navigationLabel = '广告';
|
|
protected static ?string $navigationGroup = '运营管理';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Section::make('基础信息')
|
|
->schema([
|
|
Select::make('ad_slot_id')
|
|
->label('投放位')
|
|
->relationship('adSlot', 'name')
|
|
->searchable()
|
|
->preload()
|
|
->required(),
|
|
TextInput::make('title')
|
|
->label('标题')
|
|
->required()
|
|
->maxLength(255),
|
|
FileUpload::make('image')
|
|
->label('图片')
|
|
->disk('public')
|
|
->directory('ads')
|
|
->image()
|
|
->imagePreviewHeight('120')
|
|
->nullable(),
|
|
Textarea::make('description')
|
|
->label('描述')
|
|
->rows(3)
|
|
->nullable(),
|
|
])->columns(2),
|
|
Section::make('链接与关联')
|
|
->schema([
|
|
Select::make('product_id')
|
|
->label('关联产品')
|
|
->relationship('product', 'name')
|
|
->searchable()
|
|
->preload()
|
|
->nullable()
|
|
->rules(['required_without:link_url']),
|
|
TextInput::make('link_url')
|
|
->label('外部链接')
|
|
->helperText('关联产品与外部链接二选一')
|
|
->maxLength(255)
|
|
->url()
|
|
->nullable()
|
|
->rules(['required_without:product_id']),
|
|
Select::make('link_target')
|
|
->label('打开方式')
|
|
->options([
|
|
'_blank' => '新窗口',
|
|
'_self' => '当前窗口',
|
|
])
|
|
->default('_blank'),
|
|
])->columns(2),
|
|
Section::make('投放与状态')
|
|
->schema([
|
|
DateTimePicker::make('starts_at')
|
|
->label('开始时间')
|
|
->nullable(),
|
|
DateTimePicker::make('ends_at')
|
|
->label('结束时间')
|
|
->nullable(),
|
|
Toggle::make('is_active')
|
|
->label('启用')
|
|
->default(true),
|
|
TextInput::make('sort')
|
|
->label('排序')
|
|
->numeric()
|
|
->default(0),
|
|
])->columns(2),
|
|
Section::make('统计')
|
|
->schema([
|
|
TextInput::make('view_count')
|
|
->label('展示量')
|
|
->numeric()
|
|
->disabled()
|
|
->dehydrated(false),
|
|
TextInput::make('click_count')
|
|
->label('点击量')
|
|
->numeric()
|
|
->disabled()
|
|
->dehydrated(false),
|
|
])->columns(2),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->reorderable('sort')
|
|
->defaultSort('sort')
|
|
->columns([
|
|
TextColumn::make('adSlot.name')->label('投放位')->sortable(),
|
|
TextColumn::make('title')->label('标题')->searchable(),
|
|
IconColumn::make('is_active')->label('启用')->boolean(),
|
|
TextColumn::make('sort')->label('排序')->sortable(),
|
|
TextColumn::make('view_count')->label('展示')->sortable(),
|
|
TextColumn::make('click_count')->label('点击')->sortable(),
|
|
TextColumn::make('starts_at')->label('开始时间')->dateTime(),
|
|
TextColumn::make('ends_at')->label('结束时间')->dateTime(),
|
|
TextColumn::make('created_at')->label('创建时间')->dateTime(),
|
|
])
|
|
->filters([
|
|
Tables\Filters\SelectFilter::make('ad_slot_id')
|
|
->label('投放位')
|
|
->relationship('adSlot', 'name'),
|
|
Tables\Filters\TernaryFilter::make('is_active')
|
|
->label('启用'),
|
|
])
|
|
->actions([
|
|
Tables\Actions\EditAction::make(),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\BulkActionGroup::make([
|
|
Tables\Actions\DeleteBulkAction::make(),
|
|
]),
|
|
])
|
|
->emptyStateActions([
|
|
Tables\Actions\CreateAction::make(),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListAds::route('/'),
|
|
'create' => Pages\CreateAd::route('/create'),
|
|
'edit' => Pages\EditAd::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|