"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 { Search, RefreshCw, X } 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 { Badge } from "@/components/ui/badge" import { Pagination, PaginationContent, PaginationItem, PaginationLink, PaginationNext, PaginationPrevious, } from "@/components/ui/pagination" // 定义类型,避免导入错误 interface Device { id: string imei: string name: string status: string wechatId: string usedInPlans: number } interface DeviceResponse { code: number message: string data: { devices: Device[] total: number } } interface DeviceSelectResponse { code: number message: string data: { success: boolean deviceIds: string[] } } interface DeviceSelectorProps { formData: any onChange: (data: any) => void onNext: () => void onPrev: () => void } export function DeviceSelector({ formData, onChange, onNext, onPrev }: DeviceSelectorProps) { const [devices, setDevices] = useState([]) const [searchQuery, setSearchQuery] = useState("") const [statusFilter, setStatusFilter] = useState("all") const [currentPage, setCurrentPage] = useState(1) const [loading, setLoading] = useState(false) const itemsPerPage = 5 useEffect(() => { fetchDevices() }, []) const fetchDevices = async () => { setLoading(true) try { // 实际项目中这里应该调用API获取所有设备 const response: DeviceResponse = { code: 0, message: "success", data: { devices: 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", wechatId: `wxid_${Math.random().toString(36).substr(2, 8)}`, usedInPlans: Math.floor(Math.random() * 3), })), total: 42, }, } if (response.code === 0) { setDevices(response.data.devices) } else { throw new Error(response.message) } } catch (error) { toast({ title: "获取失败", description: "无法获取设备列表", variant: "destructive", }) } finally { setLoading(false) } } const handleRefresh = () => { fetchDevices() toast({ title: "刷新成功", description: "设备列表已更新", }) } const filteredDevices = devices.filter((device) => { const matchesSearch = device.name.toLowerCase().includes(searchQuery.toLowerCase()) || device.imei.toLowerCase().includes(searchQuery.toLowerCase()) const matchesStatus = statusFilter === "all" || device.status === statusFilter return matchesSearch && matchesStatus }) const paginatedDevices = filteredDevices.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage) const handleDeviceSelect = async (deviceId: string) => { try { // 实际项目中这里应该调用API const response: DeviceSelectResponse = { code: 0, message: "success", data: { success: true, deviceIds: [deviceId], }, } if (response.code === 0 && response.data.success) { const updatedSelection = formData.selectedDevices.includes(deviceId) ? formData.selectedDevices.filter((id: string) => id !== deviceId) : [...formData.selectedDevices, deviceId] onChange({ ...formData, selectedDevices: updatedSelection }) } else { throw new Error(response.message) } } catch (error) { toast({ title: "选择失败", description: "无法选择设备", variant: "destructive", }) } } return (
setSearchQuery(e.target.value)} className="pl-9" />
{paginatedDevices.map((device) => ( handleDeviceSelect(device.id)} >
handleDeviceSelect(device.id)} />
{device.name}
{device.status === "online" ? "在线" : "离线"}
IMEI: {device.imei}
微信号: {device.wechatId}
{device.usedInPlans > 0 && (
已用于 {device.usedInPlans} 个计划
)}
))}
setCurrentPage((prev) => Math.max(1, prev - 1))} disabled={currentPage === 1} /> {Array.from({ length: Math.ceil(filteredDevices.length / itemsPerPage) }, (_, i) => i + 1).map((page) => ( setCurrentPage(page)} isActive={currentPage === page}> {page} ))} setCurrentPage((prev) => Math.min(Math.ceil(filteredDevices.length / itemsPerPage), prev + 1)) } disabled={currentPage === Math.ceil(filteredDevices.length / itemsPerPage)} />

已选设备:

{formData.selectedDevices.map((deviceId: string) => { const device = devices.find((d) => d.id === deviceId) return ( device && ( {device.name} ) ) })}
) }