<?php
include("inc/header.php");

if (!isset($_GET['category'])) die("Category not specified");

$slug = mysqli_real_escape_string($conn, $_GET['category']);

// Fetch current category
$category = mysqli_fetch_assoc(mysqli_query($conn, "
    SELECT id, name
    FROM categories
    WHERE slug='$slug' AND status='active'
"));
if (!$category) die("Category not found");

// Fetch subcategories
$subcategories = mysqli_query($conn, "
    SELECT id, name, slug, image
    FROM subcategories
    WHERE category_id={$category['id']} AND status='active'
    ORDER BY sort_order ASC
");
?>

<section class="inner-banner-row">
    <div class="container">
        <div class="inner-banner-sec"></div>
    </div>
</section>

<section class="product-page-row">
<div class="container">
<div class="row">

<!-- No sidebar, full width -->
<div class="col-md-12">

<div class="w-sec product-page-head">
<h2><?= htmlspecialchars($category['name']); ?> - Subcategories</h2>
</div>

<div class="row">

<?php if (mysqli_num_rows($subcategories) > 0): ?>
<?php while ($sub = mysqli_fetch_assoc($subcategories)):

    $imagePath = "assets/images/product-category-img1.jpg";
    if (!empty($sub['image']) && file_exists("admin/uploads/subcategories/".$sub['image'])) {
        $imagePath = "admin/uploads/subcategories/".$sub['image'];
    }

?>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-6">
<div class="product-page-sec">

<img src="<?= $imagePath ?>" alt="<?= htmlspecialchars($sub['name']); ?>">

<h3><?= htmlspecialchars($sub['name']); ?></h3>

<a href="product-category.php?category=<?= $slug ?>&sub=<?= $sub['slug'] ?>" class="stretched-link"></a>

</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<div class="col-12">
<p class="no-product">No subcategories found.</p>
</div>
<?php endif; ?>

</div>

</div>

</div>
</div>
</section>

<?php include("inc/footer.php"); ?>