schema([ Forms\Components\Section::make('基本資訊') ->schema([ Forms\Components\TextInput::make('title') ->label('標題') ->required() ->maxLength(500) ->live(onBlur: true) ->afterStateUpdated(function (string $operation, $state, Forms\Set $set) { if ($operation === 'create') { $set('slug', Str::slug($state)); } }) ->columnSpanFull(), Forms\Components\TextInput::make('slug') ->label('Slug (URL 友善)') ->required() ->unique(Post::class, 'slug', ignoreRecord: true) ->maxLength(500) ->helperText('URL 友善的標識符,會自動從標題生成') ->columnSpanFull(), Forms\Components\Select::make('post_type') ->label('文章類型') ->required() ->options([ 'news' => '新聞', 'event' => '活動', ]) ->default('news') ->live() ->reactive(), Forms\Components\Select::make('status') ->label('狀態') ->required() ->options([ 'draft' => '草稿', 'publish' => '已發布', ]) ->default('draft'), Forms\Components\DateTimePicker::make('published_at') ->label('發布時間') ->default(now()) ->required(), Forms\Components\DatePicker::make('date') ->label('日期') ->helperText('文章或活動日期') ->required(), ]) ->columns(2), Forms\Components\Section::make('內容') ->schema([ Forms\Components\Textarea::make('excerpt') ->label('摘要') ->rows(3) ->helperText('文章摘要(選填)') ->columnSpanFull(), Forms\Components\RichEditor::make('content') ->label('完整內容') ->helperText('文章完整內容(HTML)') ->columnSpanFull(), ]), Forms\Components\Section::make('圖片') ->schema([ Forms\Components\FileUpload::make('image_url') ->label('文章圖片') ->image() ->directory('posts') ->helperText('上傳文章配圖') ->columnSpanFull(), Forms\Components\Toggle::make('featured_image') ->label('特色圖片') ->default(false) ->helperText('標記為特色圖片'), ]) ->columns(2), // 活動專屬欄位 (僅當 post_type = 'event' 時顯示) Forms\Components\Section::make('活動資訊') ->schema([ Forms\Components\TextInput::make('time') ->label('活動時間') ->maxLength(50) ->helperText('例如: 0830-1630') ->placeholder('0830-1630'), Forms\Components\TextInput::make('address') ->label('活動地址') ->maxLength(500) ->helperText('活動舉辦地點') ->columnSpanFull(), Forms\Components\RichEditor::make('description') ->label('活動說明') ->helperText('活動詳細說明(HTML)') ->columnSpanFull(), Forms\Components\TextInput::make('link') ->label('活動連結') ->url() ->maxLength(500) ->helperText('活動報名或詳細資訊連結') ->columnSpanFull(), ]) ->columns(2) ->visible(fn (Forms\Get $get): bool => $get('post_type') === 'event'), Forms\Components\Section::make('分類') ->schema([ Forms\Components\CheckboxList::make('categories') ->label('選擇分類') ->relationship('categories', 'name') ->options(Category::all()->pluck('name', 'id')) ->helperText('文章可以屬於多個分類') ->columnSpanFull(), ]), ]); } public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('title') ->label('標題') ->searchable() ->sortable() ->limit(50), Tables\Columns\BadgeColumn::make('post_type') ->label('類型') ->formatStateUsing(fn (string $state): string => match ($state) { 'news' => '新聞', 'event' => '活動', default => $state, }) ->colors([ 'info' => 'news', 'warning' => 'event', ]), Tables\Columns\BadgeColumn::make('status') ->label('狀態') ->formatStateUsing(fn (string $state): string => match ($state) { 'draft' => '草稿', 'publish' => '已發布', default => $state, }) ->colors([ 'warning' => 'draft', 'success' => 'publish', ]), Tables\Columns\TextColumn::make('published_at') ->label('發布時間') ->dateTime('Y-m-d H:i') ->sortable(), Tables\Columns\TextColumn::make('date') ->label('日期') ->date('Y-m-d') ->sortable(), Tables\Columns\TextColumn::make('categories.name') ->label('分類') ->badge() ->separator(',') ->color('success'), Tables\Columns\TextColumn::make('created_at') ->label('建立時間') ->dateTime('Y-m-d H:i') ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('updated_at') ->label('更新時間') ->dateTime('Y-m-d H:i') ->sortable() ->toggleable(isToggledHiddenByDefault: true), ]) ->filters([ Tables\Filters\SelectFilter::make('post_type') ->label('文章類型') ->options([ 'news' => '新聞', 'event' => '活動', ]), Tables\Filters\SelectFilter::make('status') ->label('狀態') ->options([ 'draft' => '草稿', 'publish' => '已發布', ]), Tables\Filters\SelectFilter::make('categories') ->label('分類') ->relationship('categories', 'name') ->multiple(), ]) ->actions([ Tables\Actions\EditAction::make(), Tables\Actions\DeleteAction::make(), ]) ->bulkActions([ Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), ]) ->defaultSort('published_at', 'desc'); } public static function getRelations(): array { return [ // ]; } public static function getPages(): array { return [ 'index' => Pages\ListPosts::route('/'), 'create' => Pages\CreatePost::route('/create'), 'edit' => Pages\EditPost::route('/{record}/edit'), ]; } }