const $ = (element) => document.getElementById(element);

url_certificat = $("tableau-certificat").dataset.url;
const tableau_certificat = new Tabulator("#tableau-certificat", {
    columns: [
        {"title": "Nom du certificat", "field": "titre"},
        {"title": "Nom de l'organisme", "field": "organisme"},
        {"title": "Date d'obtention", "field": "date_obtention", formatter:"datetime", formatterParams:{
            inputFormat:"yyyy-MM-dd",
            outputFormat:"dd/MM/yy",
        }},
        {"title": "Date de fin de validité", "field": "date_fin", formatter:"datetime", formatterParams:{
            inputFormat:"yyyy-MM-dd",
            outputFormat:"dd/MM/yy",
        }},
    ],
    ajaxURL: url_certificat,
})
const enregistrerProfil = $("enregistrerProfil");

enregistrerProfil.addEventListener("click", (e) => {

    const url = $("information-personnelles").dataset.url;
    const csrftoken = document.querySelector("[name='csrfmiddlewaretoken']").value;

    const formData = new FormData();

    formData.append("nom", $("nom").value);
    formData.append("prenom", $("prenom").value);
    formData.append("email", $("email").value);
    formData.append("telephone", $("telephone").value);
    formData.append("adresse", $("adresse").value);
    formData.append("sexe", $("sexe").value);
    formData.append("date_naissance", $("date_naissance").value);

    fetch(url, {
        method: "POST",
        headers: {
            "X-CSRFToken": csrftoken
        },
        body: formData
    })
    .then(response => response.json())
    .then(data => alert(data.message))
    .catch(error => console.error("Erreur:", error));
});