const bouton_enregistrer_detail = document.getElementById("bouton-enregistrer-detail-conge");

if(bouton_enregistrer_detail){
    bouton_enregistrer_detail.addEventListener("click", () => {
        const form = document.getElementById("form-detail-conge");
        const csrftoken = new FormData(form).get("csrfmiddlewaretoken");
        const actionUrl = form.action;
        const id_conge = document.getElementById("id_conge").value;
        const validation_hierarchique_input = document.querySelector('input[name="validation_hierarchique"]:checked');
        const validation_hierarchique = validation_hierarchique_input ? validation_hierarchique_input.value : null;
        const validation_direction_input = document.querySelector('input[name="validation_direction"]:checked');
        const validation_direction = validation_direction_input ? validation_direction_input.value : null;
        const motif_refus = document.getElementById("motif_refus").value;

        fetch(actionUrl, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "X-CSRFToken": csrftoken
            },
            body: JSON.stringify({
                id_conge,
                validation_hierarchique,
                validation_direction,
                motif_refus
            })
        })
        .then(response => response.json())
        .then(data => {
            alert(data.message);
            navigation.reload();
        });
    })
}

if(document.getElementById("validation_hierarchique_refuse")){
    document.getElementById("validation_hierarchique_refuse").addEventListener('click', function(){
        if(this.checked){
            alert("coucou");
            document.getElementById("motif_refus_container").className="d-block form-group mt-3";
        }else{
            document.getElementById("motif_refus_container").className="d-none";
        }
    })
}

if(document.getElementById("validation_hierarchique_refuse")){
    document.getElementById("validation_hierarchique_refuse").addEventListener('click', function(){
        if(this.checked){
            document.getElementById("motif_refus_container").className="d-block form-group mt-3";
        }else{
            document.getElementById("motif_refus_container").className="d-none";
        }
    })
}

if(document.getElementById("validation_hierarchique_valide")){
    document.getElementById("validation_hierarchique_valide").addEventListener('click', function(){
        if(this.checked){
            document.getElementById("motif_refus_container").className="d-none";
        }else{
            document.getElementById("motif_refus_container").className="d-block form-group mt-3";
        }
    })
}

if(document.getElementById("validation_direction_valide")){
    document.getElementById("validation_direction_valide").addEventListener('click', function(){
        if(this.checked){
            document.getElementById("motif_refus_container").className="d-block form-group mt-3";
        }else{
            document.getElementById("motif_refus_container").className="d-none";
        }
    })
}