<?php include_once "include/default-values.php" ?>
<?php
ob_start();

if (isset($_GET["PID"])) {
    $product = [];
    try {
        $res = $db_con->query("SELECT * FROM products WHERE PID = {$_GET["PID"]} LIMIT 1");
        while ($row = $res->fetch_assoc()) {
            if (isset($row["PPic"])) {
                $pics = explode(" ", $row["PPic"]);
                $row["PPic"] = $pics;
            } else {
                $row["PPic"] = ["imagecomingsoon.png"];
            }
            $product = $row;
        }
    } catch (\Throwable $th) {
        echo $th;
    }
} else {
    header("Location: " . $siteroot . "shop.php");
    exit();
}


?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $product["PName"] ?></title>
    <link rel="stylesheet" href="<?= $siteroot; ?>css/style.css">
    <link rel="stylesheet" href="<?= $siteroot; ?>css/show_product.css">
</head>

<body>
    <?php include "include/header.php"; ?>
    <main>
        <h1><?= $product["PName"] ?></h1>
        <div class="skate">
            <div class="images">
                <?php
                foreach ($product["PPic"] as $pic) {
                ?>
                    <div class="img">
                        <img src="<?= $siteroot; ?>assets/img/<?= $pic ?>" alt="">
                    </div>
                <?php
                }
                ?>
            </div>
            <div class="buy">
                <div class="stiff">
                    <h4>Støvle stivhed:</h4>
                    <h5><?= $product["PStiff"] ?></h5>
                </div>
                <div class="stock <?= $product["PStock"] > 0 ? "stocked" : "" ?>">
                    <h4>Lager:</h4>
                    <h5><?= $product["PStock"] ?></h5>
                </div>
                <div class="support">
                    <h4>Understøtter:</h4>
                    <ul>
                        <?php
                        foreach (explode(" ", $product["PSupp"]) as $supp) {
                        ?>
                            <li>
                                <h5><?= $supp ?></h5>
                            </li>
                        <?php
                        }
                        ?>
                    </ul>
                </div>
                <h4 class="pris"><?= $product["PPrice"] ?>,-</h4>
                <button>Køb</button>
                <div class="stars">
                    <?php
                    for ($i = 0; $i < $product["PStars"]; $i++) {
                    ?>
                        <div class="star">
                            <svg viewBox="0 0 512 512" fill="#FFD700">
                                <path d="M512 255.1c0 11.34-7.406 20.86-18.44 23.64l-171.3 42.78l-42.78 171.1C276.7 504.6 267.2 512 255.9 512s-20.84-7.406-23.62-18.44l-42.66-171.2L18.47 279.6C7.406 276.8 0 267.3 0 255.1c0-11.34 7.406-20.83 18.44-23.61l171.2-42.78l42.78-171.1C235.2 7.406 244.7 0 256 0s20.84 7.406 23.62 18.44l42.78 171.2l171.2 42.78C504.6 235.2 512 244.6 512 255.1z" />
                            </svg>
                        </div>
                    <?php
                    }
                    ?>
                </div>
            </div>
            <div class="description">
                <h4>Beskrivelse:</h4>
                <p><?= $product["PDesc"] ?></p>
            </div>
        </div>
    </main>
    <?php include "include/footer.php"; ?>
    <script src="<?= $siteroot; ?>js/stars.js"></script>
</body>

</html>