"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { ChevronLeft, MoreVertical, Clock, Edit, Trash2, Copy, RefreshCw, FileText, MessageSquare, History } from "lucide-react" import { Card } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Avatar } from "@/components/ui/avatar" import { Switch } from "@/components/ui/switch" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog" import { api, ApiResponse } from "@/lib/api" import { showToast } from "@/lib/toast" // 定义任务详情的接口 interface TaskDetail { id: string name: string status: "running" | "paused" syncType: number accountType: number syncCount: number syncInterval: number startTime: string endTime: string enabled: boolean devices: { id: string name: string avatar: string }[] contentLibraries: { id: string name: string count: number }[] lastSyncTime: string createTime: string creator: string } // 定义同步历史的接口 interface SyncHistory { id: string syncTime: string content: string contentType: "text" | "image" | "video" status: "success" | "failed" errorMessage?: string } export default function MomentsSyncDetailPage({ params }: { params: { id: string } }) { const router = useRouter() const [taskDetail, setTaskDetail] = useState(null) const [syncHistory, setSyncHistory] = useState([]) const [isLoading, setIsLoading] = useState(true) const [activeTab, setActiveTab] = useState("overview") const [showDeleteAlert, setShowDeleteAlert] = useState(false) // 获取任务详情 useEffect(() => { const fetchTaskDetail = async () => { setIsLoading(true) try { const response = await api.get(`/v1/workbench/detail?id=${params.id}`) if (response.code === 200 && response.data) { setTaskDetail(response.data) // 获取同步历史 if (activeTab === "history") { fetchSyncHistory() } } else { showToast(response.msg || "获取任务详情失败", "error") router.push("/workspace/moments-sync") } } catch (error: any) { console.error("获取任务详情失败:", error) showToast(error?.message || "获取任务详情失败", "error") router.push("/workspace/moments-sync") } finally { setIsLoading(false) } } fetchTaskDetail() }, [params.id, router]) // 获取同步历史 const fetchSyncHistory = async () => { try { const response = await api.get(`/v1/workbench/sync/history?id=${params.id}`) if (response.code === 200 && response.data) { setSyncHistory(response.data.list || []) } else { setSyncHistory([]) } } catch (error) { console.error("获取同步历史失败:", error) setSyncHistory([]) } } // 切换Tab时加载数据 const handleTabChange = (value: string) => { setActiveTab(value) if (value === "history" && syncHistory.length === 0) { fetchSyncHistory() } } // 切换任务状态 const toggleTaskStatus = async () => { if (!taskDetail) return try { const newStatus = taskDetail.status === "running" ? "paused" : "running" const response = await api.post('/v1/workbench/update/status', { id: params.id, status: newStatus === "running" ? 1 : 0 }) if (response.code === 200) { setTaskDetail({ ...taskDetail, status: newStatus }) showToast(`任务已${newStatus === "running" ? "启用" : "暂停"}`, "success") } else { showToast(response.msg || "操作失败", "error") } } catch (error: any) { console.error("更新任务状态失败:", error) showToast(error?.message || "更新任务状态失败", "error") } } // 编辑任务 const handleEdit = () => { router.push(`/workspace/moments-sync/${params.id}/edit`) } // 确认删除 const confirmDelete = () => { setShowDeleteAlert(true) } // 执行删除 const handleDelete = async () => { try { const response = await api.post('/v1/workbench/delete', { id: params.id }) if (response.code === 200) { showToast("删除成功", "success") router.push("/workspace/moments-sync") } else { showToast(response.msg || "删除失败", "error") } } catch (error: any) { console.error("删除任务失败:", error) showToast(error?.message || "删除任务失败", "error") } finally { setShowDeleteAlert(false) } } // 复制任务 const handleCopy = async () => { try { const response = await api.post('/v1/workbench/copy', { id: params.id }) if (response.code === 200) { showToast("复制成功,正在跳转到新任务", "success") // 假设后端返回了新任务的ID if (response.data?.id) { router.push(`/workspace/moments-sync/${response.data.id}`) } else { router.push("/workspace/moments-sync") } } else { showToast(response.msg || "复制失败", "error") } } catch (error: any) { console.error("复制任务失败:", error) showToast(error?.message || "复制任务失败", "error") } } if (isLoading) { return (

加载中...

) } if (!taskDetail) { return (

任务不存在或已被删除

) } return (

任务详情

编辑 复制 删除

{taskDetail.name}

{taskDetail.status === "running" ? "进行中" : "已暂停"}
创建时间:{taskDetail.createTime}
创建人:{taskDetail.creator}
上次同步:{taskDetail.lastSyncTime}
已同步:{taskDetail.syncCount} 条
基本信息 设备列表 同步历史
账号类型
{taskDetail.accountType === 1 ? "业务号" : "人设号"}
同步类型
{taskDetail.syncType === 1 ? "循环同步" : "实时更新"}
同步间隔
{taskDetail.syncInterval} 分钟
每日同步数量
{taskDetail.syncCount} 条
允许发布时间段
{taskDetail.startTime} - {taskDetail.endTime}
内容库
{taskDetail.contentLibraries.map((lib) => ( {lib.name} ))}
{taskDetail.devices.length === 0 ? (
暂无关联设备
) : (
{taskDetail.devices.map((device) => (
{device.avatar ? ( {device.name} ) : (
{device.name.charAt(0)}
)}
{device.name}
ID: {device.id}
))}
)}

同步历史

{syncHistory.length === 0 ? (
暂无同步历史
) : (
{syncHistory.map((record) => (
{record.contentType === "text" && } {record.contentType === "image" && 图片} {record.contentType === "video" && 视频} {record.status === "success" ? "成功" : "失败"}
{record.syncTime}
{record.content}
{record.status === "failed" && record.errorMessage && (
错误信息: {record.errorMessage}
)}
))}
)}
{/* 删除确认对话框 */} 确认删除 删除后,此任务将无法恢复。确定要删除吗? 取消 删除
) }