"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { ChevronLeft, Users, Smartphone, Settings } from "lucide-react" import { Badge } from "@/components/ui/badge" import { Card } from "@/components/ui/card" import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" import { Switch } from "@/components/ui/switch" import { DeviceSelectionDialog } from "@/app/components/device-selection-dialog" import { TrafficPoolSelector } from "@/app/components/traffic-pool-selector" interface UserTag { id: string name: string color: string } interface TrafficUser { id: string avatar: string nickname: string wechatId: string phone: string region: string note: string status: "pending" | "added" | "failed" addTime: string source: string assignedTo: string category: "potential" | "customer" | "lost" tags: UserTag[] } type DeviceSelectionType = "all" | "new" | "specific" // 模拟规则数据 const mockRuleData = [ { id: "1", name: "新用户流量分发", selectedUsers: [ { id: "user-1", avatar: "/placeholder.svg?height=40&width=40", nickname: "用户1", wechatId: "wxid_abc123", phone: "13800138000", region: "北京", note: "", status: "added" as const, addTime: "2023-05-15T08:30:00.000Z", source: "抖音直播", assignedTo: "销售1", category: "potential" as const, tags: [ { id: "tag1", name: "潜在客户", color: "bg-blue-100 text-blue-800" }, { id: "tag4", name: "需跟进", color: "bg-yellow-100 text-yellow-800" }, ], }, { id: "user-2", avatar: "/placeholder.svg?height=40&width=40", nickname: "用户2", wechatId: "wxid_def456", phone: "13900139000", region: "上海", note: "这是用户2的备注", status: "pending" as const, addTime: "2023-05-16T10:15:00.000Z", source: "小红书", assignedTo: "销售2", category: "potential" as const, tags: [{ id: "tag1", name: "潜在客户", color: "bg-blue-100 text-blue-800" }], }, ], deviceSelectionType: "all", selectedDevices: [], priority: "high", createAsPackage: false, price: "", autoDistribute: true, }, { id: "2", name: "高端用户流量包", selectedUsers: [ { id: "user-3", avatar: "/placeholder.svg?height=40&width=40", nickname: "用户3", wechatId: "wxid_ghi789", phone: "13700137000", region: "广州", note: "", status: "added" as const, addTime: "2023-05-14T14:20:00.000Z", source: "微信朋友圈", assignedTo: "销售3", category: "customer" as const, tags: [ { id: "tag2", name: "高意向", color: "bg-green-100 text-green-800" }, { id: "tag7", name: "企业客户", color: "bg-red-100 text-red-800" }, ], }, ], deviceSelectionType: "specific", selectedDevices: ["device-1", "device-4", "device-5"], priority: "low", createAsPackage: true, price: "2.5", autoDistribute: false, }, ] export default function EditRulePage({ params }: { params: { id: string } }) { const router = useRouter() const { id } = params const [currentStep, setCurrentStep] = useState(1) const [name, setName] = useState("") // 流量池相关状态 const [selectedUsers, setSelectedUsers] = useState([]) const [trafficPoolDialogOpen, setTrafficPoolDialogOpen] = useState(false) // 设备选择相关状态 const [deviceSelectionType, setDeviceSelectionType] = useState("all") const [selectedDevices, setSelectedDevices] = useState([]) const [deviceDialogOpen, setDeviceDialogOpen] = useState(false) // 规则设定相关状态 const [priority, setPriority] = useState("high") const [createAsPackage, setCreateAsPackage] = useState(false) const [price, setPrice] = useState("") const [autoDistribute, setAutoDistribute] = useState(true) const [loading, setLoading] = useState(true) useEffect(() => { // 在实际应用中,这里会从API获取数据 // 这里使用模拟数据 const ruleItem = mockRuleData.find((item) => item.id === id) if (ruleItem) { setName(ruleItem.name) setSelectedUsers(ruleItem.selectedUsers) setDeviceSelectionType(ruleItem.deviceSelectionType as DeviceSelectionType) setSelectedDevices(ruleItem.selectedDevices) setPriority(ruleItem.priority) setCreateAsPackage(ruleItem.createAsPackage) setPrice(ruleItem.price) setAutoDistribute(ruleItem.autoDistribute) } else { // 如果找不到数据,返回列表页 router.push("/workspace/pricing") } setLoading(false) }, [id, router]) const handleDeviceSelect = (deviceIds: string[]) => { setSelectedDevices(deviceIds) } const handleUserSelect = (users: TrafficUser[]) => { setSelectedUsers(users) } const handleNext = () => { if (currentStep === 1) { if (name.trim() && selectedUsers.length > 0) { setCurrentStep(2) } else { alert("请填写任务名称并选择流量池") } } else if (currentStep === 2) { if (deviceSelectionType === "specific" && selectedDevices.length === 0) { alert("请选择至少一个设备") } else { setCurrentStep(3) } } } const handlePrevious = () => { if (currentStep === 2) { setCurrentStep(1) } else if (currentStep === 3) { setCurrentStep(2) } } const handleSubmit = () => { // 在实际应用中,这里会发送API请求更新数据 console.log({ id, name, selectedUsers: selectedUsers.map((user) => user.id), deviceSelectionType, selectedDevices: deviceSelectionType === "specific" ? selectedDevices : [], priority, createAsPackage, price: createAsPackage ? price : "", autoDistribute, }) // 返回到列表页 router.push("/workspace/pricing") } if (loading) { return (
) } return (
{/* 顶部栏 */}

编辑规则

{/* 占位,保持标题居中 */}
{/* 进度条 */}
= 1 ? "bg-blue-600 text-white" : "bg-gray-200 text-gray-600" }`} >
= 2 ? "bg-blue-600" : "bg-gray-200"}`}>
= 2 ? "bg-blue-600 text-white" : "bg-gray-200 text-gray-600" }`} >
= 3 ? "bg-blue-600" : "bg-gray-200"}`}>
= 3 ? "bg-blue-600 text-white" : "bg-gray-200 text-gray-600" }`} >
选择流量池
选择设备
规则设定
{/* 主内容区 */}
{currentStep === 1 ? (
setName(e.target.value)} placeholder="请输入任务名称" required />

选择流量池

{selectedUsers.length > 0 ? (

已选择的流量池

{selectedUsers.length} 个用户
{/* 显示选中用户的标签统计 */}
{Array.from( new Set(selectedUsers.flatMap((user) => user.tags.map((tag) => JSON.stringify(tag)))), ).map((tagJson) => { const tag = JSON.parse(tagJson) as UserTag const count = selectedUsers.filter((user) => user.tags.some((t) => t.id === tag.id)).length return ( {tag.name} ({count}) ) })}
{/* 显示选中用户的来源统计 */}
来源分布
{Array.from(new Set(selectedUsers.map((user) => user.source))).map((source) => { const count = selectedUsers.filter((user) => user.source === source).length return ( {source} ({count}) ) })}
) : (

请选择流量池用户

您可以从流量池中选择特定标签的用户

)}
) : currentStep === 2 ? (

设备选择

setDeviceSelectionType(value as DeviceSelectionType)} className="space-y-4" >
{deviceSelectionType === "specific" && (
{selectedDevices.length > 0 && (

已选择的设备

{selectedDevices.length} 个设备

您已选择 {selectedDevices.length} 个设备用于流量分发。 点击上方按钮可以修改选择。

)}
)} {deviceSelectionType === "all" && (

您选择了使用所有可用设备进行流量分发。系统将自动分配流量到所有在线设备。

)} {deviceSelectionType === "new" && (

您选择了使用新添加的设备进行流量分发。系统将自动将流量分配到新接入的设备。

)}
{/* 按钮组 */}
) : (

规则设定

{/* 优先级选择 */}

优先级

高优先级规则将优先于低优先级规则执行。当多个规则匹配同一流量时,系统将按优先级顺序分配。

{/* 自动分发设置 */}

开启后,系统将自动按照规则分发流量。关闭后,需要手动触发分发。

{/* 创建为流量包 */}

开启后,将创建为可售卖的流量包,可设置价格。

{createAsPackage && (
¥ setPrice(e.target.value)} placeholder="0.00" className="pl-8" required />
)}
{/* 按钮组 */}
)}
) }