"use client" import { useState, useEffect } from "react" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Search, Filter, RefreshCw } from "lucide-react" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Checkbox } from "@/components/ui/checkbox" import { Card } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { ScrollArea } from "@/components/ui/scroll-area" import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs" 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 tags?: string[] } interface DeviceSelectionDialogProps { open: boolean onOpenChange: (open: boolean) => void selectedDevices: string[] onSelect: (deviceIds: string[]) => void excludeUsedDevices?: boolean } export function DeviceSelectionDialog({ open, onOpenChange, selectedDevices, onSelect, excludeUsedDevices = false, }: DeviceSelectionDialogProps) { const [devices, setDevices] = useState([]) const [loading, setLoading] = useState(false) const [searchQuery, setSearchQuery] = useState("") const [statusFilter, setStatusFilter] = useState("all") const [tagFilter, setTagFilter] = useState("all") const [selectedDeviceIds, setSelectedDeviceIds] = useState([]) const [activeTab, setActiveTab] = useState("all") // 初始化已选设备 useEffect(() => { if (open) { setSelectedDeviceIds(selectedDevices) } }, [open, selectedDevices]) // 模拟获取设备数据 useEffect(() => { if (!open) return const fetchDevices = async () => { setLoading(true) try { // 模拟API请求 await new Promise((resolve) => setTimeout(resolve, 800)) // 生成模拟数据 const deviceTags = ["高性能", "稳定", "新设备", "已配置", "测试中", "备用"] const mockDevices: Device[] = Array.from({ length: 30 }, (_, i) => { // 随机生成1-3个标签 const tags = Array.from( { length: Math.floor(Math.random() * 3) + 1 }, () => deviceTags[Math.floor(Math.random() * deviceTags.length)], ) // 确保标签唯一 const uniqueTags = Array.from(new Set(tags)) return { 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), tags: uniqueTags, } }) setDevices(mockDevices) } catch (error) { console.error("Failed to fetch devices:", error) } finally { setLoading(false) } } fetchDevices() }, [open]) // 过滤设备 const filteredDevices = devices.filter((device) => { const matchesSearch = searchQuery === "" || device.name.toLowerCase().includes(searchQuery.toLowerCase()) || device.imei.toLowerCase().includes(searchQuery.toLowerCase()) || device.wechatAccounts.some( (account) => account.wechatId.toLowerCase().includes(searchQuery.toLowerCase()) || account.nickname.toLowerCase().includes(searchQuery.toLowerCase()), ) const matchesStatus = statusFilter === "all" || device.status === statusFilter const matchesUsage = !excludeUsedDevices || device.usedInPlans === 0 const matchesTag = tagFilter === "all" || (device.tags && device.tags.includes(tagFilter)) const matchesTab = activeTab === "all" || (activeTab === "online" && device.status === "online") || (activeTab === "offline" && device.status === "offline") || (activeTab === "unused" && device.usedInPlans === 0) return matchesSearch && matchesStatus && matchesUsage && matchesTag && matchesTab }) // 处理选择设备 const handleSelectDevice = (deviceId: string) => { setSelectedDeviceIds((prev) => prev.includes(deviceId) ? prev.filter((id) => id !== deviceId) : [...prev, deviceId], ) } // 处理全选 const handleSelectAll = () => { if (selectedDeviceIds.length === filteredDevices.length) { setSelectedDeviceIds([]) } else { setSelectedDeviceIds(filteredDevices.map((device) => device.id)) } } // 处理确认选择 const handleConfirm = () => { onSelect(selectedDeviceIds) onOpenChange(false) } // 获取所有标签选项 const allTags = Array.from(new Set(devices.flatMap((device) => device.tags || []))) return ( 选择设备
{/* 搜索和筛选区域 */}
setSearchQuery(e.target.value)} className="pl-9" />
{/* 分类标签页 */} 全部 在线 离线 未使用 {/* 筛选器 */}
{allTags.length > 0 && ( )}
{/* 设备列表 */} {loading ? (
) : filteredDevices.length === 0 ? (
{searchQuery || statusFilter !== "all" || tagFilter !== "all" || activeTab !== "all" ? "没有符合条件的设备" : "暂无设备数据"}
) : (
{filteredDevices.map((device) => (
handleSelectDevice(device.id)} id={`device-${device.id}`} />
{device.status === "online" ? "在线" : "离线"}
IMEI:
{/* 微信账号信息 */}
{device.wechatAccounts.map((account) => (
{account.nickname} {account.wechatId}
今日可添加:{account.remainingAdds} {account.todayAdded}/{account.maxDailyAdds}
))}
{/* 标签展示 */} {device.tags && device.tags.length > 0 && (
{device.tags.map((tag) => ( {tag} ))}
)} {device.usedInPlans > 0 && (
已用于 {device.usedInPlans} 个计划
)}
))}
)}
已选择 {selectedDeviceIds.length} 个设备
) }