"use client" import { useState, useEffect } from "react" import { Search, RefreshCw, Loader2 } from "lucide-react" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Button } from "@/components/ui/button" import { ScrollArea } from "@/components/ui/scroll-area" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Checkbox } from "@/components/ui/checkbox" import { Label } from "@/components/ui/label" import { Badge } from "@/components/ui/badge" import { api } from "@/lib/api" import { showToast } from "@/lib/toast" interface Device { id: string name: string imei: string wechatId: string memo?: string alive: number usedInPlans: number lastActiveTime: string } interface DeviceSelectionDialogProps { open: boolean onOpenChange: (open: boolean) => void selectedDevices: string[] onSelect: (devices: string[]) => void } export function DeviceSelectionDialog({ open, onOpenChange, selectedDevices, onSelect, }: DeviceSelectionDialogProps) { const [searchQuery, setSearchQuery] = useState("") const [statusFilter, setStatusFilter] = useState("all") const [loading, setLoading] = useState(false) const [devices, setDevices] = useState([]) const [tempSelected, setTempSelected] = useState([]) // 获取设备列表 const fetchDevices = async () => { setLoading(true) try { setLoading(true) const response = await api.get<{code: number, msg: string, data: {list: ServerDevice[], total: number}}>('/v1/devices?page=1&limit=100') if (response.code === 200 && response.data.list) { const transformedDevices: Device[] = response.data.list.map(device => ({ id: device.id, name: device.memo || "未命名设备", imei: device.imei || '', wxid: device.wechatId || '', status: device.alive === 1 ? "online" : "offline", totalFriend: device.totalFriend || 0 })) setDevices(transformedDevices) } else { showToast(response.msg || "获取设备列表失败", "error") } } catch (error: any) { console.error("获取设备列表失败:", error) showToast(error?.message || "请检查网络连接", "error") } finally { setLoading(false) } } useEffect(() => { if (open) { fetchDevices() setTempSelected(selectedDevices) } }, [open, searchQuery, selectedDevices]) const handleRefresh = () => { fetchDevices() } const filteredDevices = devices.filter(device => { const matchesSearch = !searchQuery || device.name.toLowerCase().includes(searchQuery.toLowerCase()) || device.imei.toLowerCase().includes(searchQuery.toLowerCase()) || device.wechatId.toLowerCase().includes(searchQuery.toLowerCase()) const matchesStatus = statusFilter === "all" || (statusFilter === "online" && device.alive === 1) || (statusFilter === "offline" && device.alive === 0) return matchesSearch && matchesStatus }) const handleDialogOpenChange = (open: boolean) => { if (!open) { setTempSelected(selectedDevices) } onOpenChange(open) } const handleSelectAll = () => { if (tempSelected.length === filteredDevices.length) { setTempSelected([]) } else { setTempSelected(filteredDevices.map(device => device.id)) } } const handleDeviceToggle = (deviceId: string) => { setTempSelected(prev => prev.includes(deviceId) ? prev.filter(id => id !== deviceId) : [...prev, deviceId] ) } return ( 选择设备
setSearchQuery(e.target.value)} />
已选择 {tempSelected.length} 个设备
{loading ? (
加载中...
) : filteredDevices.length === 0 ? (
暂无数据
) : ( filteredDevices.map((device) => ( )) )}
{/* */}
) }