<?php
require_once __DIR__ . "/config.php";
require_once __DIR__ . "/includes/functions.php";

$pageTitle = "Startseite";
$products = getProducts();
$categories = getCategories();
$startpageIntroLines = getStartpageIntroLines();

$category = isset($_GET['category'])
    ? normalizeCategoryId($_GET['category'])
    : "";
if ($category !== "" && getCategoryById($category) !== null) {
    $products = array_values(
        array_filter($products, function ($product) use ($category) {
            return productHasCategory($product, $category);
        }),
    );
} else {
    $category = "";
}

include __DIR__ . "/includes/header.php";
?>

<h2><?php echo escape(SITE_SERVICE_HEADER); ?></h2>

<div class="disclaimer-box">
    <?php foreach ($startpageIntroLines as $line): ?>
        <p><?php echo escape($line); ?></p>
    <?php endforeach; ?>
</div>

<div class="category-filter-bar" aria-label="Produktkategorien">
    <a href="?category=" class="btn btn-small <?php echo $category === ""
        ? ""
        : "btn-secondary"; ?>">Alle</a>
    <?php foreach ($categories as $categoryOption): ?>
        <a href="?category=<?php echo urlencode(
            $categoryOption["id"],
        ); ?>" class="btn btn-small <?php echo $category ===
$categoryOption["id"]
    ? ""
    : "btn-secondary"; ?>">
            <?php echo escape($categoryOption["label"]); ?>
        </a>
    <?php endforeach; ?>
</div>

<?php if (empty($products)): ?>
    <div class="alert alert-info">
        <p>Keine Produkte gefunden.</p>
    </div>
<?php else: ?>
    <div class="products-grid">
        <?php foreach ($products as $product): ?>
            <div class="product-card">
                <a href="product.php?id=<?php echo (int) $product["id"]; ?>">
                    <?php $imagePath = getUploadPath(
                        $product["image"] ?? "",
                    ); ?>
                    <?php $imageUrl = getUploadUrl($product["image"] ?? ""); ?>
                    <?php if (
                        $imagePath !== null &&
                        $imageUrl !== null &&
                        file_exists($imagePath)
                    ): ?>
                        <img src="<?php echo escape(
                            $imageUrl,
                        ); ?>" alt="<?php echo escape($product["name"]); ?>">
                    <?php else: ?>
                        <img src="assets/no-image.jpg" alt="Kein Bild verfügbar">
                    <?php endif; ?>
                </a>
                <div class="product-card-content">
                    <h3><a href="product.php?id=<?php echo (int) $product[
                        "id"
                    ]; ?>" class="product-card-title-link"><?php echo escape(
    $product["name"],
); ?></a></h3>
                    <?php $productCategoryIds = getProductCategoryIds($product); ?>
                    <?php if (!empty($productCategoryIds)): ?>
                        <div class="product-card-categories" aria-label="Kategorien">
                            <?php foreach ($productCategoryIds as $productCategoryId): ?>
                                <?php $chipPalette = getCategoryChipPalette(
                                    $productCategoryId,
                                ); ?>
                                <span class="category-chip" style="background-color: <?php echo escape(
                                    $chipPalette["background"],
                                ); ?>; border-color: <?php echo escape(
    $chipPalette["border"],
); ?>; color: <?php echo escape($chipPalette["text"]); ?>;">
                                    <?php echo escape(
                                        getCategoryLabel($productCategoryId),
                                    ); ?>
                                </span>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                    <a href="product.php?id=<?php echo (int) $product[
                        "id"
                    ]; ?>" class="btn btn-block mt-2">Details ansehen</a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

<?php include __DIR__ . "/includes/footer.php"; ?>
