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

// Fetch categories
$categories = [];

$result = mysqli_query($conn, "
    SELECT id, name, slug, image
    FROM categories
    WHERE status = 'active'
    ORDER BY sort_order ASC
");

while ($row = mysqli_fetch_assoc($result)) {
    $categories[] = $row;
}
?>

<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">

<?php 
// Check if we are in a subcategory view
$isSubView = $_GET['subview'] ?? 0; 
?>

<?php if (!$isSubView): ?>
<!-- SIDEBAR -->
<div class="col-xs-12 col-sm-12 col-md-3 col-lg-3">
<div class="category-sidebar-sec">
<ul>

<?php if (!empty($categories)): ?>
<?php foreach ($categories as $cat): 

    // Check if category has subcategories
    $subCountRes = mysqli_query($conn, "SELECT COUNT(*) as cnt FROM subcategories WHERE category_id={$cat['id']} AND status='active'");
    $subCount = mysqli_fetch_assoc($subCountRes)['cnt'];

    // Determine link
    if ($subCount > 0) {
        $link = "subcategory-page.php?category={$cat['slug']}";
    } else {
        $link = "product-category.php?category={$cat['slug']}";
    }

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

?>
<li>
    <a href="<?= $link ?>">
        <?= strtoupper(htmlspecialchars($cat['name'])); ?>
    </a>
</li>
<?php endforeach; ?>
<?php else: ?>
<li>No categories found</li>
<?php endif; ?>

</ul>
</div>
</div>
<?php endif; ?>

<!-- RIGHT SIDE GRID -->
<div class="<?= $isSubView ? 'col-md-12' : 'col-xs-12 col-sm-12 col-md-9 col-lg-9' ?>">

<div class="w-sec product-page-head">
<h2>Products</h2>
</div>

<div class="row">

<?php if (!empty($categories)): ?>
<?php foreach ($categories as $cat): 

    // Check if category has subcategories
    $subCountRes = mysqli_query($conn, "SELECT COUNT(*) as cnt FROM subcategories WHERE category_id={$cat['id']} AND status='active'");
    $subCount = mysqli_fetch_assoc($subCountRes)['cnt'];

    // Determine link
    if ($subCount > 0) {
        $link = "subcategory-page.php?category={$cat['slug']}";
    } else {
        $link = "product-category.php?category={$cat['slug']}";
    }

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

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

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

<h3><?php echo strtoupper(htmlspecialchars($cat['name'])); ?></h3>

<a href="<?= $link ?>" class="stretched-link"></a>

</div>
</div>

<?php endforeach; ?>
<?php else: ?>
<div class="col-12">
<p class="no-product">No categories available.</p>
</div>
<?php endif; ?>

</div>

</div>

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

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