<?php
function product_name($p,$lang){ $k='name_'.$lang; return isset($p[$k]) && $p[$k] ? $p[$k] : $p['name_en']; }
function product_desc($p,$lang){ $k='description_'.$lang; return isset($p[$k]) && $p[$k] ? $p[$k] : $p['description_en']; }
function product_final_price($p){ $price=(float)$p['price']; $d=(float)$p['discount_percent']; return $d>0 ? $price-($price*$d/100) : $price; }
function render_product_card($conn,$p,$lang){
  $pid=(int)$p['id'];
  $img=first_image($conn,$pid);
  $brand=isset($p['brand_name'])?$p['brand_name']:'';
  $title=product_name($p,$lang);
  $discount=(float)$p['discount_percent'];
?>
<article class="p-card electro-product-card">
  <?php if($discount>0): ?><span class="save-badge"><?php echo e(t('save')); ?> <?php echo (int)$discount; ?>%</span><?php endif; ?>
  <a href="product.php?id=<?php echo $pid; ?>&lang=<?php echo e($lang); ?>" class="p-logo"><?php echo e($brand?$brand:'Brand'); ?></a>
  <a href="product.php?id=<?php echo $pid; ?>&lang=<?php echo e($lang); ?>" class="p-img">
    <img <?php echo img_attrs($img,360,360,'contain'); ?> alt="<?php echo e($title); ?>" width="360" height="360" onerror="this.onerror=null;this.src='assets/images/products/no-image.svg'">
  </a>
  <a href="product.php?id=<?php echo $pid; ?>&lang=<?php echo e($lang); ?>" class="p-title"><?php echo e($title); ?></a>
  <div class="price-area">
    <?php if((float)$p['old_price']>0): ?><div class="old-price"><?php echo money_iqd($p['old_price']); ?></div><?php endif; ?>
    <div class="new-price"><?php echo money_iqd(product_final_price($p)); ?></div>
  </div>
  <button type="button" class="add-btn add-to-cart-btn" data-id="<?php echo $pid; ?>"><?php echo e(t('add_to_cart')); ?></button>
</article>
<?php }
function render_wide_product($conn,$p,$lang){
  $pid=(int)$p['id']; $img=first_image($conn,$pid); $desc=strip_tags(product_desc($p,$lang));
  if(function_exists('mb_substr')) $desc=mb_substr($desc,0,650,'UTF-8'); else $desc=substr($desc,0,650);
?>
<article class="wide-card">
  <div class="wide-top"><span class="new-label"><?php echo e(t('new_reached')); ?></span><span><?php echo e(isset($p['brand_name'])?$p['brand_name']:''); ?></span></div>
  <div class="wide-body">
    <a class="wide-img" href="product.php?id=<?php echo $pid; ?>&lang=<?php echo e($lang); ?>"><img <?php echo img_attrs($img,460,360,'contain'); ?> alt="<?php echo e(product_name($p,$lang)); ?>" width="460" height="360"></a>
    <div class="wide-info"><a class="wide-title" href="product.php?id=<?php echo $pid; ?>&lang=<?php echo e($lang); ?>"><?php echo e(product_name($p,$lang)); ?></a><p><?php echo e($desc); ?></p><div class="wide-price"><strong><?php echo money_iqd(product_final_price($p)); ?></strong><?php if((float)$p['old_price']>0): ?><span><?php echo money_iqd($p['old_price']); ?></span><?php endif; ?></div><button type="button" class="add-btn add-to-cart-btn" data-id="<?php echo $pid; ?>"><?php echo e(t('add_to_cart')); ?></button></div>
  </div>
</article>
<?php }
?>
