<?php
require "includes/functions.php";

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

try {
    $connection = getDbConnection();
    $modello = $connection->prepare("SELECT codice, nome FROM tipologia");
    $modello->execute();

    if (empty($_GET["cod"])) {
        $prepare = $connection->prepare("SELECT m.codice, m.descrizione, m.quantita, m.tipologia, t.nome
                                                FROM modello AS m
                                                INNER JOIN biciclette.tipologia t on m.tipologia = t.codice
                                                ORDER BY codice");
    } else {
        $codice_modello = $_GET["cod"];
        $prepare = $connection->prepare("SELECT m.codice, m.descrizione, m.quantita, t.nome 
                                                FROM modello AS m
                                                INNER JOIN biciclette.tipologia t on m.tipologia = t.codice
                                                WHERE m.tipologia = ?
                                                ORDER BY t.codice");
        $prepare->bindParam(1, $codice_modello);
    }

    $prepare->execute();
    $connection = null;
} catch (PDOException $e) {
    die ("Error!: " . $e->getMessage() . "<br/>");
}

?>

<html lang="en">
<head>
    <title>HTML</title>
    <?php require "includes/head.php"; ?>
</head>

<body>
    <main class="container">
        <nav>
            <ul>
                <li><strong>Acme Corp</strong></li>
            </ul>
            <ul>
                <li>
                    <form>
                        <select name="favorite-cuisine" aria-label="Cerca per Marca" required
                                onchange="redirectToMarca(this)">
                            <option selected disabled value="">
                                Cerca per Marca
                            </option>
                            <option value="">Tutte</option>
                            <?php foreach ($modello->fetchAll() as $rw) : ?>
                                <option value=<?= $rw["codice"] ?>><?= $rw["nome"] ?></option>
                            <?php endforeach; ?>
                        </select>
                    </form>
                </li>
            </ul>
            <ul>
                <li><a href="#">Login</a></li>
                <li><a href="index.php">Home</a></li>
                <li><a href="listino.php">Listino</a></li>
            </ul>
        </nav>

        <div class="grid">
            <?php foreach ($prepare->fetchAll() as $row) : ?>
                <?= "<div>" ?>
                <article>
                    <header>
                        <a href=<?= "dettagli.php?cod=" . $row["codice"] ?>><b><?= $row["descrizione"] ?></b></a>
                    </header>
                    <?= $row["nome"] ?>
                    <footer>Quantità: <?= $row["quantita"] ?></footer>
                </article>
                <?= "</div>" ?>
            <?php endforeach; ?>
        </div>
    </main>

    <script>
        function redirectToMarca(selectElement) {
            const selectedMarca = selectElement.value;
            if (selectedMarca) {
                window.location.href = `index.php?cod=${encodeURIComponent(selectedMarca)}`;
            }
        }
    </script>
</body>


</html>