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;
    fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': csrftoken
        },
        body: JSON.stringify({ 
            "nom": $("nom").value,
            "prenom": $("prenom").value,
            "email": $("email").value,
            "telephone": $("telephone").value,
            "adresse": $("adresse").value,
            "sexe": $("sexe").value,
            "date_naissance": $("date_naissance").value,
        })
    })
    .then(response => response.json())
    .then(data => alert(data.message))
})