<script>
    function changePassword() {
        const oldPassword = document.getElementById("changeOldPassword").value;
        const newPassword = document.getElementById("changeNewPassword").value;

        const data = new URLSearchParams();
        data.append("action", "change_password");
        data.append("old_password", oldPassword);
        data.append("new_password", newPassword);

        doChangePassword(data, "Password change Successful!", "Password change failed.");
    }

    function doChangePassword(requestData, successMessage, failureMessage) {
        doAccountAction(requestData, successMessage, failureMessage);
    }

    function updateUserProfile() {
        const firstName = document.getElementById("updateFirstName").value;
        const lastName = document.getElementById("updateLastName").value;
        const nickname = document.getElementById("updateNickname").value;
        const minecraftNick = document.getElementById("updateMinecraftNick").value;

        const data = new URLSearchParams();
        data.append("action", "update_user_profile");
        data.append("first_name", firstName);
        data.append("last_name", lastName);
        data.append("nickname", nickname);
        data.append("minecraft_nick", minecraftNick);

        doAccountAction(data, "Profile update Successful!", "Profile update failed.");
    }

    function updateEmail() {
        const newEmail = document.getElementById("updateNewEmail").value;

        const data = new URLSearchParams();
        data.append("action", "update_user_email");
        data.append("email", newEmail);

        doAccountAction(data, "Email update Successful!", "Email update failed.");
    }

    function populateUserInfoFields(userData) {
        document.getElementById("updateFirstName").value = userData.FirstName || "";
        document.getElementById("updateLastName").value = userData.LastName || "";
        document.getElementById("updateNickname").value = userData.Nickname || "";
        document.getElementById("updateMinecraftNick").value = userData.MinecraftNick || "";
        document.getElementById("updateNewEmail").value = userData.Email || "";
    }

    async function getUserInfo() {
        const data = new URLSearchParams();
        data.append("action", "get_user_info");

        const result = await doAccountAction(data, "User info retrieved Successfully!", "User info retrieval failed.", true);

        if (result && result.Status === "Success") {
            populateUserInfoFields(result.UserInfo);
        }
    }
</script>

<script defer>
    getUserInfo();
</script>

<!-- Centralized Status Message -->
<p id="StatusMessage"></p>

<button type="button" onclick="logout()">Logout</button><br>

<div class="form-container" id="updateUserProfileForm">
    <h1>Update User</h1>

    <h2>Profile</h2>
    <label for="updateFirstName">First Name:</label>
    <input type="text" id="updateFirstName" name="updateFirstName" required><br>

    <label for="updateLastName">Last Name:</label>
    <input type="text" id="updateLastName" name="updateLastName" required><br>

    <label for="updateNickname">Nickname:</label>
    <input type="text" id="updateNickname" name="updateNickname" required><br>

    <label for="updateMinecraftNick">Minecraft Nick:</label>
    <input type="text" id="updateMinecraftNick" name="updateMinecraftNick" required><br>

    <button type="button" onclick="updateUserProfile()">Update Profile</button>

    <br><br>

    <h2>Email</h2>

    <label for="updateNewEmail">New Email:</label>
    <input type="email" id="updateNewEmail" name="updateNewEmail" required><br>

    <button type="button" onclick="updateEmail()">Update Email</button>

    <br><br>

    <h2>Password</h2>

    <label for="changeOldPassword">Old Password:</label>
    <input type="password" id="changeOldPassword" name="changeOldPassword" required><br>

    <label for="changeNewPassword">New Password:</label>
    <input type="password" id="changeNewPassword" name="changeNewPassword" required><br>

    <button type="button" onclick="changePassword()">Change Password</button>
</div>

<hr>

