<?php
// login.php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();

// 1) DB-Verbindung (einmal)
$servername = "localhost";
$port = 3306;
$username = "FSST";
$password = "L9wUNZZ9Qkbt";
$db = "FSST";

$conn = mysqli_connect($servername, $username, $password, $db, $port);
if (!$conn)
{
    http_response_code(500);
    die("Datenbankfehler");
}
?>

<?php
$activeCategory = isset($_GET['category']) ? $_GET['category'] : 'all';
?>

<?php
$categories = [
        'iphone'  => ['id' => 20, 'label' => 'iPhone'],
        'ipad'    => ['id' => 21, 'label' => 'iPad'],
        'macbook' => ['id' => 22, 'label' => 'MacBook'],
        'airpods' => ['id' => 23, 'label' => 'AirPods'],
        'accessories' => ['id' => 24, 'label' => 'Accessories'],
];
?>




<?php foreach ($categories as $key => $cat): ?>

    <?php if ($activeCategory === 'all' || $activeCategory === $key): ?>

        <?php
        $stmt = $conn->prepare("
            SELECT productID, model, description, imagePath
            FROM products
            WHERE categoryID = ?
        ");

        $stmt->bind_param("i", $cat['id']); // i = integer
        $stmt->execute();

        $result = $stmt->get_result();
        ?>

        <?php if ($result->num_rows > 0): ?>
            <section class="product-section">
                <h2><?= htmlspecialchars($cat['label']) ?></h2>

                <div class="product-scroll">
                    <?php while ($product = $result->fetch_assoc()): ?>
                        <div class="product-card">
                            <img
                                    src="<?= isset($product['imagePath']) ? $product['imagePath'] : 'assets/images/placeholder.png' ?>"
                                    alt="<?= htmlspecialchars($product['model']) ?>">

                            <div class="product-card__content">
                                <h3><?= htmlspecialchars($product['model']) ?></h3>
                                <p><?= htmlspecialchars($product['description']) ?></p>
                            </div>
                        </div>
                    <?php endwhile; ?>
                </div>
            </section>
        <?php endif; ?>

        <?php $stmt->close(); ?>

    <?php endif; ?>

<?php endforeach; ?>


<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="assets/css/compcard.css">
</head>