| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace App\Filament\Resources\TextbookResource\Tables;
- use App\Filament\Resources\TextbookResource;
- use Filament\Actions\EditAction;
- use Filament\Actions\Action;
- use Filament\Tables;
- use Filament\Tables\Columns\BadgeColumn;
- use Filament\Tables\Columns\TextColumn;
- use Filament\Tables\Filters\SelectFilter;
- use Filament\Tables\Table;
- use Illuminate\Database\Eloquent\Model;
- class TextbookTable
- {
- public static function make(Table $table): Table
- {
- // 直接返回配置好的表格,不使用query()
- return $table
- ->columns([
- TextColumn::make('id')
- ->label('ID')
- ->sortable(),
- TextColumn::make('series.name')
- ->label('系列')
- ->searchable(),
- TextColumn::make('official_title')
- ->label('官方书名')
- ->searchable()
- ->wrap(),
- TextColumn::make('stage')
- ->label('学段')
- ->formatStateUsing(function ($state): string {
- return match ($state) {
- 'primary' => '小学',
- 'junior' => '初中',
- 'senior' => '高中',
- default => $state,
- };
- })
- ->badge()
- ->color('info'),
- TextColumn::make('grade')
- ->label('年级')
- ->formatStateUsing(function ($state): string {
- return $state ? "{$state}年级" : '-';
- }),
- TextColumn::make('semester')
- ->label('学期')
- ->formatStateUsing(function ($state): string {
- return match ($state) {
- 1 => '上学期',
- 2 => '下学期',
- default => '-',
- };
- })
- ->badge()
- ->color('success'),
- TextColumn::make('naming_scheme')
- ->label('体系')
- ->formatStateUsing(function ($state): string {
- return match ($state) {
- 'new' => '新体系',
- 'old' => '旧体系',
- default => $state,
- };
- })
- ->badge(),
- TextColumn::make('status')
- ->label('状态')
- ->formatStateUsing(function ($state): string {
- return match ($state) {
- 'draft' => '草稿',
- 'published' => '已发布',
- 'archived' => '已归档',
- default => $state,
- };
- })
- ->badge()
- ->color(function ($state): string {
- return match ($state) {
- 'draft' => 'gray',
- 'published' => 'success',
- 'archived' => 'danger',
- default => 'gray',
- };
- }),
- TextColumn::make('created_at')
- ->label('创建时间')
- ->dateTime()
- ->sortable()
- ->toggleable(isToggledHiddenByDefault: true),
- TextColumn::make('updated_at')
- ->label('更新时间')
- ->dateTime()
- ->sortable()
- ->toggleable(isToggledHiddenByDefault: true),
- ])
- ->filters([
- SelectFilter::make('stage')
- ->label('学段')
- ->options([
- 'primary' => '小学',
- 'junior' => '初中',
- 'senior' => '高中',
- ]),
- SelectFilter::make('status')
- ->label('状态')
- ->options([
- 'draft' => '草稿',
- 'published' => '已发布',
- 'archived' => '已归档',
- ]),
- ])
- ->actions([
- EditAction::make()
- ->label('编辑'),
- Action::make('delete')
- ->label('删除')
- ->color('danger')
- ->icon('heroicon-o-trash')
- ->requiresConfirmation()
- ->modalHeading('删除教材')
- ->modalDescription('确定要删除这个教材吗?此操作无法撤销。')
- ->action(function (Model $record) {
- // 添加调试日志
- \Log::info('Deleting textbook', ['id' => $record->id, 'record' => $record]);
- if (!$record || !$record->id) {
- \Filament\Notifications\Notification::make()
- ->title('错误')
- ->body('无效的教材记录。')
- ->danger()
- ->send();
- return;
- }
- $apiService = app(\App\Services\TextbookApiService::class);
- $deleted = $apiService->deleteTextbook($record->id);
- \Log::info('Delete result', ['deleted' => $deleted]);
- if ($deleted) {
- \Filament\Notifications\Notification::make()
- ->title('成功')
- ->body('教材删除成功。')
- ->success()
- ->send();
- } else {
- \Filament\Notifications\Notification::make()
- ->title('错误')
- ->body('删除失败,请重试。')
- ->danger()
- ->send();
- }
- }),
- Action::make('view_catalog')
- ->label('查看目录')
- ->icon('heroicon-o-list-bullet')
- ->url(fn(Model $record): string =>
- route('filament.admin.resources.textbook-catalogs.index', ['tableFilters[textbook_id][value]' => $record->id])
- ),
- ])
- ->bulkActions([
- \Filament\Actions\BulkActionGroup::make([
- \Filament\Actions\DeleteBulkAction::make()
- ->label('批量删除'),
- ]),
- ])
- ->defaultSort('sort_order')
- ->paginated([10, 25, 50, 100]);
- }
- }
|