<script>
    var eval_id = 0;
    var classe_libelle = "";
    var classe_slug = "";

    function get_classe_slug(slug) {
        classe_slug = slug;
    }

    function get_eval_id(id) {
        eval_id = id;
        //console.log(id)
    }

    function get_classe_libelle(libelle) {
        classe_libelle = libelle;
    }

    function getOldDate(date) {
        $('#nv_date_id').val(date);
        //console.log(date)
    }

    function getOldMatiere(matiere) {
        $('#nv_matiere_id option[value=' + matiere + ']').prop('selected', true);
        //console.log(matiere)
    }

    function getOldSemestre(semestre) {
        $('#nv_semestre_id option[value=' + semestre + ']').prop('selected', true);
        //console.log(matiere)
    }

    //Supprimer evaluation
    $('.supp_eval').on('click', () => {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            url: "/accueil/classe/" + classe_slug + "/evaluationHistory/" + eval_id + "/supprimer",
            type: "POST",
            dataType: 'json',
            beforeSend: function() {
                $('#loader').removeClass('is-hidden')
                $('*').css('pointer-events', 'none')
            },
            complete: function() { // Set our complete callback, adding the .hidden class and hiding the spinner.
                $('#loader').addClass('is-hidden')
                $('*').css('pointer-events', 'all')
            },
            data: {
                eval_id: eval_id,
            },
            success: function(response) {
                if (response == true) {
                    window.location.replace(
                        '/accueil/classe/' + classe_slug + '/evaluationHistory');
                    eval_id = 0;
                    classe_libelle = "";
                    classe_slug = "";
                }
            },
            error: function(response) {
                console.log(response);
            }
        });
    });

    //Modifier Evaluation
    $('#submit_form_modifier_evaluation').on('click', function(e) {
        $('#form_modifier_evaluation').validate({
            rules: {
                nv_matiere: {
                    required: true,
                },
                nv_date: {
                    required: true,
                },
                nv_semestre: {
                    required: true
                },
            },
            messages: {
                nv_matiere: {
                    required: "Précisez la matière!",
                },
                nv_date: {
                    required: "La date est obligatoire!",
                },
                nv_semestre: {
                    required: "Le semestre est obligatoire!"
                },
            },
            submitHandler: function() {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                $.ajax({
                    url: "/accueil/classe/" + classe_slug + "/evaluationHistory/" +
                        eval_id +
                        "/modifier",
                    type: "POST",
                    dataType: 'json',
                    beforeSend: function() {
                        $('#loader').removeClass('is-hidden')
                        $('*').css('pointer-events', 'none')
                    },
                    complete: function() { // Set our complete callback, adding the .hidden class and hiding the spinner.
                        $('#loader').addClass('is-hidden')
                        $('*').css('pointer-events', 'all')
                    },
                    data: {
                        eval_id: eval_id,
                        classe_libelle: classe_libelle,
                        nv_matiere: $('#nv_matiere_id').val(),
                        nv_date: $('#nv_date_id').val(),
                        nv_semestre: $('#nv_semestre_id').val(),
                    },
                    success: function(response) {
                        if (response == true) {
                            window.location.replace(
                                '/accueil/classe/' + classe_slug +
                                '/evaluationHistory/');
                            eval_id = 0;
                            classe_libelle = "";
                            classe_slug = "";
                        }
                    },
                    error: function(response) {
                        console.log(response);
                    }
                });
            }
        })
    });
</script>
