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

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

try {
    $connection = getDbConnection();
    $prepare = $connection->prepare("SELECT m.codice, m.descrizione, m.quantita, t.nome, t.codice AS codice_modello
                                                FROM modello AS m
                                                INNER JOIN biciclette.tipologia t on m.tipologia = t.codice
                                                ORDER BY m.codice");

    $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><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">
            <table>
                <tr>
                    <th>Codice</th>
                    <th>Descrizione</th>
                    <th>Quantità</th>
                    <th>Tipologia</th>
                </tr>
                <?php foreach ($prepare->fetchAll() as $model) : ?>
                    <tr>
                        <td><?= $model["codice"] ?></td>
                        <td><?= $model["descrizione"] ?></td>
                        <td><?= $model["quantita"] ?></td>
                        <td><a href="index.php?cod=<?= $model["codice_modello"] ?>"><?= $model["nome"] ?></a></td>
                    </tr>
                <?php endforeach; ?>
            </table>
        </div>
    </main>

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


</html>