<?php
if (isset($_GET["location"])) {
    $loc = $_GET["location"];
    if (is_file("$loc.txt")) {
        $games = file("$loc.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $game = $games[array_rand($games)];
    }
}

?>
<!DOCTYPE html>
<html>
    <head>
        <title>pin</title>
        <link rel="stylesheet" href="../gruvbox/gruvbox.min.css">
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>
    <body id="top">
        <main>
            <h1><?=$loc ?? ""?> pin picker</h1>

            <ul>
            <?php if (isset($loc)) { ?>
                <p><a href=".">&lt;- back to list</a></p>
                <p>reload the page to pick a table</p>
                <br>
                <?php foreach ($games as $g) {
                if ($g === $game) { ?>
                    <li><strong><mark><?=$g?></mark></strong></li>
                <?php } else { ?>
                    <li><?=$g?></li>
                <?php }
            } ?>
            <?php } else {
            foreach (array_map(fn($txt) => basename($txt, ".txt"), glob("*.txt")) as $l) { ?>
                <li><a href="?location=<?=$l?>"><?=$l?></a></li>
            <?php }} ?>
            </ul>
        </main>
    </body>
</html>

