"use client" import { useState, useEffect } from "react" import { Card } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Filter, Search, RefreshCw, AlertCircle } from "lucide-react" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Checkbox } from "@/components/ui/checkbox" import { toast } from "@/components/ui/use-toast" import { Progress } from "@/components/ui/progress" import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip" import { Pagination, PaginationContent, PaginationItem, PaginationLink, PaginationNext, PaginationPrevious, } from "@/components/ui/pagination" import { ImeiDisplay } from "@/components/ImeiDisplay" interface WechatAccount { wechatId: string nickname: string remainingAdds: number maxDailyAdds: number todayAdded: number } interface Device { id: string imei: string name: string status: "online" | "offline" wechatAccounts: WechatAccount[] usedInPlans: number } interface DeviceSelectorProps { onSelect: (selectedDevices: string[]) => void initialSelectedDevices?: string[] excludeUsedDevices?: boolean } export function DeviceSelector({ onSelect, initialSelectedDevices = [], excludeUsedDevices = true, }: DeviceSelectorProps) { const [devices, setDevices] = useState([]) const [selectedDevices, setSelectedDevices] = useState(initialSelectedDevices) const [searchQuery, setSearchQuery] = useState("") const [statusFilter, setStatusFilter] = useState("all") const [currentPage, setCurrentPage] = useState(1) const devicesPerPage = 10 useEffect(() => { // 模拟获取设备数据 const fetchDevices = async () => { await new Promise((resolve) => setTimeout(resolve, 500)) const mockDevices = Array.from({ length: 42 }, (_, i) => ({ id: `device-${i + 1}`, imei: `IMEI-${Math.random().toString(36).substr(2, 9)}`, name: `设备 ${i + 1}`, status: Math.random() > 0.3 ? "online" : "offline", wechatAccounts: Array.from({ length: Math.floor(Math.random() * 2) + 1 }, (_, j) => ({ wechatId: `wxid_${Math.random().toString(36).substr(2, 8)}`, nickname: `微信号 ${j + 1}`, remainingAdds: Math.floor(Math.random() * 10) + 5, maxDailyAdds: 20, todayAdded: Math.floor(Math.random() * 15), })), usedInPlans: Math.floor(Math.random() * 3), })) setDevices(mockDevices) } fetchDevices() }, []) const handleRefresh = () => { toast({ title: "刷新成功", description: "设备列表已更新", }) } const filteredDevices = devices.filter((device) => { const matchesSearch = device.name.toLowerCase().includes(searchQuery.toLowerCase()) || device.imei.toLowerCase().includes(searchQuery.toLowerCase()) || device.wechatAccounts.some((account) => account.wechatId.toLowerCase().includes(searchQuery.toLowerCase())) const matchesStatus = statusFilter === "all" || device.status === statusFilter const matchesUsage = !excludeUsedDevices || device.usedInPlans === 0 return matchesSearch && matchesStatus && matchesUsage }) const paginatedDevices = filteredDevices.slice((currentPage - 1) * devicesPerPage, currentPage * devicesPerPage) const handleSelectAll = () => { if (selectedDevices.length === paginatedDevices.length) { setSelectedDevices([]) } else { setSelectedDevices(paginatedDevices.map((device) => device.id)) } onSelect(selectedDevices) } const handleDeviceSelect = (deviceId: string) => { const updatedSelection = selectedDevices.includes(deviceId) ? selectedDevices.filter((id) => id !== deviceId) : [...selectedDevices, deviceId] setSelectedDevices(updatedSelection) onSelect(updatedSelection) } return (
setSearchQuery(e.target.value)} className="pl-9" />
{paginatedDevices.map((device) => (
handleDeviceSelect(device.id)} />
{device.name}
{device.status === "online" ? "在线" : "离线"}
IMEI:
{device.wechatAccounts.map((account) => (
{account.nickname} {account.wechatId}
今日可添加: {account.remainingAdds}

每日最多添加 {account.maxDailyAdds} 个好友

{account.todayAdded}/{account.maxDailyAdds}
))}
{!excludeUsedDevices && device.usedInPlans > 0 && (
已用于 {device.usedInPlans} 个计划
)}
))}
{ e.preventDefault() setCurrentPage((prev) => Math.max(1, prev - 1)) }} /> {Array.from({ length: Math.ceil(filteredDevices.length / devicesPerPage) }, (_, i) => i + 1).map((page) => ( { e.preventDefault() setCurrentPage(page) }} > {page} ))} { e.preventDefault() setCurrentPage((prev) => Math.min(Math.ceil(filteredDevices.length / devicesPerPage), prev + 1)) }} />
) }