TextbookTable.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Filament\Resources\TextbookResource\Tables;
  3. use App\Filament\Resources\TextbookResource;
  4. use Filament\Actions\EditAction;
  5. use Filament\Actions\Action;
  6. use Filament\Tables;
  7. use Filament\Tables\Columns\BadgeColumn;
  8. use Filament\Tables\Columns\TextColumn;
  9. use Filament\Tables\Filters\SelectFilter;
  10. use Filament\Tables\Table;
  11. use Illuminate\Database\Eloquent\Model;
  12. class TextbookTable
  13. {
  14. public static function make(Table $table): Table
  15. {
  16. // 直接返回配置好的表格,不使用query()
  17. return $table
  18. ->columns([
  19. TextColumn::make('id')
  20. ->label('ID')
  21. ->sortable(),
  22. TextColumn::make('series.name')
  23. ->label('系列')
  24. ->searchable(),
  25. TextColumn::make('official_title')
  26. ->label('官方书名')
  27. ->searchable()
  28. ->wrap(),
  29. TextColumn::make('stage')
  30. ->label('学段')
  31. ->formatStateUsing(function ($state): string {
  32. return match ($state) {
  33. 'primary' => '小学',
  34. 'junior' => '初中',
  35. 'senior' => '高中',
  36. default => $state,
  37. };
  38. })
  39. ->badge()
  40. ->color('info'),
  41. TextColumn::make('grade')
  42. ->label('年级')
  43. ->formatStateUsing(function ($state): string {
  44. return $state ? "{$state}年级" : '-';
  45. }),
  46. TextColumn::make('semester')
  47. ->label('学期')
  48. ->formatStateUsing(function ($state): string {
  49. return match ($state) {
  50. 1 => '上学期',
  51. 2 => '下学期',
  52. default => '-',
  53. };
  54. })
  55. ->badge()
  56. ->color('success'),
  57. TextColumn::make('naming_scheme')
  58. ->label('体系')
  59. ->formatStateUsing(function ($state): string {
  60. return match ($state) {
  61. 'new' => '新体系',
  62. 'old' => '旧体系',
  63. default => $state,
  64. };
  65. })
  66. ->badge(),
  67. TextColumn::make('status')
  68. ->label('状态')
  69. ->formatStateUsing(function ($state): string {
  70. return match ($state) {
  71. 'draft' => '草稿',
  72. 'published' => '已发布',
  73. 'archived' => '已归档',
  74. default => $state,
  75. };
  76. })
  77. ->badge()
  78. ->color(function ($state): string {
  79. return match ($state) {
  80. 'draft' => 'gray',
  81. 'published' => 'success',
  82. 'archived' => 'danger',
  83. default => 'gray',
  84. };
  85. }),
  86. TextColumn::make('created_at')
  87. ->label('创建时间')
  88. ->dateTime()
  89. ->sortable()
  90. ->toggleable(isToggledHiddenByDefault: true),
  91. TextColumn::make('updated_at')
  92. ->label('更新时间')
  93. ->dateTime()
  94. ->sortable()
  95. ->toggleable(isToggledHiddenByDefault: true),
  96. ])
  97. ->filters([
  98. SelectFilter::make('stage')
  99. ->label('学段')
  100. ->options([
  101. 'primary' => '小学',
  102. 'junior' => '初中',
  103. 'senior' => '高中',
  104. ]),
  105. SelectFilter::make('status')
  106. ->label('状态')
  107. ->options([
  108. 'draft' => '草稿',
  109. 'published' => '已发布',
  110. 'archived' => '已归档',
  111. ]),
  112. ])
  113. ->actions([
  114. EditAction::make()
  115. ->label('编辑'),
  116. Action::make('delete')
  117. ->label('删除')
  118. ->color('danger')
  119. ->icon('heroicon-o-trash')
  120. ->requiresConfirmation()
  121. ->modalHeading('删除教材')
  122. ->modalDescription('确定要删除这个教材吗?此操作无法撤销。')
  123. ->action(function (Model $record) {
  124. // 添加调试日志
  125. \Log::info('Deleting textbook', ['id' => $record->id, 'record' => $record]);
  126. if (!$record || !$record->id) {
  127. \Filament\Notifications\Notification::make()
  128. ->title('错误')
  129. ->body('无效的教材记录。')
  130. ->danger()
  131. ->send();
  132. return;
  133. }
  134. $apiService = app(\App\Services\TextbookApiService::class);
  135. $deleted = $apiService->deleteTextbook($record->id);
  136. \Log::info('Delete result', ['deleted' => $deleted]);
  137. if ($deleted) {
  138. \Filament\Notifications\Notification::make()
  139. ->title('成功')
  140. ->body('教材删除成功。')
  141. ->success()
  142. ->send();
  143. } else {
  144. \Filament\Notifications\Notification::make()
  145. ->title('错误')
  146. ->body('删除失败,请重试。')
  147. ->danger()
  148. ->send();
  149. }
  150. }),
  151. Action::make('view_catalog')
  152. ->label('查看目录')
  153. ->icon('heroicon-o-list-bullet')
  154. ->url(fn(Model $record): string =>
  155. route('filament.admin.resources.textbook-catalogs.index', ['tableFilters[textbook_id][value]' => $record->id])
  156. ),
  157. ])
  158. ->bulkActions([
  159. \Filament\Actions\BulkActionGroup::make([
  160. \Filament\Actions\DeleteBulkAction::make()
  161. ->label('批量删除'),
  162. ]),
  163. ])
  164. ->defaultSort('sort_order')
  165. ->paginated([10, 25, 50, 100]);
  166. }
  167. }