/*
    Created: 2025-12-04
    Author: Kārlis Grīnvalds
    Last edited: 2026-05-11
    Edited by: Kārlis Grīnvalds
*/

// Used for topic.html

// Executes all the needed code
async function Load() {
    // Loads topic data
    await $.ajax({
        url: "/topic/" + Topic(),
        method: "GET",
        success: function(response) {
            $("#TopicTitle").append(response[0].title);
            if (response[0].content.trim().length <= 0) {
                $("#TopicContent").append("<p style=\"color: rgb(100, 100, 100); font-size: 80%;\">No content added..</p>");
            }
            else {
                $("#TopicContent").append(LoadContent(response[0].content));
            }
            $("footer").append("Created at: " + response[0].created_at);
            if (response[0].edited_at != null) {
                $("footer").append("<br>Last edited: " + response[0].edited_at);
            }
        },
        error: function(response) {
            Link("/error");
        }
    });
    await $.ajax({
        url: "/user/permissions",
        method: "GET",
        data: {
            topic: Topic(),
            select: ["edit_topic"]
        },
        success: function(permissions) {
            if (Object.keys(permissions).length > 0) {
                if (permissions.edit_topic) {
                    CreateDropDownMenu(permissions);
                    $("#DropDownMenu").append("<li><a href=\"?a=edit\">Edit topic</a></li>");
                }
            }
        }
    });
}