<?php 
    include_once './inc/auto_login.php';
    include_once './inc/database.php';
    // Recent posts
    $rcPost = mysqli_query($con, "SELECT * FROM `posts` WHERE `status`= 1 ORDER BY `id` DESC LIMIT 5") or null;
    if (is_null($rcPost) || mysqli_num_rows($rcPost) == 0) {
        $rcRes = false;
    } else {
        $rcRes = true;
    }

    // Featured posts
    $ftPost = mysqli_query($con, "SELECT * FROM `posts` WHERE `status` = 1 ORDER BY `views` DESC LIMIT 5") or null;
    if (is_null($ftPost) || mysqli_num_rows($ftPost) == 0) {
        $ftRes = false;
    } else {
        $ftRes = true;
    }

    // Featured users
    $ftUser = mysqli_query($con, "SELECT U.id, U.displayname, COUNT(C.ID) AS PostCount FROM users AS U INNER JOIN posts AS C ON U.ID = C.author WHERE C.status = 1 GROUP BY U.displayname ORDER BY COUNT(C.ID) DESC LIMIT 5") or null;
    if (is_null($ftUser) || mysqli_num_rows($ftUser) == 0) {
        $ftuRes = false;
    } else {
        $ftuRes = true;
    }
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>Home page | ThiccWiki</title>
    <?php include './inc/css.php';?>
</head>

