<?php
include_once "header.php";
?>
<!-- Main -->
<div id="main-wrapper">
    <div class="container">
        <div class="row 200%">
            <div class="4u 12u">
                <?php
                include_once "sidebar.php";
                ?>
            </div>
            <div class="8u 12u">
                <div id="content">

                    <!-- Content -->
                    <article>
                        <?php
                        if (!($defaultDestinationSelected == true OR $defaultDestinationId != 0)) {
                            echo '<h2>Restaurants</h2>';
                            $destIdExists = false;
                            $gotDestId = null;

                            $rowsDestinationsSearch = $db->select("SELECT destinations.Id AS DestId,destinations.Name AS DestName,countries.Name AS CountryName FROM destinations,countries WHERE countries.Id=destinations.countryId ORDER BY countryName ASC");


                            if (isset($_POST['destId'])) {
                                $gotDestId = $_POST['destId'];
                            } else {
                                $gotDestId = null;
                            }
                            if ($gotDestId != null) {
                                foreach ($rowsDestinationsSearch as $rowDestinationSearch) {
                                    if ($gotDestId == $rowDestinationSearch['DestId']) {
                                        $destIdExists = true;
                                    }
                                }
                            }
                            ?>
                            <form action="<?php echo Init::HOMEURL; ?>pois/search" method="POST">
                                Select destination:
                                <?php
                                if ($rowsDestinationsSearch) {
                                    echo "<select name='destId'>";
                                    echo "<option value=''>-</option>";
                                }
                                //print_r($rowsDestinationsSearch);
                                foreach ($rowsDestinationsSearch as $rowDestinationSearch) {
                                    $opt = "<option value='" . $rowDestinationSearch['DestId'] . "'";
                                    if ($destIdExists && ($gotDestId == $rowDestinationSearch['DestId'])) {
                                        $opt .= " selected";
                                    }
                                    $opt .= ">" . $rowDestinationSearch['CountryName'] . ' ->' . $rowDestinationSearch['DestName'] . "</option>";
                                    echo $opt;
                                }
                                if ($rowsDestinationsSearch) {
                                    echo "</select>";
                                }
                                ?>
                                <input type="submit" value="Filter">
                            </form>
    <!--                        <p>Phasellus quam turpis, feugiat sit amet ornare in, hendrerit in lectus.
                                Praesent semper mod quis eget mi. Etiam eu ante risus. Aliquam erat volutpat.
                                Aliquam luctus et mattis lectus sit amet pulvinar. Nam turpis nisi
                                consequat etiam lorem ipsum dolor sit amet nullam.</p>-->
                            <br>
                            <?php
                            $db = new Db();
                            if ($gotDestId != null && $destIdExists) {
                                $sqlString = "SELECT `Id`,`destinationId`,`Name`,`imageURL`,`Recommended` FROM `restaurants` WHERE `destinationId`=" . filter_var($gotDestId, FILTER_SANITIZE_NUMBER_INT) . " ORDER BY name ASC";
                            } else {
                                $sqlString = "SELECT `Id`,`destinationId`,`Name`,`imageURL`,`Recommended` FROM `restaurants` ORDER BY name ASC";
                            }
                        } else {
                            echo '<h2>Restaurants in ' . $classFunctions->getDestinationNameById($defaultDestinationId) . '</h2>';
                            $sqlString = "SELECT `Id`,`destinationId`,`Name`,`imageURL`,`Recommended` FROM `restaurants` WHERE `destinationId`=" . filter_var($defaultDestinationId, FILTER_SANITIZE_NUMBER_INT) . " ORDER BY name ASC";
                        }

                        $rowsDestinations = $db->select($sqlString);
                        if ($rowsDestinations) {
                            ?>
                            <div class="container">
                                <?php
                                foreach ($rowsDestinations as $row) {
                                    echo $classFunctions->getGrid($row, 'restaurant');
                                }
                                ?>
                            </div>
                            <?php
                        } else {
                            echo "No restaurants found!";
                        }
                        ?>
                    </article>

                </div>
            </div>
        </div>
    </div>
</div>
<?php
include_once "footer.php";
?>