"use client" import type React from "react" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Alert, AlertDescription } from "@/components/ui/alert" import { AlertCircle, Info, Plus, Minus, User, Users, X, Search } from "lucide-react" import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip" import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" import { Badge } from "@/components/ui/badge" import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog" import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar" import { ScrollArea } from "@/components/ui/scroll-area" import { Checkbox } from "@/components/ui/checkbox" interface WechatFriend { id: string nickname: string wechatId: string avatar: string tags: string[] } interface GroupSettingsProps { onNext: () => void initialValues?: { name: string fixedWechatIds: string[] groupingOption: "all" | "fixed" fixedGroupCount: number } onValuesChange: (values: { name: string fixedWechatIds: string[] groupingOption: "all" | "fixed" fixedGroupCount: number }) => void } export function GroupSettings({ onNext, initialValues, onValuesChange }: GroupSettingsProps) { const [name, setName] = useState(initialValues?.name || "新建群计划") const [fixedWechatIds, setFixedWechatIds] = useState(initialValues?.fixedWechatIds || []) const [newWechatId, setNewWechatId] = useState("") const [groupingOption, setGroupingOption] = useState<"all" | "fixed">(initialValues?.groupingOption || "all") const [fixedGroupCount, setFixedGroupCount] = useState(initialValues?.fixedGroupCount || 5) const [error, setError] = useState(null) const [warning, setWarning] = useState(null) const [friendSelectorOpen, setFriendSelectorOpen] = useState(false) const [searchQuery, setSearchQuery] = useState("") const [friends, setFriends] = useState([]) const [loading, setLoading] = useState(false) const [selectedFriends, setSelectedFriends] = useState([]) // 微信群人数固定为38人 const GROUP_SIZE = 38 // 系统建议的最大群组数 const RECOMMENDED_MAX_GROUPS = 20 // 最多可选择的微信号数量 const MAX_WECHAT_IDS = 5 // 只在组件挂载时执行一次初始验证 useEffect(() => { validateSettings() fetchFriends() }, []) // 当值变化时,通知父组件 useEffect(() => { const timer = setTimeout(() => { onValuesChange({ name, fixedWechatIds, groupingOption, fixedGroupCount, }) }, 300) return () => clearTimeout(timer) }, [name, fixedWechatIds, groupingOption, fixedGroupCount, onValuesChange]) const fetchFriends = async () => { setLoading(true) // 模拟从API获取好友列表 await new Promise((resolve) => setTimeout(resolve, 1000)) const mockFriends = Array.from({ length: 20 }, (_, i) => ({ id: `friend-${i}`, nickname: `好友${i + 1}`, wechatId: `wxid_${Math.random().toString(36).substring(2, 8)}`, avatar: `/placeholder.svg?height=40&width=40&text=${i + 1}`, tags: i % 3 === 0 ? ["重要客户"] : i % 3 === 1 ? ["潜在客户", "已沟通"] : ["新客户"], })) setFriends(mockFriends) setLoading(false) } const validateSettings = () => { setError(null) setWarning(null) if (!name.trim()) { setError("计划名称不能为空") return false } if (fixedWechatIds.length === 0) { setError("请至少添加一个固定微信号") return false } if (groupingOption === "fixed") { if (fixedGroupCount <= 0) { setError("群组数必须大于0") return false } if (fixedGroupCount > RECOMMENDED_MAX_GROUPS) { setWarning(`创建${fixedGroupCount}个群可能会消耗较多资源,建议减少群组数量`) } } return true } const handleNext = () => { if (validateSettings()) { onNext() } } const adjustGroupCount = (delta: number) => { setFixedGroupCount((prev) => { const newValue = Math.max(1, prev + delta) return newValue }) } const handleNameChange = (e: React.ChangeEvent) => { setName(e.target.value) } const handleNewWechatIdChange = (e: React.ChangeEvent) => { setNewWechatId(e.target.value) } const handleGroupCountChange = (e: React.ChangeEvent) => { const value = Number.parseInt(e.target.value) || 0 setFixedGroupCount(value) } const handleGroupingOptionChange = (value: string) => { setGroupingOption(value as "all" | "fixed") } const addWechatId = () => { if (!newWechatId.trim()) return if (fixedWechatIds.includes(newWechatId.trim())) { setError("该微信号已添加") return } if (fixedWechatIds.length >= MAX_WECHAT_IDS) { setError(`最多只能添加${MAX_WECHAT_IDS}个微信号`) return } setFixedWechatIds((prev) => [...prev, newWechatId.trim()]) setNewWechatId("") setError(null) } const removeWechatId = (id: string) => { setFixedWechatIds((prev) => prev.filter((wid) => wid !== id)) } const openFriendSelector = () => { if (fixedWechatIds.length >= MAX_WECHAT_IDS) { setError(`最多只能添加${MAX_WECHAT_IDS}个微信号`) return } setFriendSelectorOpen(true) } const handleFriendSelection = () => { const newIds = selectedFriends.map((f) => f.wechatId).filter((id) => !fixedWechatIds.includes(id)) const combinedIds = [...fixedWechatIds, ...newIds] if (combinedIds.length > MAX_WECHAT_IDS) { setError(`最多只能添加${MAX_WECHAT_IDS}个微信号,已自动截取前${MAX_WECHAT_IDS}个`) setFixedWechatIds(combinedIds.slice(0, MAX_WECHAT_IDS)) } else { setFixedWechatIds(combinedIds) } setFriendSelectorOpen(false) setSelectedFriends([]) } const toggleFriendSelection = (friend: WechatFriend) => { setSelectedFriends((prev) => { const isSelected = prev.some((f) => f.id === friend.id) if (isSelected) { return prev.filter((f) => f.id !== friend.id) } else { return [...prev, friend] } }) } const filteredFriends = friends.filter( (friend) => friend.nickname.toLowerCase().includes(searchQuery.toLowerCase()) || friend.wechatId.toLowerCase().includes(searchQuery.toLowerCase()), ) // 计算总人数 const totalMembers = groupingOption === "fixed" ? fixedGroupCount * GROUP_SIZE : "根据好友总数自动计算" return (
{ if (e.key === "Enter") { e.preventDefault() addWechatId() } }} />
{fixedWechatIds.length > 0 && (
{fixedWechatIds.map((id) => ( {id} ))}
)}
已添加 {fixedWechatIds.length}/{MAX_WECHAT_IDS} 个微信号

系统将根据好友总数自动计算需要创建的群数量

手动指定需要创建的群数量

{groupingOption === "fixed" && (
)}
群配置信息
每个群人数: {GROUP_SIZE} 人
预计总人数: {totalMembers}
{error && ( {error} )} {warning && ( {warning} )}
选择微信好友
setSearchQuery(e.target.value)} className="pl-9" />
{loading ? (
加载中...
) : filteredFriends.length === 0 ? (
未找到匹配的好友
) : ( filteredFriends.map((friend) => (
toggleFriendSelection(friend)} > f.id === friend.id)} onCheckedChange={() => toggleFriendSelection(friend)} /> {friend.nickname[0]}
{friend.nickname}
{friend.wechatId}
{friend.tags.map((tag) => ( {tag} ))}
)) )}
) }