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

// Used for file.html

// Executes all the needed code
async function Load() {
    $("#CancelChanges").on("click", function() {
        RedirectBack("/" + Topic() + "/Page:Files");
    });
    $("#UploadedFile").on("change", function() {
        $("#DisplayFile").empty();
        $("#FileUploadLabel").attr("value", 1);
        const uploadedFile = $("#UploadedFile")[0].files[0];
        $("#FileUploadLabel").text("File: " + uploadedFile.name);
        $("#FileGivenName").val(uploadedFile.name);
        const reader = new FileReader();
        reader.onload = function (e) {
            if (uploadedFile.type.split("/")[0] == "image") {
                $("#DisplayFile").append("<img src=\"" + e.target.result + "\">");
            }
            else {
                $("#DisplayFile").append("<audio controls><source src=\"" + e.target.result + "\"></audio>");
            }
        };
        reader.readAsDataURL(uploadedFile);
    });
    $("#SaveChanges").on("click", async function() {
        // Checks if the fields have been filled
        if ($("#SaveChanges").attr("value") != "disabled" && $("#UploadedFile")[0].files.length == 1) {
            $("#SaveChanges").attr("value", "disabled");
            // Hides the error text
            $("#ErrorText").hide();
            $("#ErrorText").text("");
            const formData = new FormData();
            formData.append("topic", Topic());
            formData.append("file", $("#UploadedFile")[0].files[0]);
            formData.append("given", $("#FileGivenName").val());
            formData.append("description", $("#FileDescription").val());
            if (activeLock > 0) {
                return;
            }
            LockUI();
            await $.ajax({
                url: "/file/0",
                method: "POST",
                contentType: false,
                processData: false,
                data: formData,
                success: function(response) {
                    activeLock++;
                    UnlockUI();
                    AlertWindow("success", "File has been uploaded!", "/" + Topic() + "/File:" + response.redirect, true);
                },
                error: function(response) {
                    UnlockUI();
                    // Outputs the error message
                    $("#ErrorText").show();
                    $("#ErrorText").text(response.responseJSON.messages.error);
                    $("#SaveChanges").attr("value", "");
                }
            });
        }
        else {
            // Outputs the error message
            $("#ErrorText").show();
            if ($("#SaveChanges").attr("value") == "disabled") {
                $("#ErrorText").text("Loading!");
            }
            else if ($("#UploadedFile")[0].files.length == 0) {
                $("#ErrorText").text("You must upload a file!");
            }
            else {
                $("#ErrorText").text("Something has gone wrong!");
            }
        }
        $("#SaveChanges").attr("value", "");
    });
}