<?php 
    include("includes/header.php");

    $orderByGet = $_GET["orderBy"] ?? "latest";

    if($orderByGet == "oldest") {
        $orderBy = "ASC";
    } else if ($orderByGet == "latest") {
        $orderBy = "DESC";
    } else {
        $orderBy = "DESC";
    }

    $offset = isset($_GET["offset"]) ? $_GET['offset'] : 0;

    $Parsedown = new Parsedown();
?>

<div class="container">
    <div class="row"> 
        <div class="leftSection col">
            <b>Order by: <a href="index.php?orderBy=latest" class="link-secondary" style="color: lightblue;">latest</a> | <a href="index.php?orderBy=oldest" class="link-secondary" style="color: lightblue;">oldest</a> || </b><a href="https://posts.winsdominoes.net/feed.php" style="color: orange;">Get RSS Feed!</a>
            <hr>
                <?php
                    $stmt = $con->prepare("SELECT * FROM posts ORDER BY id $orderBy");
                    $stmt->execute();

                    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                        $post = new Post($con, $row);
                        $postUrl = $post->getPostUrl();
                        $postAuthor = $post->getPostAuthor();
                        $postedDate = $post->getPublishedDate();
                        $postContent = $Parsedown->text($post->getPostContent());

                        $item = "<div class='post' style='padding: 10px; border: 1px solid rgb(47, 51, 54); border-radius: 0px; margin-bottom: 5px;'>
                                    <span id='content' style='font-size: 12px;'>" . $postAuthor . " · <span style='color: gray'>" . $postedDate . " [GMT +7]</span></span>
                                    <p class='post-text'>" . $postContent . "</p>
                                    <a href='status.php?url=$postUrl' class='text-decoration-none' style='font-size: 12px;'>Read More</a>
                                </div>";
                        
                        echo $item;
                    }
                ?>
        </div>
        <!-- <div class="rightSection col">
            <b>Tweets <a href="https://twitter.com/WinsDominoes">@WinsDominoes on Twitter</a></b>
            <hr>
            <?php
                include_once('includes/classes/twitter.php');
            ?>
        </div> -->
    </div>
    
</div>

<?php
    include("includes/footer.php");
?>
