<?php
require_once __DIR__ . '/../app/bootstrap.php';
use App\Core\Cart;
if($_SERVER['REQUEST_METHOD']==='POST'){
 if(!verify_csrf())set_flash('error','Invalid security token.');else foreach(($_POST['qty']??[]) as $key=>$qty)Cart::update((string)$key,(int)$qty);
 set_flash('success','Cart updated.');redirect('public/cart.php');
}
$cart=Cart::summary();$pageTitle='Cart | '.BRAND_NAME;include ROOT_PATH.'/app/Layouts/frontend_header.php';
?>
<section class="container py-5"><h1 class="section-title mb-4">Shopping Cart</h1><?php if($m=flash('success')): ?><div class="alert alert-success"><?= e($m) ?></div><?php endif; ?><?php if($m=flash('error')): ?><div class="alert alert-danger"><?= e($m) ?></div><?php endif; ?><form method="post"><?= csrf_field() ?><div class="table-card"><div class="table-responsive"><table class="table align-middle mb-0"><thead><tr><th>Product</th><th>Price</th><th width="140">Qty</th><th>Total</th><th></th></tr></thead><tbody><?php if(!$cart['items']): ?><?= table_empty(5,'Cart is empty.') ?><?php endif; ?><?php foreach($cart['items'] as $key=>$item): $p=$item['product']; ?><tr><td><a href="<?= url('product/'.$p['slug']) ?>"><?= e($p['name']) ?></a><div class="small text-muted"><?= e($item['variant']['variant_name']??$p['sku']) ?></div></td><td><?= money($item['unit_price']) ?></td><td><input class="form-control" type="number" min="0" max="<?= (int)($item['variant']['stock']??$p['stock']) ?>" name="qty[<?= e($key) ?>]" value="<?= (int)$item['qty'] ?>"></td><td><?= money($item['line_total']) ?></td><td><button type="button" class="btn btn-sm btn-outline-danger" onclick="document.querySelector('[name=&quot;qty[<?= e($key) ?>]&quot;]').value=0;this.form.submit()">Remove</button></td></tr><?php endforeach; ?></tbody></table></div></div><div class="d-flex flex-wrap gap-3 justify-content-between align-items-center mt-4"><h4>Subtotal: <?= money($cart['subtotal']) ?></h4><div><button class="btn btn-outline-primary">Update Cart</button> <a class="btn btn-primary <?= !$cart['items']?'disabled':'' ?>" href="<?= url('public/checkout.php') ?>">Checkout</a></div></div></form></section><?php include ROOT_PATH.'/app/Layouts/frontend_footer.php'; ?>
