"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent } from "@/components/ui/card" import { Checkbox } from "@/components/ui/checkbox" import { Search } from "lucide-react" import { Input } from "@/components/ui/input" import { Database } from "lucide-react" import { api } from "@/lib/api" import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog" interface TrafficPool { id: string name: string count: number description: string } interface TrafficPoolStepProps { onSubmit: (data: any) => void onBack: () => void initialData?: any devices?: string[] } export default function TrafficPoolStep({ onSubmit, onBack, initialData = {}, devices = [] }: TrafficPoolStepProps) { const [selectedPools, setSelectedPools] = useState(initialData.selectedPools || []) const [searchInput, setSearchInput] = useState("") const [searchTerm, setSearchTerm] = useState("") const [isSubmitting, setIsSubmitting] = useState(false) const [deviceLabels, setDeviceLabels] = useState<{ label: string; count: number }[]>([]) const [dialogOpen, setDialogOpen] = useState(false) const [currentPage, setCurrentPage] = useState(1) const pageSize = 10 const [total, setTotal] = useState(0) const [loading, setLoading] = useState(false) const filteredPools = deviceLabels.filter( (pool) => pool.label && pool.label.toLowerCase().includes(searchTerm.toLowerCase()) ) const totalPages = Math.ceil(total / pageSize) const pagedPools = filteredPools.slice((currentPage - 1) * pageSize, currentPage * pageSize) // 监听 devices、currentPage、searchTerm 变化,请求标签(后端分页+搜索) useEffect(() => { if (!devices || devices.length === 0) { setDeviceLabels([]) setTotal(0) return } const fetchLabels = async () => { setLoading(true) try { const params = devices.join(",") const res = await api.get<{ code: number; msg: string; data: { list: { label: string; count: number }[]; total: number } }>(`/v1/workbench/device-labels?deviceIds=${params}&page=${currentPage}&pageSize=${pageSize}&keyword=${encodeURIComponent(searchTerm)}`) if (res.code === 200 && Array.isArray(res.data?.list)) { setDeviceLabels(res.data.list) setTotal(res.data.total || 0) } else { setDeviceLabels([]) setTotal(0) } } catch (e) { setDeviceLabels([]) setTotal(0) } finally { setLoading(false) } } fetchLabels() }, [devices, currentPage, searchTerm]) // 搜索时重置分页并触发搜索 const handleSearch = () => { setCurrentPage(1) setSearchTerm(searchInput) } // label 到描述的映射 const poolDescMap: Record = { "新客流量池": "新获取的客户流量", "高意向流量池": "有购买意向的客户", "复购流量池": "已购买过产品的客户", "活跃流量池": "近期活跃的客户", "沉睡流量池": "长期未活跃的客户", } const togglePool = (label: string) => { setSelectedPools((prev) => prev.includes(label) ? prev.filter((id) => id !== label) : [...prev, label] ) } const handleSubmit = async () => { setIsSubmitting(true) try { await new Promise((resolve) => setTimeout(resolve, 1000)) onSubmit({ poolIds: selectedPools }) } catch (error) { console.error("提交失败:", error) } finally { setIsSubmitting(false) } } // 每次弹窗打开时重置分页 useEffect(() => { if (dialogOpen) setCurrentPage(1) }, [dialogOpen]) return (

流量池选择

setDialogOpen(true)} />
选择流量池
{/* 搜索栏 */}
setSearchInput(e.target.value)} className="pl-10 rounded-lg border-gray-200 focus:border-blue-500 focus:ring-2 focus:ring-blue-100" />
{/* 流量池列表 */}
{loading ? (
加载中...
) : filteredPools.length === 0 ? (
暂无流量池
) : ( filteredPools.map((pool) => (
togglePool(pool.label)} >

{pool.label}

{poolDescMap[pool.label] || ""}

{pool.count} 人 { e.stopPropagation(); togglePool(pool.label); }} onClick={e => e.stopPropagation()} />
)) )}
{/* 分页按钮 */} {totalPages > 1 && (
第 {currentPage} / {totalPages} 页
)} {/* 确认按钮 */}
) }