<body>
    <?php include './inc/navbar.php';?>
    <div class="wrapper circle-area">
        <section style="padding-top: 100px;padding-bottom: 100px;">
            <div class="container">
                <div class="row">
                    <div class="col-md-5 col-lg-5 col-xl-4 offset-lg-0 offset-xl-1 text-center">
                        <h1 style="font-family: Montserrat, sans-serif;"><strong>ThiccWiki</strong></h1>
                        <p>Official ThiccMC wiki. <br>For community, by community.</p>
                        <div class="accordion accordion-flush" role="tablist" id="accordion-1" style="margin-bottom: 10px;">
                            <div class="accordion-item">
                                <h2 class="accordion-header" role="tab"><button class="accordion-button collapsed text-white" data-bs-toggle="collapse" data-bs-target="#accordion-1 .item-1" aria-expanded="false" aria-controls="accordion-1 .item-1" style="background: rgb(34,34,34);margin-top: 30px;"><strong>Recent posts</strong></button></h2>
                                <div class="accordion-collapse collapse item-1" role="tabpanel" data-bs-parent="#accordion-1" style="background: rgba(3,3,3,0.1);border-bottom-right-radius: 10px;border-bottom-left-radius: 10px;">
                                    <div class="accordion-body">
                                        <h6 class="text-start" style="padding: 2px;"><a class="link-primary" href="javascript:" id="_rc-post" style="text-decoration: none;">&gt; Recent posts</a><br></h6>
                                        <h6 class="text-start" style="padding: 2px;"><a class="link-primary" href="javascript:" id="_ft-post" style="text-decoration: none;">&gt; Featured posts</a><br></h6>
                                        <h6 class="text-start" style="padding: 2px;"><a class="link-primary" href="javascript:" id="_ft-user" style="text-decoration: none;">&gt; Featured members</a><br></h6>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div><a class="btn btn-primary anchor-redirect" role="button" style="margin: 5px;padding-top: 8px;padding-bottom: 8px;padding-right: 16px;padding-left: 16px;" href="template.html">List of available posts</a></div>
                    </div>
                    <div class="col-md-7 col-lg-7 col-xl-6 offset-lg-0 offset-xl-0" id="_rc-post-content" style="padding-top: 20px;padding-right: 20px;padding-left: 20px; display: none;">
                        <h4>Recent posts:</h4>
                        <hr>
                <?php 
                    if ($rcRes == true) {
                        while ($r = mysqli_fetch_array($rcPost)) {
                            echo '
                            <div class="row">
                                <div class="col-1 col-md-1 col-lg-1 col-xl-1 text-end" style="padding-right: 8px;padding-left: 0px;"><span>•<br></span></div>
                                <div class="col-11 col-md-11 col-xl-11" style="padding-left: 0px;">
                                    <h5><a href="'.$host."/wiki/".$r['link_encoded'].'" style="text-decoration: none;">'.$r['title'].'</a><br></h5>
                                </div>
                            </div>
                            ';
                        }
                    } else {
                        echo '
                            <h4>No data available.</h4>
                        ';
                    }
                ?>
                    </div>
                    <div class="col-md-7 col-lg-7 col-xl-6 offset-lg-0 offset-xl-0" id="_ft-post-content" style="padding-top: 20px;padding-right: 20px;padding-left: 20px; display: none;">
                        <h4>Featured posts:</h4>
                        <hr>
                <?php 
                    if ($ftRes == true) {
                        while ($r = mysqli_fetch_array($ftPost)) {
                            echo '
                            <div class="row">
                                <div class="col-1 col-md-1 col-lg-1 col-xl-1 text-end" style="padding-right: 8px;padding-left: 0px;"><span>•<br></span></div>
                                <div class="col-11 col-md-11 col-xl-11" style="padding-left: 0px;">
                                    <h5><a href="'.$host."/wiki/".$r['link_encoded'].'" style="text-decoration: none;">'.$r['title'].'</a><br></h5>
                                </div>
                            </div>
                            ';
                        }
                    } else {
                        echo '
                            <h4>No data available.</h4>
                        ';
                    }
                ?>
                    </div>
                    <div class="col-md-7 col-lg-7 col-xl-6 offset-lg-0 offset-xl-0" id="_ft-user-content" style="padding-top: 20px;padding-right: 20px;padding-left: 20px; display: none;">
                        <h4>Featured members:</h4>
                        <hr>
                <?php 
                    if ($ftuRes == true) {
                        while ($r = mysqli_fetch_array($ftUser)) {
                            echo '
                            <div class="row">
                                <div class="col-1 col-md-1 col-lg-1 col-xl-1 text-end" style="padding-right: 8px;padding-left: 0px;"><span>•<br></span></div>
                                <div class="col-11 col-md-11 col-xl-11" style="padding-left: 0px;">
                                    <h5><a href="'.$host."/users/".$r['id'].'" style="text-decoration: none;">'.$r['displayname'].'</a><br></h5>
                                </div>
                            </div>
                            ';
                        }
                    } else {
                        echo '
                            <h4>No data available.</h4>
                        ';
                    }
                ?>
                    </div>
                </div>
            </div>
        </section>
        <section style="padding-top: 50px;padding-bottom: 50px;">
            <div class="container">
                <div class="row">
                    <div class="col-md-5 col-lg-5 col-xl-4 offset-lg-0 offset-xl-1 text-center"><img src="assets/img/xd.png" width="250" height="250"></div>
                    <div class="col-md-7 col-lg-7 col-xl-6 offset-lg-0 offset-xl-0" id="home-discord" style="padding-top: 20px;padding-right: 20px;padding-left: 20px;">
                        <h3>Join ThiccMC Discord</h3>
                        <hr>
                        <p>ThiccMC is built on our awesome community.<br>With over 3,000 members with active members everyday, join us with the fun now!</p><a class="btn btn-primary" role="button" style="margin: 5px;padding-top: 8px;padding-bottom: 8px;padding-right: 16px;padding-left: 16px;" href="discord">Discord</a><a class="btn btn-primary" role="button" style="margin: 5px;padding-top: 8px;padding-bottom: 8px;padding-right: 16px;padding-left: 16px;" href="https://thiccmc.com" target="_blank">ThiccMC Website</a>
                    </div>
                </div>
            </div>
        </section>
    </div>
    <div class="loading-bar-frame"></div>
    <?php include './inc/footer.php';?>
    <?php 
    if (isset($_GET['signin'])) {
        if (isset($_COOKIE["SESSLOG"]) && $_COOKIE["SESSLOG"] == "1") {
            echo '
            <script id="_SignInSuccess">
                document.cookie = "SESSLOG=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
                const Toast = Swal.mixin({
                    toast: true,
                    position: "bottom-end",
                    showConfirmButton: false,
                    timer: 3000,
                });
        
                Toast.fire({
                    icon:  "success",
                    title: `<span style="font-size: 14px; color: #fff">Signed in as <strong>'.$nav_username.'</strong></span>`,
                    showClass: {
                        popup: \'animate__animated animate__fadeInUp\'
                    },
                    hideClass: {
                        popup: \'animate__animated animate__fadeOutDown\'
                    }
                });
        
                setTimeout(function() {
                    $("#_SignInSuccess").remove();
                }, 3100);
            </script>
            ';
        }
    }
    ?>
</body>

</html>
