<?php
require_once __DIR__ . '/../config.php';

// Handle AJAX cart actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    $pid    = (int)($_POST['pid'] ?? 0);
    $qty    = (int)($_POST['qty'] ?? 1);
    if ($action === 'add')    { cartAdd($pid, $qty);    }
    if ($action === 'update') { cartUpdate($pid, $qty); }
    if ($action === 'remove') { cartRemove($pid);       }
    // redirect back
    $ref = $_SERVER['HTTP_REFERER'] ?? SITE_URL . '/pages/cart.php';
    redirect($ref);
}

$items = cartItems();
$total = cartTotal();
$page_title = 'Shopping Cart | Balimeen Private Limited';
include __DIR__ . '/../includes/header.php';
?>
<div class="page-hero" style="padding:40px 0;">
  <div class="container">
    <h1 style="font-size:1.8rem;">🛒 Shopping Cart</h1>
    <div class="breadcrumb"><a href="<?= SITE_URL ?>">Home</a> → Cart</div>
  </div>
</div>

<div class="cart-page">
  <div class="container">
    <?php if (!$items): ?>
    <div class="cart-table-card">
      <div class="cart-empty">
        <i class="fas fa-shopping-cart"></i>
        <h3>Your cart is empty</h3>
        <p>Browse our premium stainless steel sinks and add them to your cart.</p>
        <a href="products.php" class="btn btn-primary">Browse Products</a>
      </div>
    </div>
    <?php else: ?>
    <div class="cart-grid">
      <!-- Items -->
      <div>
        <div class="cart-table-card">
          <div class="cart-head">
            <h2>Cart Items (<?= count($items) ?>)</h2>
            <form method="POST">
              <input type="hidden" name="action" value="remove">
              <input type="hidden" name="pid" value="0">
              <a href="?clear=1" style="font-size:.82rem;color:#ef4444;" onclick="return confirm('Clear entire cart?')">Clear All</a>
            </form>
          </div>
          <?php foreach ($items as $item): ?>
          <div class="cart-item">
            <img src="<?= SITE_URL ?>/assets/images/<?= h($item['image']) ?>" class="cart-item-img" alt="<?= h($item['name']) ?>">
            <div>
              <div class="cart-item-name"><?= h($item['name']) ?></div>
              <div class="cart-item-meta">Material: <?= h($item['material']) ?> &nbsp;·&nbsp; <?= h($item['dimensions']) ?></div>
            </div>
            <!-- Qty control -->
            <form method="POST" style="display:inline;">
              <input type="hidden" name="action" value="update">
              <input type="hidden" name="pid" value="<?= $item['id'] ?>">
              <div class="qty-control">
                <button type="submit" name="qty" value="<?= max(1,$item['qty']-1) ?>" class="qty-btn">−</button>
                <span class="qty-val"><?= $item['qty'] ?></span>
                <button type="submit" name="qty" value="<?= $item['qty']+1 ?>" class="qty-btn">+</button>
              </div>
            </form>
            <div class="cart-item-price"><?= rupee($item['price'] * $item['qty']) ?></div>
            <form method="POST" style="display:inline;">
              <input type="hidden" name="action" value="remove">
              <input type="hidden" name="pid" value="<?= $item['id'] ?>">
              <button type="submit" class="remove-btn" title="Remove">✕</button>
            </form>
          </div>
          <?php endforeach; ?>
        </div>
        <div style="margin-top:16px;display:flex;gap:12px;">
          <a href="products.php" class="btn btn-outline" style="color:var(--blue-main);border-color:var(--blue-main);">← Continue Shopping</a>
        </div>
      </div>

      <!-- Summary -->
      <div class="cart-summary">
        <h3>Order Summary</h3>
        <?php foreach ($items as $item): ?>
        <div class="summary-row">
          <span><?= h($item['name']) ?> ×<?= $item['qty'] ?></span>
          <span><?= rupee($item['price'] * $item['qty']) ?></span>
        </div>
        <?php endforeach; ?>
        <?php $shipping = $total >= 5000 ? 0 : 299; ?>
        <div class="summary-row"><span>Subtotal</span><span><?= rupee($total) ?></span></div>
        <div class="summary-row"><span>Shipping</span><span><?= $shipping == 0 ? '<span style="color:#10b981;font-weight:600;">FREE</span>' : rupee($shipping) ?></span></div>
        <div class="summary-row total"><span>Total</span><span><?= rupee($total + $shipping) ?></span></div>
        <p class="summary-note"><?= $shipping > 0 ? 'Add '.rupee(5000-$total).' more for FREE shipping!' : '🎉 You qualify for free shipping!' ?></p>
        <?php if (isCustomer()): ?>
        <a href="checkout.php" class="btn btn-primary" style="width:100%;justify-content:center;">Proceed to Checkout →</a>
        <?php else: ?>
        <a href="login.php?next=<?= urlencode(SITE_URL.'/pages/checkout.php') ?>" class="btn btn-primary" style="width:100%;justify-content:center;">Login to Checkout →</a>
        <p style="text-align:center;font-size:.8rem;margin-top:10px;color:#64748b;">New customer? <a href="register.php" style="color:#2557d6;">Create account</a></p>
        <?php endif; ?>
      </div>
    </div>
    <?php endif; ?>
  </div>
</div>

<?php
// Handle clear cart
if (isset($_GET['clear'])) { cartClear(); redirect(SITE_URL.'/pages/cart.php'); }
include __DIR__ . '/../includes/footer.php';
?>
