2025-07-16 15:18:54 +08:00
|
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
|
|
import { Search } from "lucide-react";
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
|
import { ScrollArea } from "@/components/ui/scroll-area";
|
|
|
|
|
|
import { Checkbox } from "@/components/ui/checkbox";
|
|
|
|
|
|
import {
|
|
|
|
|
|
Dialog,
|
|
|
|
|
|
DialogContent,
|
|
|
|
|
|
DialogHeader,
|
|
|
|
|
|
DialogTitle,
|
|
|
|
|
|
} from "@/components/ui/dialog";
|
|
|
|
|
|
import { fetchDeviceList } from "@/api/devices";
|
2025-07-10 15:34:23 +08:00
|
|
|
|
|
|
|
|
|
|
// 设备选择项接口
|
|
|
|
|
|
interface DeviceSelectionItem {
|
|
|
|
|
|
id: string;
|
|
|
|
|
|
name: string;
|
|
|
|
|
|
imei: string;
|
|
|
|
|
|
wechatId: string;
|
2025-07-16 15:18:54 +08:00
|
|
|
|
status: "online" | "offline";
|
2025-07-10 15:34:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 组件属性接口
|
|
|
|
|
|
interface DeviceSelectionProps {
|
|
|
|
|
|
selectedDevices: string[];
|
|
|
|
|
|
onSelect: (devices: string[]) => void;
|
|
|
|
|
|
placeholder?: string;
|
|
|
|
|
|
className?: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-16 15:18:54 +08:00
|
|
|
|
export default function DeviceSelection({
|
|
|
|
|
|
selectedDevices,
|
|
|
|
|
|
onSelect,
|
2025-07-10 15:34:23 +08:00
|
|
|
|
placeholder = "选择设备",
|
2025-07-16 15:18:54 +08:00
|
|
|
|
className = "",
|
2025-07-10 15:34:23 +08:00
|
|
|
|
}: DeviceSelectionProps) {
|
|
|
|
|
|
const [dialogOpen, setDialogOpen] = useState(false);
|
|
|
|
|
|
const [devices, setDevices] = useState<DeviceSelectionItem[]>([]);
|
2025-07-16 15:18:54 +08:00
|
|
|
|
const [searchQuery, setSearchQuery] = useState("");
|
|
|
|
|
|
const [statusFilter, setStatusFilter] = useState("all");
|
2025-07-10 15:34:23 +08:00
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
|
|
2025-07-17 09:59:54 +08:00
|
|
|
|
// 获取设备列表,支持keyword
|
|
|
|
|
|
const fetchDevices = async (keyword: string = "") => {
|
2025-07-10 15:34:23 +08:00
|
|
|
|
setLoading(true);
|
|
|
|
|
|
try {
|
2025-07-17 09:59:54 +08:00
|
|
|
|
const res = await fetchDeviceList(1, 100, keyword.trim() || undefined);
|
2025-07-10 15:34:23 +08:00
|
|
|
|
if (res && res.data && Array.isArray(res.data.list)) {
|
2025-07-16 15:18:54 +08:00
|
|
|
|
setDevices(
|
|
|
|
|
|
res.data.list.map((d) => ({
|
|
|
|
|
|
id: d.id?.toString() || "",
|
|
|
|
|
|
name: d.memo || d.imei || "",
|
|
|
|
|
|
imei: d.imei || "",
|
|
|
|
|
|
wechatId: d.wechatId || "",
|
|
|
|
|
|
status: d.alive === 1 ? "online" : "offline",
|
|
|
|
|
|
}))
|
|
|
|
|
|
);
|
2025-07-10 15:34:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
2025-07-16 15:18:54 +08:00
|
|
|
|
console.error("获取设备列表失败:", error);
|
2025-07-10 15:34:23 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-07-17 09:59:54 +08:00
|
|
|
|
// 打开弹窗时获取设备列表
|
|
|
|
|
|
const openDialog = () => {
|
|
|
|
|
|
setSearchQuery("");
|
|
|
|
|
|
setDialogOpen(true);
|
|
|
|
|
|
fetchDevices("");
|
|
|
|
|
|
};
|
2025-07-10 15:34:23 +08:00
|
|
|
|
|
2025-07-17 09:59:54 +08:00
|
|
|
|
// 搜索防抖
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (!dialogOpen) return;
|
|
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
|
|
fetchDevices(searchQuery);
|
|
|
|
|
|
}, 500);
|
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
|
|
}, [searchQuery, dialogOpen]);
|
|
|
|
|
|
|
|
|
|
|
|
// 过滤设备(只保留状态过滤)
|
|
|
|
|
|
const filteredDevices = devices.filter((device) => {
|
2025-07-10 15:34:23 +08:00
|
|
|
|
const matchesStatus =
|
2025-07-16 15:18:54 +08:00
|
|
|
|
statusFilter === "all" ||
|
|
|
|
|
|
(statusFilter === "online" && device.status === "online") ||
|
|
|
|
|
|
(statusFilter === "offline" && device.status === "offline");
|
2025-07-17 09:59:54 +08:00
|
|
|
|
return matchesStatus;
|
2025-07-10 15:34:23 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 处理设备选择
|
|
|
|
|
|
const handleDeviceToggle = (deviceId: string) => {
|
|
|
|
|
|
if (selectedDevices.includes(deviceId)) {
|
2025-07-16 15:18:54 +08:00
|
|
|
|
onSelect(selectedDevices.filter((id) => id !== deviceId));
|
2025-07-10 15:34:23 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
onSelect([...selectedDevices, deviceId]);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取显示文本
|
|
|
|
|
|
const getDisplayText = () => {
|
2025-07-16 15:18:54 +08:00
|
|
|
|
if (selectedDevices.length === 0) return "";
|
2025-07-10 15:34:23 +08:00
|
|
|
|
return `已选择 ${selectedDevices.length} 个设备`;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<>
|
|
|
|
|
|
{/* 输入框 */}
|
|
|
|
|
|
<div className={`relative ${className}`}>
|
|
|
|
|
|
<Search className="absolute left-3 top-4 h-5 w-5 text-gray-400" />
|
|
|
|
|
|
<Input
|
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
|
className="pl-10 h-14 rounded-xl border-gray-200 text-base"
|
|
|
|
|
|
readOnly
|
2025-07-17 09:59:54 +08:00
|
|
|
|
onClick={openDialog}
|
2025-07-10 15:34:23 +08:00
|
|
|
|
value={getDisplayText()}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 设备选择弹窗 */}
|
|
|
|
|
|
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
|
2025-07-17 17:43:54 +08:00
|
|
|
|
<DialogContent
|
|
|
|
|
|
className="w-full h-full max-w-none max-h-none flex flex-col bg-white"
|
|
|
|
|
|
aria-describedby="device-selection-description"
|
|
|
|
|
|
>
|
|
|
|
|
|
<div id="device-selection-description" className="sr-only">
|
|
|
|
|
|
请选择一个或多个设备,支持搜索和筛选。
|
|
|
|
|
|
</div>
|
2025-07-10 15:34:23 +08:00
|
|
|
|
<DialogHeader>
|
|
|
|
|
|
<DialogTitle>选择设备</DialogTitle>
|
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="flex items-center space-x-4 my-4">
|
|
|
|
|
|
<div className="relative flex-1">
|
|
|
|
|
|
<Search className="absolute left-3 top-2.5 h-4 w-4 text-gray-400" />
|
|
|
|
|
|
<Input
|
|
|
|
|
|
placeholder="搜索设备IMEI/备注/微信号"
|
|
|
|
|
|
value={searchQuery}
|
|
|
|
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
|
|
|
|
className="pl-9"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<select
|
|
|
|
|
|
value={statusFilter}
|
2025-07-16 15:18:54 +08:00
|
|
|
|
onChange={(e) => setStatusFilter(e.target.value)}
|
2025-07-10 15:34:23 +08:00
|
|
|
|
className="w-32 h-10 rounded border border-gray-300 px-2 text-base"
|
|
|
|
|
|
>
|
|
|
|
|
|
<option value="all">全部状态</option>
|
|
|
|
|
|
<option value="online">在线</option>
|
|
|
|
|
|
<option value="offline">离线</option>
|
|
|
|
|
|
</select>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-07-17 17:01:40 +08:00
|
|
|
|
<div className="flex-1 overflow-y-auto">
|
2025-07-10 15:34:23 +08:00
|
|
|
|
{loading ? (
|
|
|
|
|
|
<div className="flex items-center justify-center h-full">
|
|
|
|
|
|
<div className="text-gray-500">加载中...</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
|
{filteredDevices.map((device) => (
|
|
|
|
|
|
<label
|
|
|
|
|
|
key={device.id}
|
|
|
|
|
|
className="flex items-start space-x-3 p-4 rounded-lg hover:bg-gray-50 cursor-pointer"
|
|
|
|
|
|
>
|
|
|
|
|
|
<Checkbox
|
|
|
|
|
|
checked={selectedDevices.includes(device.id)}
|
|
|
|
|
|
onCheckedChange={() => handleDeviceToggle(device.id)}
|
|
|
|
|
|
className="mt-1"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<div className="flex-1">
|
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
|
<span className="font-medium">{device.name}</span>
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<div
|
|
|
|
|
|
className={`w-16 h-6 flex items-center justify-center text-xs ${
|
|
|
|
|
|
device.status === "online"
|
|
|
|
|
|
? "bg-green-500 text-white"
|
|
|
|
|
|
: "bg-gray-200 text-gray-600"
|
|
|
|
|
|
}`}
|
|
|
|
|
|
>
|
|
|
|
|
|
{device.status === "online" ? "在线" : "离线"}
|
2025-07-10 15:34:23 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="text-sm text-gray-500 mt-1">
|
|
|
|
|
|
<div>IMEI: {device.imei}</div>
|
|
|
|
|
|
<div>微信号: {device.wechatId}</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</label>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2025-07-17 17:01:40 +08:00
|
|
|
|
</div>
|
2025-07-10 15:34:23 +08:00
|
|
|
|
|
|
|
|
|
|
<div className="flex items-center justify-between pt-4 border-t">
|
|
|
|
|
|
<div className="text-sm text-gray-500">
|
|
|
|
|
|
已选择 {selectedDevices.length} 个设备
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="flex space-x-2">
|
|
|
|
|
|
<Button variant="outline" onClick={() => setDialogOpen(false)}>
|
|
|
|
|
|
取消
|
|
|
|
|
|
</Button>
|
2025-07-16 15:18:54 +08:00
|
|
|
|
<Button onClick={() => setDialogOpen(false)}>确定</Button>
|
2025-07-10 15:34:23 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</DialogContent>
|
|
|
|
|
|
</Dialog>
|
|
|
|
|
|
</>
|
|
|
|
|
|
);
|
2025-07-16 15:18:54 +08:00
|
|
|
|
}
|