import { postData, verifyLogin } from "./client.js";

verifyLogin();
const token = window.localStorage.getItem("token");

const infoForm = document.getElementById("infoForm");

async function fetchPersonalInformation() {
    const userData = await postData("https://134.209.36.64:3000/getInfo", {}, token);
    
    console.log(userData);

    document.getElementById("usernameField").textContent = userData.Username;
    document.getElementById("firstNameField").value = userData.FirstName;
    document.getElementById("lastNameField").value = userData.LastName;
    document.getElementById("dobField").value = userData.DOB.split("T")[0];
    
    return userData
}

const userData = fetchPersonalInformation();

async function updatePersonalInformation(e) {
    console.log(verifyLogin());
    e.preventDefault();
    const userData = {
        firstName: document.getElementById("firstNameField").value,
        lastName: document.getElementById("lastNameField").value,
        dob: document.getElementById("dobField").value
    }
    console.log(userData.dob);
    const resultObject = await postData("https://134.209.36.64:3000/setInfo", userData, token);
    alert(resultObject.message);
}

infoForm.onsubmit = updatePersonalInformation;
