import React from 'react'; import { ShoppingCart, ShoppingBag, Trash2, RefreshCw, CreditCard, XCircle } from 'lucide-react'; import type { ModuleItem, Currency, UserProfile, VerificationResult } from '../../types'; interface CartDrawerProps { cart: ModuleItem[]; currency: Currency; setCurrency: (c: Currency) => void; toggleCartItem: (module: ModuleItem) => void; couponCode: string; setCouponCode: (code: string) => void; appliedCoupon: string | null; applyPromoCode: () => void; removePromoCode: () => void; pricingCalc: any; loadingPricing: boolean; loggedInUser: UserProfile | null; onUpdateBillingPhone: () => void; verificationResult: VerificationResult | null; checkoutLoading: boolean; handleCheckout: () => void; } export const CartDrawer: React.FC = ({ cart, currency, setCurrency, toggleCartItem, couponCode, setCouponCode, appliedCoupon, applyPromoCode, removePromoCode, pricingCalc, loadingPricing, loggedInUser, onUpdateBillingPhone, verificationResult, checkoutLoading, handleCheckout, }) => { const fallbackCalc = cart.length > 0 ? (() => { const subtotal = cart.reduce((acc, item) => { const price = currency === 'INR' ? item.priceInr : currency === 'EUR' ? item.priceEur : item.priceUsd; return acc + (Number(price) || 0); }, 0); return { currency, base_subtotal: subtotal, bundle_discount_amount: 0, coupon_discount_amount: 0, total_discounts: 0, convenience_fee_rate: 0, convenience_fee: 0, taxable_amount: 0, gst_amount: 0, grand_total: subtotal, discount_policy: 'None', coupon_applied: 'None', coupon_error: null, }; })() : null; const activePricing = (pricingCalc && pricingCalc.currency === currency) ? pricingCalc : fallbackCalc; return (

Cart Summary

{cart.length} {cart.length === 1 ? 'Item' : 'Items'}
{/* Currency selector toggle */}
Billing Currency:
{/* Selected modules list */} {cart.length === 0 ? (

Your checkout cart is empty. Select from curriculum.

) : (
{cart.map((item) => (

{item.name}

{item.category}

{(() => { const calculatedItem = activePricing?.cart_items?.find((ci: any) => ci.code === item.code); const price = calculatedItem ? calculatedItem.price : (currency === 'INR' ? item.priceInr : currency === 'EUR' ? item.priceEur : item.priceUsd); const symbol = currency === 'INR' ? '₹' : currency === 'EUR' ? '€' : '$'; return `${symbol}${price}`; })()}
))}
)} {/* Coupon Code Input */} {cart.length > 0 && (
setCouponCode(e.target.value)} placeholder="e.g. AGENT40, EVENT_NEW" className="flex-1 bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 placeholder:text-slate-400 uppercase" />
{appliedCoupon && (pricingCalc?.coupon_error || !pricingCalc?.coupon_applied || pricingCalc?.coupon_applied === 'None' || (pricingCalc?.coupon_discount_amount !== undefined && pricingCalc?.coupon_discount_amount <= 0) ? (
⚠️ {pricingCalc?.coupon_error || 'Invalid or expired coupon code.'}
) : (
Coupon Active: {appliedCoupon} ✓{' '} {pricingCalc?.coupon_applied && pricingCalc.coupon_applied !== 'None' ? `(${pricingCalc.coupon_applied.replace(/Code: \w+ - /, '')})` : ''}
))}
)} {/* Checkout Summary Calculations */} {loadingPricing && (!pricingCalc || pricingCalc.currency !== currency) && (
Calculating {currency} pricing...
)} {activePricing && cart.length > 0 && (
Base Subtotal {currency === 'INR' ? `₹${activePricing.base_subtotal}` : currency === 'EUR' ? `€${activePricing.base_subtotal}` : `$${activePricing.base_subtotal}`}
{activePricing.renewal_discount_amount > 0 && (
Early Renewal Discount (20%) -{currency === 'INR' ? `₹${activePricing.renewal_discount_amount}` : currency === 'EUR' ? `€${activePricing.renewal_discount_amount}` : `$${activePricing.renewal_discount_amount}`}
)} {activePricing.bundle_discount_amount > 0 && (
{activePricing.discount_policy && activePricing.discount_policy !== 'None' ? (activePricing.discount_policy.startsWith('Staggered Buy:') ? activePricing.discount_policy.replace('Staggered Buy:', 'Loyalty Discount:') : activePricing.discount_policy) : 'Loyalty Discount'} -{currency === 'INR' ? `₹${activePricing.bundle_discount_amount}` : currency === 'EUR' ? `€${activePricing.bundle_discount_amount}` : `$${activePricing.bundle_discount_amount}`}
)} {activePricing.coupon_discount_amount > 0 && (
Coupon Discount ({appliedCoupon}) -{currency === 'INR' ? `₹${activePricing.coupon_discount_amount}` : currency === 'EUR' ? `€${activePricing.coupon_discount_amount}` : `$${activePricing.coupon_discount_amount}`}
)} {activePricing.total_discounts > 0 && !activePricing.renewal_discount_amount && !activePricing.bundle_discount_amount && !activePricing.coupon_discount_amount && (
Total Discounts -{currency === 'INR' ? `₹${activePricing.total_discounts}` : currency === 'EUR' ? `€${activePricing.total_discounts}` : `$${activePricing.total_discounts}`}
)} {activePricing.convenience_fee > 0 && (
Gateway fee ({activePricing.convenience_fee_rate}%) {currency === 'INR' ? `₹${activePricing.convenience_fee}` : currency === 'EUR' ? `€${activePricing.convenience_fee}` : `$${activePricing.convenience_fee}`}
)} {activePricing.gst_amount > 0 && (
GST (18%) {currency === 'INR' ? `₹${activePricing.gst_amount}` : currency === 'EUR' ? `€${activePricing.gst_amount}` : `$${activePricing.gst_amount}`}
(Taxable Amount:{' '} {currency === 'INR' ? `₹${activePricing.taxable_amount}` : currency === 'EUR' ? `€${activePricing.taxable_amount}` : `$${activePricing.taxable_amount}`} )
)}
Total Chargeable {currency === 'INR' ? `₹${activePricing.grand_total}` : currency === 'EUR' ? `€${activePricing.grand_total}` : `$${activePricing.grand_total}`}
)} {/* Billing Details Prefill Summary */} {loggedInUser && cart.length > 0 && (
Billing Profile
Email: {loggedInUser.email}
Phone: {loggedInUser.phone_number || ( Required for checkout )}
)} {/* Verification failure result badge */} {verificationResult && verificationResult.status === 'error' && (

Verification Failed

{verificationResult.message}

)} {/* Checkout Trigger */} {cart.length > 0 && (

* Integrated with Razorpay Gateway API (Test Mode).

)}
); };