"use client"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card, CardContent } from "@/components/ui/card"; import { Trash, PlusCircle, Info } from "lucide-react"; import { useState } from "react"; interface PricingTiersProps { pricing: any[]; handleTierChange: ( e: React.ChangeEvent, index: number ) => void; handleRemoveTier: (index: number) => void; handleAddTier: () => void; } export const PricingTiers = ({ pricing, handleTierChange, handleRemoveTier, handleAddTier, }: PricingTiersProps) => { const [showHelp, setShowHelp] = useState(false); const formatNumber = (num: number) => { if (num === 0) return ""; return num % 1 === 0 ? num.toString() : num.toFixed(2); }; const calculateTotal = (quantity: number, pricePerUnit: number) => { return (quantity * pricePerUnit).toFixed(2); }; const validateTier = (tier: any, index: number) => { const errors = []; if (tier.minQuantity <= 0) { errors.push("Quantity must be greater than 0"); } if (tier.pricePerUnit <= 0) { errors.push("Price must be greater than 0"); } // Check for duplicate quantities const duplicateIndex = pricing.findIndex((p, i) => i !== index && p.minQuantity === tier.minQuantity ); if (duplicateIndex !== -1) { errors.push("Duplicate quantity found"); } return errors; }; const sortedPricing = [...pricing].sort((a, b) => a.minQuantity - b.minQuantity); return (

Pricing Tiers

{showHelp && (

How it works: Set different prices based on quantity. For example: 1-10 units at £5 each, 11-50 units at £4 each, 51+ units at £3 each. Quantities should be in ascending order.

)} {pricing?.length > 0 ? (
{sortedPricing.map((tier, sortedIndex) => { const originalIndex = pricing.findIndex(p => p === tier); const errors = validateTier(tier, originalIndex); const total = tier.minQuantity && tier.pricePerUnit ? calculateTotal(tier.minQuantity, tier.pricePerUnit) : "0.00"; return ( 0 ? 'border-red-200 bg-red-50' : 'border-gray-200'}`}>
handleTierChange(e, originalIndex)} className={`h-10 ${errors.some(e => e.includes('Quantity')) ? 'border-red-500' : ''}`} /> {errors.some(e => e.includes('Quantity')) && (

{errors.find(e => e.includes('Quantity'))}

)}
handleTierChange(e, originalIndex)} className={`h-10 ${errors.some(e => e.includes('Price')) ? 'border-red-500' : ''}`} /> {errors.some(e => e.includes('Price')) && (

{errors.find(e => e.includes('Price'))}

)}
£{total}

{tier.minQuantity} × £{formatNumber(tier.pricePerUnit)}

{errors.some(e => e.includes('Duplicate')) && (

⚠️ This quantity is already used in another tier

)}
); })}
) : (

No pricing tiers added yet

Add your first tier to get started

)}
); };