<?php
require("../../include/variables.php");
require("../../include/init.php");
require("../../include/main-functions.php");
require("../../include/objects.php");
require("../../include/inputs.php");
require("../../include/panels.php");

$pdo = sqlConnect($sqlDatabaseHost, $sqlDatabaseName, $sqlDatabaseUser, $sqlDatabasePass);

if(isset($_GET['u']) && empty($_GET['u']) == false) {
    $request = $pdo->prepare("SELECT users.ID, users.username, users.display_name, users.profile_picture, users.banner, users.bio, roles.badge_svg FROM users JOIN roles ON users.role = roles.ID WHERE users.username = :username");
    $requestedUserName = htmlspecialchars($_GET['u']);
    $request->bindParam(":username", $requestedUserName);
    $request->execute();
    $result = $request->fetchAll(PDO::FETCH_ASSOC);

    if($result) {
        $userID = $result[0]['ID'];
        $userName = $result[0]['username'];
        $userDisplayName = $result[0]['display_name'];
        $userPPURL = $result[0]['profile_picture'] == NULL ? "https://abs.twimg.com/sticky/default_profile_images/default_profile_400x400.png" : $result[0]['profile_picture'];
        $userBanner = $result[0]['banner'] = NULL ? "" : $result[0]['banner'];
        $userBio = $result[0]['bio'];
        $userBadgeSVG = $userBadgeSVG = $result[0]['badge_svg'];
    } else {
        $userID = 0;
        $userName = htmlspecialchars($_GET['u']);
        $userDisplayName = "Unknown";
        $userPPURL = "https://abs.twimg.com/sticky/default_profile_images/default_profile_400x400.png";
        $userBanner = "";
        $userBio = "";
        $userBadgeSVG = "";
    }
} else {
    $userID = 0;
    $userName = htmlspecialchars($_GET['u']);
    $userDisplayName = "Unknown";
    $userPPURL = "https://abs.twimg.com/sticky/default_profile_images/default_profile_400x400.png";
    $userBanner = "";
    $userBio = "";
    $userBadgeSVG = "";
}

?>

<!DOCTYPE html>
<html lang="fr">

    <head>
        <?php fillHead($rootPageURL, $pageTitle, $darkTheme, $lightTheme);?>
        <style>
            .content {
            padding: 0;
            width: 100%;
            }

            .user-banner {
                <?php
                if (empty($userBanner)) {
                    echo "background-color: black";
                } else {
                    echo "background-image: url('" . $userBanner . "');";
                }
                ?>
            }
        </style>
    </head>

    <body class="body">

        <header>
            <div class="panel-content">
                <?php fillHeader($rootPageURL, $headerTitle, $headerSubtitle);?>
            </div>
        </header>

        <nav>
            <div class="panel-content">
                <?php fillNav($rootPageURL);?>
            </div>
        </nav>

        <main>
            <div class="content">
                <div class="user-profile">
                        <div class="user-banner"></div>
                        <div class="user-header">
                            <div class="user-header-content">
                                <div class="user-pp"><img src="<?=$userPPURL?>"></div>
                                <div class="status"><?=$status?></div>
                                <div class="user-info">
                                    <div class="names">
                                        <div class="display-name"><?=$userDisplayName?></div>
                                        <div class="user-level">
                                            <?php
                                                echo($userBadgeSVG)
                                            ?>
                                        </div>
                                        <div class="username">@<?=$userName?></div>
                                        <div class="bio"><?=$userBio?></div>
                                    </div>
                                    <div>
                                    <?php 
                                    if ($userID == $_SESSION['userID']) {
                                        echo '<a href="/account" class="button">Editer votre profil</a>';
                                    }
                                    ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="user-articles">
                            <div class="user-content">
                                <?php
                                    $search = isset($_GET['search']) ? "%" . htmlspecialchars($_GET['search']) . "%" : "%%";
                                    $request = $pdo->prepare("SELECT ID, title, creation_date, miniature, resume FROM articles WHERE (title LIKE :search OR ID LIKE :search) AND author = :userID AND classification <= :userRole ORDER BY creation_date DESC");
                                    $request->bindParam(":search", $search);
                                    $request->bindParam(":userID", $userID);
                                    $request->bindParam(":userRole", $_SESSION['userRole']);
                                    $request->execute();
                                    $result = $request->fetchAll(PDO::FETCH_ASSOC);
                                    echo ('<h1>Articles de ' . $userDisplayName . '</h1>');
                                    echo ('<form action="#" method="get">');
                                    $shapePath = '<path d="M10.25 3.75c-3.59 0-6.5 2.91-6.5 6.5s2.91 6.5 6.5 6.5c1.795 0 3.419-.726 4.596-1.904 1.178-1.177 1.904-2.801 1.904-4.596 0-3.59-2.91-6.5-6.5-6.5zm-8.5 6.5c0-4.694 3.806-8.5 8.5-8.5s8.5 3.806 8.5 8.5c0 1.986-.682 3.815-1.824 5.262l4.781 4.781-1.414 1.414-4.781-4.781c-1.447 1.142-3.276 1.824-5.262 1.824-4.694 0-8.5-3.806-8.5-8.5z"></path>';
                                    textInput("text", $shapePath, "search", "Chercher", "");
                                    echo ('</form>');

                                    if ($result) {
                                        echo('<div class="articles-list">');
                                        listArticles($result, $rootPageURL);
                                        echo ('</div>');
                                    } else {
                                        echo ($userDisplayName . ' n\'a encore rien publié');
                                    }
                                ?>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </main>
        <footer>
            <div class="panel-content">
                <?php fillFooter($footerText);?>
            </div>
        </footer>
    </body>
</html>