<?php
require("../../include/variables.php");
require("../../include/functions.php");
$pdo = sqlConnect($sqlDatabaseHost, $sqlDatabaseName, $sqlDatabaseUser, $sqlDatabasePass);

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

    if($result) {
        $userID = $result[0]['ID'];
        $userName = $result[0]['username'];
        $userDisplayName = $result[0]['display_name'];
        $userLevel = $result[0]['level'];
        $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'];
    } else {
        $userID = 0;
        $userName = htmlspecialchars($_GET['u']);
        $userDisplayName = "Unknown";
        $userLevel = 0;
        $userPPURL = "https://abs.twimg.com/sticky/default_profile_images/default_profile_400x400.png";
        $userBanner = "";
        $userBio = "";
    }
} else {
    $userID = 0;
    $userName = htmlspecialchars($_GET['u']);
    $userDisplayName = "Unknown";
    $userLevel = 0;
    $userPPURL = "https://abs.twimg.com/sticky/default_profile_images/default_profile_400x400.png";
    $userBanner = "";
    $userBio = "";
}

?>

<!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 . "');";
                }
                ?>
            }

            .certification {
                fill: <?php echo $userLevel > 0 ? $certificationColors[$userLevel] : "#000000"?>;
            }
        </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
                                                if($userLevel > 1) {
                                                    echo '<svg class="certification" viewBox="0 0 22 22" aria-label="Compte certifié" role="img"><g><path d="M20.396 11c-.018-.646-.215-1.275-.57-1.816-.354-.54-.852-.972-1.438-1.246.223-.607.27-1.264.14-1.897-.131-.634-.437-1.218-.882-1.687-.47-.445-1.053-.75-1.687-.882-.633-.13-1.29-.083-1.897.14-.273-.587-.704-1.086-1.245-1.44S11.647 1.62 11 1.604c-.646.017-1.273.213-1.813.568s-.969.854-1.24 1.44c-.608-.223-1.267-.272-1.902-.14-.635.13-1.22.436-1.69.882-.445.47-.749 1.055-.878 1.688-.13.633-.08 1.29.144 1.896-.587.274-1.087.705-1.443 1.245-.356.54-.555 1.17-.574 1.817.02.647.218 1.276.574 1.817.356.54.856.972 1.443 1.245-.224.606-.274 1.263-.144 1.896.13.634.433 1.218.877 1.688.47.443 1.054.747 1.687.878.633.132 1.29.084 1.897-.136.274.586.705 1.084 1.246 1.439.54.354 1.17.551 1.816.569.647-.016 1.276-.213 1.817-.567s.972-.854 1.245-1.44c.604.239 1.266.296 1.903.164.636-.132 1.22-.447 1.68-.907.46-.46.776-1.044.908-1.681s.075-1.299-.165-1.903c.586-.274 1.084-.705 1.439-1.246.354-.54.551-1.17.569-1.816zM9.662 14.85l-3.429-3.428 1.293-1.302 2.072 2.072 4.4-4.794 1.347 1.246z"></path></g></svg>';
                                                }
                                            ?>
                                        </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, date, miniature, resume FROM articles WHERE (title LIKE :search OR ID LIKE :search) AND author = :userID ORDER BY date DESC");
                                    $request->bindParam(":search", $search);
                                    $request->bindParam(":userID", $userID);
                                    $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>