<?php
/**
 * Product Card Component with Discount Support
 * مكون كارت المنتج مع دعم الخصومات
 * 
 * Usage:
 * include 'includes/product-card.php';
 * renderProductCard($product, $currency_symbol);
 */

function renderProductCard($product, $currency_symbol = 'ج.م') {
    $image = !empty($product['image']) ? $product['image'] : 'https://via.placeholder.com/300';
    $hasDiscount = !empty($product['discount_price']) && $product['discount_price'] < $product['price'];
    $discountPercent = $hasDiscount ? round((($product['price'] - $product['discount_price']) / $product['price']) * 100) : 0;
    ?>
    
    <div class="product-card" onclick="window.location.href='product.php?id=<?php echo $product['id']; ?>'">
        <?php if ($hasDiscount): ?>
            <div class="product-card-badge">-<?php echo $discountPercent; ?>%</div>
        <?php endif; ?>
        
        <img src="<?php echo htmlspecialchars($image); ?>" 
             alt="<?php echo htmlspecialchars($product['name']); ?>" 
             class="product-image">
        
        <div class="product-info">
            <h3 class="product-name"><?php echo htmlspecialchars($product['name']); ?></h3>
            
            <?php if ($hasDiscount): ?>
                <div class="price-container">
                    <span class="original-price"><?php echo $currency_symbol; ?> <?php echo number_format($product['price'], 0); ?></span>
                    <span class="discount-price"><?php echo $currency_symbol; ?> <?php echo number_format($product['discount_price'], 0); ?></span>
                </div>
            <?php else: ?>
                <div class="product-price"><?php echo $currency_symbol; ?> <?php echo number_format($product['price'], 0); ?></div>
            <?php endif; ?>
            
            <button class="btn-add-to-cart" onclick="event.stopPropagation(); addToCart(<?php echo $product['id']; ?>)">
                <i class="fas fa-shopping-cart"></i>
                أضف للسلة
            </button>
        </div>
    </div>
    
    <?php
}
?>