<?php include 'includes/header.php'; ?>

<?php
$cat_id = $_GET['id'];

$cat = $pdo->prepare("SELECT name FROM categories WHERE id = ?");
$cat->execute([$cat_id]);
$cat_name = $cat->fetchColumn();

$products = $pdo->prepare("SELECT * FROM products WHERE category_id = ?");
$products->execute([$cat_id]);
?>

<h2>دسته: <?php echo $cat_name; ?></h2>

<div class="products">

<?php foreach ($products as $p):

    $img = $pdo->prepare("SELECT image_path FROM product_images WHERE product_id = ? LIMIT 1");
    $img->execute([$p['id']]);
    $image = $img->fetchColumn();
?>

<div class="product-card">
    <img src="../admin/<?php echo $image; ?>">
    <h3><?php echo $p['title']; ?></h3>
    <p><?php echo number_format($p['final_price']); ?> تومان</p>
    <a href="product.php?id=<?php echo $p['id']; ?>">مشاهده</a>
</div>

<?php endforeach; ?>

</div>

<?php include 'includes/footer.php'; ?>