import { X, Star, Minus, Plus, Phone } from 'lucide-react'; import { useState } from 'react'; import { formatRupiah } from '@/lib/rupiah'; import type { Product, ProductVariant, ProductPrice } from '@/types/homepage'; type ProductModalProps = { product: Product | null; onClose: () => void; }; function getProductImage(): string { return 'https://images.unsplash.com/photo-1490481651871-ab68de25d43d?w=1000&auto=format&fit=crop&q=80'; } function getRetailPrice(variant: ProductVariant): number { const retail = variant.product_prices.find((p) => p.type === 'retail'); return retail?.price ?? 0; } function getWholesalePrice(variant: ProductVariant): number { const wholesale = variant.product_prices.find((p) => p.type === 'wholesale'); return wholesale?.price ?? 0; } export default function ProductModal({ product, onClose }: ProductModalProps) { const [selectedVariant, setSelectedVariant] = useState(null); const [selectedPriceType, setSelectedPriceType] = useState<'retail' | 'wholesale'>('retail'); const [quantity, setQuantity] = useState(1); if (!product) return null; const variants = product.product_variants; const activeVariant = selectedVariant ?? variants[0]; const retailPrice = activeVariant ? getRetailPrice(activeVariant) : 0; const wholesalePrice = activeVariant ? getWholesalePrice(activeVariant) : 0; const currentPrice = selectedPriceType === 'retail' ? retailPrice : wholesalePrice; const maxStock = activeVariant?.stock ?? 0; const handleWhatsAppOrder = () => { if (!activeVariant) return; const priceLabel = selectedPriceType === 'retail' ? 'Retail' : 'Grosir'; const message = `Halo, saya tertarik dengan produk:\n\n*${product.name}*\n- Varian: ${activeVariant.name}\n- Harga ${priceLabel}: Rp ${formatRupiah(currentPrice)}\n- Jumlah: ${quantity} pcs\n- Subtotal: Rp ${formatRupiah(currentPrice * quantity)}\n\nMohon info detail pesanan. Terima kasih!`; const phone = '6281234567890'; const waUrl = `https://wa.me/${phone}?text=${encodeURIComponent(message)}`; window.open(waUrl, '_blank'); }; return (
e.stopPropagation()} > {/* Header */}

Detail Produk

{/* Image */}
{product.name}
{/* Details */}
{product.categories.slice(0, 2).map((cat) => ( {cat.name} ))}

{product.name}

{product.description || 'Produk fashion berkualitas dengan desain elegan dan bahan nyaman.'}

{/* Rating */}
5.0 (120+)
{/* Price */}

Harga

Rp {formatRupiah(currentPrice)}

{selectedPriceType === 'wholesale' && wholesalePrice > 0 && (

Harga retail: Rp {formatRupiah(retailPrice)}

)}
{/* Variant Selection */} {variants.length > 0 && (

Varian

{variants.map((variant) => ( ))}
)} {/* Price Type */}

Jenis Harga

{/* Quantity */}

Jumlah

{quantity}

Stok tersedia: {maxStock}

{/* Total */}
Subtotal Rp {formatRupiah(currentPrice * quantity)}
{/* WhatsApp Order */}
); }