"use client" import { AlertTriangle, CheckCircle2, Clock, MessageSquare, RefreshCw, Star, XCircle, } from "lucide-react" import Link from "next/link" import { useCallback, useEffect } from "react" import { Button } from "@/components/ui/button" import type { Actor } from "@/lib/policy/actor" import { notifyInfo, notifySuccess } from "@/lib/toast" import type { OrderStatus } from "@/lib/types" import { useAuthStore } from "@/store/auth" import { useChatStore } from "@/store/chat" import { useOrderStore } from "@/store/orders" import { useShopStore } from "@/store/shops" interface OrderActionsProps { orderId: string initialStatus: OrderStatus chatSessionId?: string serviceId: string } function showFeedback(message: string) { notifySuccess(message) } export default function OrderActions({ orderId, initialStatus, chatSessionId, serviceId, }: OrderActionsProps) { const currentRole = useAuthStore((state) => state.currentRole) const currentUserId = useAuthStore((state) => state.user?.id) const payOrder = useOrderStore((state) => state.payOrder) const acceptOrder = useOrderStore((state) => state.acceptOrder) const requestClose = useOrderStore((state) => state.requestClose) const confirmClose = useOrderStore((state) => state.confirmClose) const cancelPreAccept = useOrderStore((state) => state.cancelPreAccept) const order = useOrderStore((state) => state.orders.find((item) => item.id === orderId)) const sessions = useChatStore((state) => state.sessions) const ensureOrderSession = useChatStore((state) => state.ensureOrderSession) const actorShopId = useShopStore((state) => { if (!currentUserId || currentRole !== "owner") return undefined const owned = state.shops.find((shop) => shop.owner.id === currentUserId) return owned?.id }) const dispatchMode = useShopStore((state) => { if (!order?.shopId) return "manual" const shop = state.shops.find((item) => item.id === order.shopId) return shop?.dispatchMode ?? "manual" }) const resolvedChatSessionId = chatSessionId ?? sessions.find((session) => session.type === "order" && session.orderId === orderId)?.id const status = order?.status ?? initialStatus const actor: Actor | undefined = currentUserId ? { userId: currentUserId, role: currentRole, shopId: actorShopId, } : undefined const handleDecision = useCallback( (okMessage: string, result: { decision: { ok: boolean; message?: string } }) => { if (result.decision.ok) { showFeedback(okMessage) return } notifyInfo(result.decision.message ?? "当前操作不允许") }, [], ) useEffect(() => { if (chatSessionId || !order || resolvedChatSessionId) return ensureOrderSession(order) }, [chatSessionId, order, ensureOrderSession, resolvedChatSessionId]) useEffect(() => { if (!order) return if (order.status !== "pending_accept") return if (!order.shopId) return if (dispatchMode !== "auto") return const timer = setTimeout(() => { const systemActor: Actor = { userId: order.playerId, role: "player", shopId: order.shopId, } const result = acceptOrder(orderId, systemActor) handleDecision("系统已自动派单", result) }, 3000) return () => clearTimeout(timer) }, [acceptOrder, dispatchMode, handleDecision, order, orderId]) return (
{status === "pending_payment" && ( <> )} {status === "pending_accept" && ( <> {order?.shopId && dispatchMode === "auto" ? ( ) : ( )} )} {(status === "in_progress" || status === "pending_close") && resolvedChatSessionId && ( )} {status === "in_progress" && ( <> )} {status === "pending_close" && ( <> )} {status === "pending_review" && ( )} {status === "completed" && ( )} {status === "cancelled" && ( )} {status === "disputed" && ( )}
) }