"use client" import { useState, useEffect } from "react" import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Search, Filter, RefreshCw } from "lucide-react" import { Badge } from "@/components/ui/badge" import { Card } from "@/components/ui/card" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { toast } from "@/components/ui/use-toast" interface Device { id: string name: string imei: string status: "online" | "offline" wechatId: string friendCount: number battery: number avatar?: string } interface DeviceSelectionDialogProps { open: boolean onOpenChange: (open: boolean) => void selectedDevices: string[] onSelect: (deviceIds: string[]) => void excludeUsedDevices?: boolean planId?: string } export function DeviceSelectionDialog({ open, onOpenChange, selectedDevices, onSelect, excludeUsedDevices = false, planId, }: DeviceSelectionDialogProps) { const [devices, setDevices] = useState([]) const [filteredDevices, setFilteredDevices] = useState([]) const [searchQuery, setSearchQuery] = useState("") const [statusFilter, setStatusFilter] = useState("all") const [selected, setSelected] = useState(selectedDevices || []) const [currentPage, setCurrentPage] = useState(1) const devicesPerPage = 5 useEffect(() => { const fetchDevices = async () => { // 模拟API调用获取设备列表 const mockDevices: Device[] = Array.from({ length: 20 }, (_, i) => ({ id: `device-${i + 1}`, name: `设备 ${i + 1}`, imei: `sd${123123 + i}`, status: Math.random() > 0.2 ? "online" : "offline", wechatId: `wxid_${Math.random().toString(36).substr(2, 8)}`, friendCount: Math.floor(Math.random() * 1000), battery: Math.floor(Math.random() * 100), avatar: "https://api.dicebear.com/7.x/avataaars/svg?seed=" + (i + 1), })) if (excludeUsedDevices && planId) { const availableDevices = mockDevices.filter((device) => !device.id.includes("-even")) setDevices(availableDevices) setFilteredDevices(availableDevices) } else { setDevices(mockDevices) setFilteredDevices(mockDevices) } } if (open) { fetchDevices() setSelected(selectedDevices || []) } }, [open, selectedDevices, excludeUsedDevices, planId]) useEffect(() => { const filtered = 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 }) setFilteredDevices(filtered) setCurrentPage(1) }, [searchQuery, statusFilter, devices]) const handleSelect = (deviceId: string) => { let newSelected: string[] if (selected.includes(deviceId)) { // 如果已选中,则取消选择 newSelected = selected.filter((id) => id !== deviceId) } else { // 如果未选中,检查是否超过限制 if (selected.length >= 5) { toast({ title: "选择超出限制", description: "最多可选择5个设备", variant: "destructive", }) return } newSelected = [...selected, deviceId] } setSelected(newSelected) onSelect(newSelected) // 直接触发选择回调 } const handleRefresh = () => { const refreshedDevices = devices.map((device) => ({ ...device, status: Math.random() > 0.2 ? "online" : "offline", battery: Math.floor(Math.random() * 100), })) setDevices(refreshedDevices) setFilteredDevices(refreshedDevices) } // 分页逻辑 const totalPages = Math.ceil(filteredDevices.length / devicesPerPage) const startIndex = (currentPage - 1) * devicesPerPage const currentDevices = filteredDevices.slice(startIndex, startIndex + devicesPerPage) return ( 选择设备
setSearchQuery(e.target.value)} className="pl-9" />
已选择 {selected.length}/5 个设备
{currentDevices.length === 0 ? (
暂无符合条件的设备
) : ( currentDevices.map((device) => ( handleSelect(device.id)} >
{device.name}
{device.status === "online" ? "在线" : "离线"}
IMEI: {device.imei}
微信号: {device.wechatId}
好友数: {device.friendCount} 电量: {device.battery}%
)) )}
{totalPages > 1 && (
{currentPage} / {totalPages}
)}
) }