listPrompts(); // 筛选 if ($this->selectedType) { $prompts = array_filter($prompts, fn($prompt) => $prompt['template_type'] === $this->selectedType ); } if ($this->search) { $searchTerm = strtolower($this->search); $prompts = array_filter($prompts, fn($prompt) => str_contains(strtolower($prompt['template_name']), $searchTerm) || str_contains(strtolower($prompt['description']), $searchTerm) ); } // 分页 $total = count($prompts); $offset = ($this->currentPage - 1) * $this->perPage; $paginated = array_slice($prompts, $offset, $this->perPage); return [ 'data' => $paginated, 'meta' => [ 'page' => $this->currentPage, 'per_page' => $this->perPage, 'total' => $total, 'total_pages' => (int) ceil($total / $this->perPage), ] ]; } catch (\Exception $e) { \Log::error('Failed to fetch prompts: ' . $e->getMessage()); return ['data' => [], 'meta' => ['total' => 0]]; } } /** * 获取提示词类型统计 */ public function getTypeStats(): array { $service = app(QuestionServiceApi::class); try { $prompts = $service->listPrompts(); $stats = []; foreach ($prompts as $prompt) { $type = $prompt['template_type']; if (!isset($stats[$type])) { $stats[$type] = 0; } $stats[$type]++; } return $stats; } catch (\Exception $e) { \Log::error('Failed to fetch prompt stats: ' . $e->getMessage()); return []; } } /** * 获取所有类型选项 */ public function getTypeOptions(): array { return [ '题目生成' => '题目生成', '掌握度评估' => '掌握度评估', '技能熟练度' => '技能熟练度', '质量审核' => '质量审核', ]; } /** * 筛选更新 */ public function updatedSelectedType(): void { $this->currentPage = 1; } public function updatedSearch(): void { $this->currentPage = 1; } public function updatedPerPage(): void { $this->currentPage = 1; } /** * 跳转到指定页 */ public function gotoPage(int $page): void { $this->currentPage = $page; } /** * 上一页 */ public function previousPage(): void { if ($this->currentPage > 1) { $this->currentPage--; } } /** * 下一页 */ public function nextPage(): void { $totalPages = $this->prompts['meta']['total_pages'] ?? 1; if ($this->currentPage < $totalPages) { $this->currentPage++; } } /** * 创建新提示词 */ #[On('create-prompt')] public function createPrompt(): void { Notification::make() ->title('创建提示词') ->body('打开创建表单') ->info() ->send(); } /** * 编辑提示词 */ #[On('edit-prompt')] public function editPrompt(array $prompt): void { Notification::make() ->title('编辑提示词:' . $prompt['template_name']) ->body('打开编辑表单') ->info() ->send(); } /** * 删除提示词 */ #[On('delete-prompt')] public function deletePrompt(string $promptName): void { Notification::make() ->title('删除提示词:' . $promptName) ->body('此操作不可恢复') ->danger() ->send(); } /** * 启用/禁用提示词 */ #[On('toggle-prompt')] public function togglePrompt(string $promptName, bool $isActive): void { $status = $isActive ? '禁用' : '启用'; Notification::make() ->title($status . '提示词:' . $promptName) ->success() ->send(); } /** * 复制提示词 */ #[On('duplicate-prompt')] public function duplicatePrompt(array $prompt): void { Notification::make() ->title('复制提示词:' . $prompt['template_name']) ->success() ->send(); } /** * 刷新数据 */ #[On('refresh-prompts')] public function refreshPrompts(): void { Notification::make() ->title('数据已刷新') ->success() ->send(); } /** * 头部操作 */ protected function getHeaderActions(): array { return [ Actions\Action::make('create') ->label('新建提示词') ->icon('heroicon-m-plus') ->color('success') ->action('createPrompt'), Actions\Action::make('refresh') ->label('刷新') ->icon('heroicon-m-arrow-path') ->color('warning') ->action('refreshPrompts'), ]; } }