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

                    <!-- Content -->
                    <article>

                        <?php
                        if (!($defaultDestinationSelected == true OR $defaultDestinationId != 0)) {
                            echo '<h2>Points of Interest</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 = filter_var($_POST['destId'],FILTER_SANITIZE_NUMBER_INT);
                            } 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">
                                <label>Select destination:</label>
                                <?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>
                            <br>
                            <?php
                            $db = new Db();
                            if ($gotDestId != null && $destIdExists) {
                                $sqlString = "SELECT `Id`,`destinationId`,`typeId`,`Name` FROM `poi` WHERE `destinationId`=" . filter_var($gotDestId,FILTER_SANITIZE_NUMBER_INT) . " ORDER BY name ASC";
                            } else {
                                $sqlString = "SELECT `Id`,`destinationId`,`typeId`,`Name` FROM `poi` ORDER BY name ASC";
                            }
                        } else {
                            echo '<h2>Points of Interest in ' . $classFunctions->getDestinationNameById($defaultDestinationId) . '</h2>';
                            $sqlString = "SELECT `Id`,`destinationId`,`Name`,`typeId` FROM `poi` 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,'poi');
                                }
                                ?>
                            </div>
                            <?php
                        } else {
                            echo "No restaurants found!";
                        }
                        ?>
                    </article>

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