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

// Used for topic.html

// Runs all the needed functions for the current page
async function Load() {
    // Gets the url parameter
    const topicAction = window.location.href.split("/topic")[1];
    await $.ajax({
        url: "/topic/" + Topic(),
        method: "GET",
        success: function(response) {
            if (topicAction.length > 0 && topicAction == "?a=edit") {
                $("#MainContainer").html('<div class="form"><h1>Topic data</h1><input id="TopicTitle" placeholder="Title"><br><textarea id="TopicContent" placeholder="Content"></textarea></div><p id="ErrorText" class="error-text"></p>');
                // Creates all the actions that the user can do - save or delete the entry, or don't save any changes
                $("#UserActionBar").append("<a href=\"javascript:void(0);\" id=\"SaveChanges\">Save</a><a href=\"/" + Topic() + "/admin\">Return</a>");
                // Inserts all topic data in input fields
                $("#TopicTitle").val(response[0].title);
                $("#TopicContent").val(response[0].content);
                // Adds a function to save button
                $("#SaveChanges").on("click", async function() {
                    // Checks, if the fields have been filled
                    if ($("#TopicTitle").val().trim().length > 0 && $("#SaveChanges").attr("value") != "disabled") {
                        $("#SaveChanges").attr("value", "disabled");
                        // Hides the error text
                        $("#ErrorText").hide();
                        $("#ErrorText").text("");
                        // Prepares POST data
                        let data = {
                            title: $("#TopicTitle").val(),
                            content: $("#TopicContent").val()
                        };
                        // Updates the topic and returns the results
                        await $.ajax({
                            url: "/topic/" + Topic(),
                            method: "POST",
                            data: data,
                            success: function() {
                                // Topic page has been updated!
                                alert("Topic page has been updated!");
                            },
                            error: function(response) {
                                // Outputs an error message
                                $("#ErrorText").show();
                                $("#ErrorText").text(response.responseJSON.messages.error);
                            }
                        });
                    }
                    else {
                        // Outputs an error message
                        $("#ErrorText").show();
                        $("#ErrorText").text("All input fields must be filled out!");
                    }
                });
                $("#SaveChanges").attr("value", "");
            }
            else {
                $("#MainContainer").html('<h1 id="TopicTitle"></h1><hr><div id="TopicContent" class="entry-content"></div>');
                $("#TopicTitle").append(response[0].title);
                let pageContent = LoadContent(response[0].content);
                $("#TopicContent").append(FormatText(pageContent));
                $("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/404");
        }
    });
}