"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { Card } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { ChevronLeft, Filter, Search, RefreshCw } from "lucide-react" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Badge } from "@/components/ui/badge" import { Checkbox } from "@/components/ui/checkbox" import { toast } from "@/components/ui/use-toast" import type { Device } from "@/types/device" export default function ScenarioDevicesPage({ params }: { params: { channel: string } }) { const router = useRouter() const [devices, setDevices] = useState([]) const [searchQuery, setSearchQuery] = useState("") const [statusFilter, setStatusFilter] = useState("all") const [currentPage, setCurrentPage] = useState(1) const [selectedDevices, setSelectedDevices] = useState([]) const devicesPerPage = 10 const maxDevices = 5 // 获取渠道中文名称 const getChannelName = (channel: string) => { const channelMap: Record = { douyin: "抖音", kuaishou: "快手", xiaohongshu: "小红书", weibo: "微博", } return channelMap[channel] || channel } const channelName = getChannelName(params.channel) useEffect(() => { // 模拟API调用 const fetchDevices = async () => { const mockDevices = Array.from({ length: 15 }, (_, i) => ({ id: `device-${i + 1}`, imei: `sd${123123 + i}`, name: `设备 ${i + 1}`, remark: `${channelName}获客设备 ${i + 1}`, status: Math.random() > 0.2 ? "online" : "offline", battery: Math.floor(Math.random() * 100), wechatId: `wxid_${Math.random().toString(36).substr(2, 8)}`, friendCount: Math.floor(Math.random() * 1000), todayAdded: Math.floor(Math.random() * 50), messageCount: Math.floor(Math.random() * 200), lastActive: new Date(Date.now() - Math.random() * 86400000).toLocaleString(), addFriendStatus: Math.random() > 0.2 ? "normal" : "abnormal", })) setDevices(mockDevices) } fetchDevices() }, [channelName]) const handleRefresh = () => { toast({ title: "刷新成功", description: "设备列表已更新", }) } const handleSelectAll = () => { if (selectedDevices.length === devices.length || selectedDevices.length === maxDevices) { setSelectedDevices([]) } else { const newSelection = devices.slice(0, maxDevices).map((d) => d.id) setSelectedDevices(newSelection) } } const handleDeviceSelect = (deviceId: string) => { if (selectedDevices.includes(deviceId)) { setSelectedDevices(selectedDevices.filter((id) => id !== deviceId)) } else { if (selectedDevices.length >= maxDevices) { toast({ title: "选择超出限制", description: `最多可选择${maxDevices}个设备`, variant: "destructive", }) return } setSelectedDevices([...selectedDevices, deviceId]) } } const handleSave = async () => { try { // 这里应该是实际的API调用来保存选中的设备 await new Promise((resolve) => setTimeout(resolve, 1000)) toast({ title: "保存成功", description: "已更新计划设备", }) router.back() } catch (error) { toast({ title: "保存失败", description: "更新设备失败,请重试", variant: "destructive", }) } } const filteredDevices = devices.filter((device) => { const matchesSearch = device.name.toLowerCase().includes(searchQuery.toLowerCase()) || device.imei.toLowerCase().includes(searchQuery.toLowerCase()) || device.wechatId.toLowerCase().includes(searchQuery.toLowerCase()) const matchesStatus = statusFilter === "all" || device.status === statusFilter return matchesSearch && matchesStatus }) const paginatedDevices = filteredDevices.slice((currentPage - 1) * devicesPerPage, currentPage * devicesPerPage) return (

{channelName}设备

setSearchQuery(e.target.value)} className="pl-9" />
已选择 {selectedDevices.length}/{maxDevices} 个设备
0 && (selectedDevices.length === devices.length || selectedDevices.length === maxDevices) } onCheckedChange={handleSelectAll} /> 全选
{paginatedDevices.length === 0 ? (
暂无设备
) : ( paginatedDevices.map((device) => ( handleDeviceSelect(device.id)} >
{ e.stopPropagation() handleDeviceSelect(device.id) }} />
{device.name}
{device.status === "online" ? "在线" : "离线"}
IMEI: {device.imei}
微信号: {device.wechatId}
好友数: {device.friendCount} 今日新增: +{device.todayAdded}
)) )}
{filteredDevices.length > devicesPerPage && (
第 {currentPage} / {Math.ceil(filteredDevices.length / devicesPerPage)} 页
)}
) }