<?php
require "../include/variables.php";
require "../include/functions.php";

if($_SESSION['role'] < 3) {
    header("Location: index.php");
    http_response_code(404);
    die();
}

if(isset($_POST['userid']) && isset($_POST['role']) && isset($_POST['accreditation']) && $_POST['userid'] != $_SESSION['userid'])  {
    $bdd = connect($dbhost, $dbname, $dbuser, $dbpass);

    $req = $bdd->prepare("UPDATE users SET role = :role, accreditation = :accreditation WHERE ID = :userid");
    $req->bindParam(':role',$_POST['role'], PDO::PARAM_INT);
    $req->bindParam(':accreditation',$_POST['accreditation'], PDO::PARAM_INT);
    $req->bindParam(':userid', $_POST['userid'], PDO::PARAM_INT);
    if ($req->execute()) {
        $status = "Utilisateur mis à jour";
    } else {
        $status = "Erreur SQL";
    }
}

?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta name="robots" content="noindex">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./src/css/style.css">
    <style>
        @media (prefers-color-scheme: dark) {
            .body {
                <?=$theme_dark?>
            }
        } 
        @media (prefers-color-scheme: light) {
            .body {
                <?=$theme_light?>
            }
        }

        .user {
            background-color: var(--banner-background);
            display: flex;
            padding: 0.5em 1em 0.5em 0.5em;
            border-radius: 10em;
            width: 30em;
            height: 10em;
            margin: 2em auto;
        }

        .user-username {

        }

        .user-display-name {
            font-weight: bold;
            display: flex;
        }

        .user-display-name .certification {
            transform: translate(0, -15%);
            width: 1.5em;
            margin-left: 0.5em;
        }

        .perm-div {
            display: inline;
        }

        .user-names {
            margin: 1em 1em 1em 0;
        }

        .user-pp {
            height: 6em;
            height: 100%;
            padding-right: 2em;
        }

        .user-pp-image {
            height: 10em;
            width: 10em;
            border-radius: 10em;
        }

        .certification {
            transform: translate(0, -15%);
            width: 1.5em;
        }
    </style>
    <link rel="icon" href="./src/img/favicon.ico">
    <title><?=$title?></title>
</head>
<body class="body">
    <header>
        <div class="pancontent">
            <div class="athena-container">
                <a href="<?=$root?>">
                    <img src="./src/img/athena-mono.png" class="athena">
                </a>
            </div>
            <div class="content">
                <div>
                    <div class="main-title">
                        <div class="title"><?=$header_title?></div>
                        <div class="subtitle"><?=$header_subtitle?></div>
                    </div>
                </div>
            </div>
        </div>
    </header>
    <nav>
        <div class="pancontent">
            <?php nav($nav);?>
        </div>
    </nav>
    <main>
        <div class="content">
            <div>
                <div class="status"><?=$status?></div>
                <?php
                    $bdd = connect($dbhost, $dbname, $dbuser, $dbpass);
                    $req = $bdd->prepare("SELECT ID, display_name, username, email, role, accreditation, profile_picture FROM users ORDER BY creation_date DESC");
                    $req->execute();
                    $resultat = $req->fetchAll(PDO::FETCH_ASSOC);

                    if ($resultat) {
                        foreach($resultat as $row) {
                            $user_pp = $row['profile_picture'] == NULL ? "https://abs.twimg.com/sticky/default_profile_images/default_profile_400x400.png" : $row['profile_picture'];
                            echo "<div class='user'>";
                            echo "<div class='user-pp'><a href='user.php?user="  .  $row['username'] . "'>";
                            echo "<img src='" . $user_pp  . "' class='user-pp-image'>";
                            echo "</a></div>";
                            echo "<div class='user-data'>";
                            echo "<div class='user-names'>";
                            echo "<div class='user-display-name'>" . $row['display_name'];
                            if($row['role'] > 1) {
                                echo '<svg class="certification" fill="' . $certif_colors[$row['role']] . '" 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>';
                            }
                            echo "</div>";
                            echo "<div class='user-username'>@" . $row['username'] . "</div>";
                            echo "</div>";
                            echo "<div class='user-perms'>";
                            echo "<form action='' method='post'>";
                            echo "<div class='perm-div'><select name='accreditation' required>";
                            foreach($classifications as $id => $nom) {
                                if($row['accreditation'] == $id) {
                                    echo '<option value="' . $id .'" selected>' . $nom . '</option>';
                                } else {
                                    echo '<option value="' . $id .'">' . $nom . '</option>';
                                }
                            }
                            echo "</select>";
                            echo "<select name='role' required>";
                            foreach($roles as $id => $nom) {
                                if($row['role'] == $id) {
                                    echo '<option value="' . $id .'" selected>' . $nom . '</option>';
                                } else {
                                    echo '<option value="' . $id .'">' . $nom . '</option>';
                                }
                            }
                            echo "</select></div>";
                            echo "<div class='perm-div'><input type='hidden' name='userid' value='" . $row['ID'] . "'><input type='submit' value='Mettre à jour'></div>";
                            echo "</div></form></div></div>";
                            echo "</div>";
                        }
                    }
                    ?>
            </div>
        </div>
    </main>
    <footer>
        <div class="content">
            <div>
                <div><?=$copyright?></div>
            </div>
        </div>
    </footer>
</body>
</html>