var selectedRow = null

function onFormSubmit() {
    var formData = readFormData();
    if (selectedRow == null)
        insertNewRecord(formData);
    else
        updateRecord(formData);
    resetForm();
}

function readFormData() {
    var formData = {};
    formData["login"] = document.getElementById("login").value;
    formData["age"] = document.getElementById("age").value;
    formData["poids"] = document.getElementById("poids").value;
    formData["taille"] = document.getElementById("taille").value;
    formData["M"] = document.getElementById("M").checked;
    formData["F"] = document.getElementById("F").checked;
    formData["bas"] = document.getElementById("bas").checked;
    formData["moyen"] = document.getElementById("moyen").checked;
    formData["eleve"] = document.getElementById("eleve").checked;
    return formData;
}

function insertNewRecord(data) {
    var table = document.getElementById("table_récap").getElementsByTagName('tbody')[0];
    var newRow = table.insertRow(table.length);
    var genre;
    var sport;
    var calori;
    cell1 = newRow.insertCell(0);
    cell1.innerHTML = data.login;
    cell2 = newRow.insertCell(1);
    cell2.innerHTML = data.age;
    cell3 = newRow.insertCell(2);
    cell3.innerHTML = data.poids;
    cell4 = newRow.insertCell(3);
    cell4.innerHTML = data.taille;
    cell5 = newRow.insertCell(4);
    if (data.M) {
      cell5.innerHTML = "Masculin";
      genre ="Masc" ;
    } else {
      cell5.innerHTML = "Féminin";
      genre ="Fem";
    }
    cell6 = newRow.insertCell(5);
    if (data.bas) {
      cell6.innerHTML = "Bas";
      sport = 1.3;
    } else if(data.moyen) {
      cell6.innerHTML = "Moyen";
      sport = 1.55;
    } else {
      cell6.innerHTML = "Élevé";
      sport = 1.75;
    }
    cell7 = newRow.insertCell(6);
    if(genre=="Masc"){
      cell7.innerHTML = (13.707*data.poids+492.3*data.taille+77.607-6.673*data.age)*sport;
    } else{
      cell7.innerHTML = (9.740*data.poids+172.9*data.taille+667.051-4.737*data.age)*sport;
    }
    
    cell8 = newRow.insertCell(7);
    cell8.innerHTML = `<a onClick="onEdit(this)">Edit</a>
                       <a onClick="onDelete(this)">Delete</a>`;
    
}

function resetForm() {
    document.getElementById("login").value = "";
    document.getElementById("age").value = "";
    document.getElementById("poids").value = "";
    document.getElementById("taille").value = "";
    document.getElementById("M").checked = false;
    document.getElementById("F").checked = false;
    document.getElementById("bas").checked = false;
    document.getElementById("moyen").checked = false;
    document.getElementById("eleve").checked = false;
    selectedRow = null;
}

function onEdit(td) {
    selectedRow = td.parentElement.parentElement;
    document.getElementById("login").value = selectedRow.cells[0].innerHTML;
    document.getElementById("age").value = selectedRow.cells[1].innerHTML;
    document.getElementById("poids").value = selectedRow.cells[2].innerHTML;
    document.getElementById("taille").value = selectedRow.cells[3].innerHTML;
    if (selectedRow.cells[4].innerHTML == "Masuclin") {
        document.getElementById("M").checked = true;
        document.getElementById("F").checked = false;
    } else if (selectedRow.cells[4].innerHTML == "Féminin"){
        document.getElementById("M").checked = false;
        document.getElementById("F").checked = true;
    }
    if (selectedRow.cells[5].innerHTML == "Bas") {
        document.getElementById("bas").checked = true;
        document.getElementById("moyen").checked = false;
        document.getElementById("eleve").checked = false;
    } else if (selectedRow.cells[5].innerHTML == "Moyen"){
        document.getElementById("bas").checked = false;
        document.getElementById("moyen").checked = true;
        document.getElementById("eleve").checked = false;
    } else if (selectedRow.cells[5].innerHTML == "Élevé"){
        document.getElementById("bas").checked = false;
        document.getElementById("moyen").checked = false;
        document.getElementById("eleve").checked = true;
    }
}

function updateRecord(formData) {
    selectedRow.cells[0].innerHTML = formData.login;
    selectedRow.cells[1].innerHTML = formData.age;
    selectedRow.cells[2].innerHTML = formData.poids;
    selectedRow.cells[3].innerHTML = formData.taille;
    if (formData.M) {
      selectedRow.cells[4].innerHTML = "Masculin";
    } else {
      selectedRow.cells[4].innerHTML = "Féminin";
    }
    if (formData.bas) {
        selectedRow.cells[5].innerHTML = "Bas";
      } else if (formData.moyen) {
        selectedRow.cells[5].innerHTML = "Moyen";
      } else if (formData.eleve) {
        selectedRow.cells[5].innerHTML = "Élevé";   }
}

function onDelete(td) {
    if (confirm('Vous voulez vraiment supprimer cette ligne ?')) {
        row = td.parentElement.parentElement;
        document.getElementById("table_récap").deleteRow(row.rowIndex);
        resetForm();
    }
}