import React, { useState } from 'react' import { FaPlus, FaMinus } from 'react-icons/fa' import { BillingCycle, ProductDto } from '@/proxy/order/models' import { BasketData } from '@/utils/basketUtils' import { useLocalization } from '@/utils/hooks/useLocalization' import { Button } from '@/components/ui' interface ProductCardProps { product: ProductDto globalBillingCycle: BillingCycle globalPeriod: number addItem: (product: ProductDto, quantity: number, billingCycle: BillingCycle) => void basketData: BasketData } export const ProductCard: React.FC = ({ product, globalBillingCycle, globalPeriod, addItem, basketData, }) => { const [quantity, setQuantity] = useState(1) const { translate } = useLocalization() const isInBasket = basketData.items.some( (item) => item.product.id === product.id && item.billingCycle === globalBillingCycle, ) const formatPrice = (price: number) => { return new Intl.NumberFormat('tr-TR', { style: 'currency', currency: 'TRY', minimumFractionDigits: 0, }).format(price) } const getCurrentPrice = () => { return globalBillingCycle === 'monthly' ? product.monthlyPrice! : product.yearlyPrice! } const getVatRate = () => product.vatRate ?? 0.2 const getTotalPrice = () => { return getCurrentPrice() * quantity * globalPeriod } const getVatAmount = () => { return getTotalPrice() * getVatRate() } const getTotalPriceWithVat = () => { return getTotalPrice() + getVatAmount() } const handleAddToBasket = () => { if (!product.isQuantityBased && isInBasket) { return } const quantityToAdd = !product.isQuantityBased ? 1 : quantity addItem(product, quantityToAdd, globalBillingCycle) setQuantity(1) } const getUnitText = () => { switch (product.unit) { case 'monthly': return globalBillingCycle === 'monthly' ? translate('::App.PublicProducts.MonthPeriod') : translate('::App.PublicProducts.YearPeriod') case 'yearly': return translate('::App.PublicProducts.YearPeriod') default: return '' } } const getPeriodText = () => { const periodText = globalBillingCycle === 'monthly' ? translate('::App.Platform.Months') : translate('::App.Platform.Years') return globalPeriod > 1 ? ` (${globalPeriod} ${periodText})` : '' } const hasValidPrice = () => { if (globalBillingCycle === 'monthly' && !product.monthlyPrice) return false if (globalBillingCycle === 'yearly' && !product.yearlyPrice) return false return true } // If product doesn't have valid price, don't render it at all if (!hasValidPrice()) { return null } return (
{product.imageUrl && (
{product.name}
)}
{translate('::' + product.category)}

{translate('::' + product.name)}

{translate('::' + product.description)}

{/* Quantity and Yearly Savings above price */}
{product.isQuantityBased && (
{translate('::App.PublicProducts.Quantity')}
)} {globalBillingCycle === 'yearly' && product.monthlyPrice && product.yearlyPrice && (
{translate('::App.ProductsSavings.Yearly', { percent: Math.round((1 - product.yearlyPrice / (product.monthlyPrice * 12)) * 100), })}
)}
{/* Bottom section with consistent height */}
{formatPrice(getCurrentPrice())} {getUnitText()}
{translate('::App.PublicProducts.Kdv')} (%{(getVatRate() * 100).toFixed(0)}):{' '} {formatPrice(getVatAmount())}
{globalPeriod > 1 && (
{translate('::App.Listform.ListformField.Total')}{' '} {formatPrice(getTotalPriceWithVat())} {getPeriodText()}
)} {globalPeriod <= 1 && (
{translate('::App.PublicProducts.Kdvdahil')} {formatPrice(getTotalPriceWithVat())}
)}
{(() => { const isNonQuantityProduct = !product.isQuantityBased const isDisabled = isNonQuantityProduct && isInBasket return ( ) })()}
) }