<?php
require_once 'includes/bootstrap.php';
require_once 'includes/product-card.php';
if(isset($_GET['remove'])){ unset($_SESSION['cart'][(string)(int)$_GET['remove']]); redirect('cart.php?lang='.$lang); }
if(isset($_POST['update_cart']) && isset($_POST['qty']) && is_array($_POST['qty'])){
  foreach($_POST['qty'] as $pid=>$qty){ $pid=(int)$pid; $qty=(int)$qty; if($qty<=0) unset($_SESSION['cart'][(string)$pid]); else $_SESSION['cart'][(string)$pid]=$qty; }
  redirect('cart.php?lang='.$lang);
}
include 'includes/header.php';
$grand=0;
$items=array();
if(isset($_SESSION['cart']) && is_array($_SESSION['cart'])){
  foreach($_SESSION['cart'] as $pid=>$qty){
    $pid=(int)$pid; $qty=(int)$qty;
    if($qty<=0) continue;
    $q=mysqli_query($conn,"SELECT p.*,b.name brand_name FROM products p LEFT JOIN brands b ON p.brand_id=b.id WHERE p.id=$pid AND p.status=1 LIMIT 1");
    if($q && mysqli_num_rows($q)){
      $p=mysqli_fetch_assoc($q); $price=product_final_price($p); $line=$price*$qty; $grand+=$line; $items[]=array('p'=>$p,'qty'=>$qty,'price'=>$price,'line'=>$line);
    }
  }
}
?>
<main class="container section cart-page">
  <h2><?php echo e(t('cart')); ?></h2>
  <?php if(empty($items)): ?>
    <div class="empty-cart-card"><i class="fa-solid fa-bag-shopping"></i><h3><?php echo e(t('empty_cart')); ?></h3><a class="primary-btn" href="index.php?lang=<?php echo e($lang); ?>"><?php echo e(t('continue_shopping')); ?></a></div>
  <?php else: ?>
    <form method="post" class="cart-layout">
      <div class="cart-items">
        <?php foreach($items as $it): $p=$it['p']; $pid=(int)$p['id']; $img=first_image($conn,$pid); ?>
          <div class="cart-item">
            <a class="cart-img" href="product.php?id=<?php echo $pid; ?>&lang=<?php echo e($lang); ?>"><img <?php echo img_attrs($img,160,160,'contain'); ?> alt=""></a>
            <div class="cart-info"><strong><?php echo e(product_name($p,$lang)); ?></strong><span><?php echo e(isset($p['brand_name'])?$p['brand_name']:''); ?></span><a class="remove-link" href="cart.php?remove=<?php echo $pid; ?>&lang=<?php echo e($lang); ?>"><?php echo e(t('remove')); ?></a></div>
            <div class="cart-price"><?php echo money_iqd($it['price']); ?></div>
            <input class="qty-input" name="qty[<?php echo $pid; ?>]" type="number" min="1" value="<?php echo (int)$it['qty']; ?>">
            <div class="cart-line"><?php echo money_iqd($it['line']); ?></div>
          </div>
        <?php endforeach; ?>
      </div>
      <aside class="cart-summary">
        <h3><?php echo e(t('total')); ?></h3>
        <div class="summary-row"><span><?php echo e(t('subtotal')); ?></span><strong><?php echo money_iqd($grand); ?></strong></div>
        <button class="primary-btn" name="update_cart" value="1" type="submit"><?php echo is_rtl($lang)?'تحديث السلة':'Update cart'; ?></button>
        <a class="primary-btn checkout-btn" href="checkout.php?lang=<?php echo e($lang); ?>"><?php echo e(t('checkout')); ?></a>
      </aside>
    </form>
  <?php endif; ?>
</main>
<?php include 'includes/footer.php'; ?>
