<?php
require_once __DIR__ . '/../app/bootstrap.php';
use App\Core\Auth; use App\Core\Database;
Auth::requireLogin('public/login.php');
$products=Database::safeFetchAll('SELECT p.*,b.name brand_name,v.shop_name FROM compares c JOIN products p ON p.id=c.product_id LEFT JOIN brands b ON b.id=p.brand_id LEFT JOIN vendors v ON v.id=p.vendor_id WHERE c.user_id=? AND p.status="published" ORDER BY c.id DESC',[Auth::id()]);
$pageTitle='Compare | '.BRAND_NAME; require ROOT_PATH.'/app/Layouts/frontend_header.php';
?>
<section class="container py-5"><h1>Compare Products</h1><p class="text-muted">Compare up to four products.</p><div class="table-responsive"><table class="table table-bordered align-middle"><thead><tr><th>Feature</th><?php foreach($products as $p): ?><th class="text-center"><img src="<?= e(product_image($p)) ?>" style="width:120px;height:100px;object-fit:contain" alt=""><div><?= e($p['name']) ?></div></th><?php endforeach; ?></tr></thead><tbody><?php if(!$products): ?><?= table_empty(2,'No products selected for comparison.') ?><?php else: ?><tr><th>Price</th><?php foreach($products as $p): ?><td><?= money($p['sale_price']?:$p['regular_price']) ?></td><?php endforeach; ?></tr><tr><th>Brand</th><?php foreach($products as $p): ?><td><?= e($p['brand_name']??'') ?></td><?php endforeach; ?></tr><tr><th>Shop</th><?php foreach($products as $p): ?><td><?= e($p['shop_name']??'') ?></td><?php endforeach; ?></tr><tr><th>Stock</th><?php foreach($products as $p): ?><td><?= (int)$p['stock'] ?></td><?php endforeach; ?></tr><tr><th>Action</th><?php foreach($products as $p): ?><td><button class="btn btn-primary btn-sm mb-2" onclick="phAddCart(<?= (int)$p['id'] ?>,1)">Add cart</button><br><button class="btn btn-outline-danger btn-sm" onclick="phToggleList('compare',<?= (int)$p['id'] ?>,this,true)">Remove</button></td><?php endforeach; ?></tr><?php endif; ?></tbody></table></div></section><?php require ROOT_PATH.'/app/Layouts/frontend_footer.php'; ?>
