'use client' import Link from 'next/link' import Image from 'next/image' import { useState } from 'react' import { ShoppingCart, Check } from 'lucide-react' interface Component { id: string name: string brand: string model: string price: number description?: string | null imageUrl?: string | null stock: number componentType?: { id: string name: string } } interface ComponentCardProps { component: Component } export function ComponentCard({ component }: ComponentCardProps) { const [isAdding, setIsAdding] = useState(false) const [isAdded, setIsAdded] = useState(false) const addToCart = async (e: React.MouseEvent) => { e.preventDefault() e.stopPropagation() if (component.stock <= 0 || isAdding) return const token = localStorage.getItem('token') if (!token) { alert('请先登录') return } setIsAdding(true) try { const response = await fetch('/api/cart', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify({ componentId: component.id, quantity: 1 }) }) if (response.ok) { setIsAdded(true) setTimeout(() => setIsAdded(false), 2000) } else if (response.status === 401) { localStorage.removeItem('token') localStorage.removeItem('user') alert('登录已过期,请重新登录') window.location.href = '/login' } else { const data = await response.json() alert(data.message || '添加失败') } } catch (error) { console.error('添加到购物车失败:', error) alert('添加失败') } finally { setIsAdding(false) } } return (
{component.imageUrl ? ( {component.name} ) : (
📦
)}
{component.brand}

{component.name}

{component.description && (

{component.description}

)}
¥{component.price.toLocaleString()} 0 ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }`}> {component.stock > 0 ? `库存 ${component.stock}` : '缺货'}
) }