"use client"; import { useCallback, useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { useAuth } from "@/features/agent/hooks/useAuth"; import { ResponsiveLayout } from "@/components/layout"; import { fetchFAQs, createFAQ, updateFAQ, deleteFAQ, type FAQSummary, type CreateFAQRequest, type UpdateFAQRequest, } from "@/features/agent/services/faqApi"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from "@/components/ui/dialog"; import { Card } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { Plus, Edit, Trash2, Search, FileText, Save, X, } from "lucide-react"; import { Textarea } from "@/components/ui/textarea"; export default function FAQsPage({ embedded = false }: { embedded?: boolean } = {}) { const router = useRouter(); const { agent } = useAuth(); const [faqs, setFaqs] = useState([]); const [loading, setLoading] = useState(true); const [searchQuery, setSearchQuery] = useState(""); const [createDialogOpen, setCreateDialogOpen] = useState(false); const [editDialogOpen, setEditDialogOpen] = useState(false); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [selectedFAQ, setSelectedFAQ] = useState(null); const [submitting, setSubmitting] = useState(false); // 创建 FAQ 表单 const [createForm, setCreateForm] = useState({ question: "", answer: "", keywords: "", }); // 编辑 FAQ 表单 const [editForm, setEditForm] = useState({ question: "", answer: "", keywords: "", }); // 加载 FAQ 列表 const loadFAQs = useCallback(async () => { setLoading(true); try { // 如果搜索框有内容,使用关键词搜索;否则加载全部 const query = searchQuery.trim() || undefined; const data = await fetchFAQs(query); setFaqs(data); } catch (error) { console.error("加载 FAQ 列表失败:", error); alert((error as Error).message || "加载 FAQ 列表失败"); } finally { setLoading(false); } }, [searchQuery]); // 初始加载和搜索 useEffect(() => { // 延迟搜索,避免频繁请求 const timer = setTimeout(() => { loadFAQs(); }, 500); return () => clearTimeout(timer); }, [loadFAQs]); // 打开创建对话框 const handleOpenCreate = () => { setCreateForm({ question: "", answer: "", keywords: "", }); setCreateDialogOpen(true); }; // 创建 FAQ const handleCreate = async () => { if (!createForm.question.trim() || !createForm.answer.trim()) { alert("问题和答案不能为空"); return; } setSubmitting(true); try { await createFAQ(createForm); setCreateDialogOpen(false); setCreateForm({ question: "", answer: "", keywords: "" }); await loadFAQs(); alert("创建成功"); } catch (error) { alert((error as Error).message || "创建 FAQ 失败"); } finally { setSubmitting(false); } }; // 打开编辑对话框 const handleOpenEdit = (faq: FAQSummary) => { setSelectedFAQ(faq); setEditForm({ question: faq.question, answer: faq.answer, keywords: faq.keywords || "", }); setEditDialogOpen(true); }; // 更新 FAQ const handleUpdate = async () => { if (!selectedFAQ) { return; } if (!editForm.question?.trim() || !editForm.answer?.trim()) { alert("问题和答案不能为空"); return; } setSubmitting(true); try { await updateFAQ(selectedFAQ.id, editForm); setEditDialogOpen(false); setSelectedFAQ(null); await loadFAQs(); alert("更新成功"); } catch (error) { alert((error as Error).message || "更新 FAQ 失败"); } finally { setSubmitting(false); } }; // 打开删除对话框 const handleOpenDelete = (faq: FAQSummary) => { setSelectedFAQ(faq); setDeleteDialogOpen(true); }; // 删除 FAQ const handleDelete = async () => { if (!selectedFAQ) { return; } setSubmitting(true); try { await deleteFAQ(selectedFAQ.id); setDeleteDialogOpen(false); setSelectedFAQ(null); await loadFAQs(); alert("删除成功"); } catch (error) { alert((error as Error).message || "删除 FAQ 失败"); } finally { setSubmitting(false); } }; // 格式化时间 const formatTime = (dateStr: string) => { const date = new Date(dateStr); return date.toLocaleString("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", }); }; // 构建头部内容 const headerContent = (

事件管理(FAQ)

{!embedded && ( )}
{/* 搜索和操作栏 */}
setSearchQuery(e.target.value)} className="pl-10" />
); // 构建主内容区 const mainContent = (
{loading ? (
加载中...
) : faqs.length === 0 ? (
{searchQuery ? "没有找到匹配的事件" : "暂无事件"}
) : (
{faqs.map((faq) => (

{faq.question}

{faq.answer}
{faq.keywords && (
关键词: {faq.keywords}
)}
创建时间: {formatTime(faq.created_at)}
))}
)}
); // 如果是嵌入模式,只返回内容,不包含 ResponsiveLayout if (embedded) { return ( <>
{headerContent} {mainContent}
{/* 对话框 */} {/* 创建 FAQ 对话框 */} 创建新事件 填写问题和答案,可以添加关键词以便搜索