<script>
    //submit form_cours
    var classe_id = 0;

    function get_id(id) {
        classe_id = id;
    }

    function getCoursId(id) {
        cours_id = id;
        //console.log(cours_id)
    }

    function getClasseLib(libelle) {
        classe_libelle = libelle;
        //console.log(classe_libelle)
    }

    function get_classe_slug(slug) {
        classe_slug = slug;
    }

    $('#submit_form_cours').on('click', function() {
        //Marque des absents
        var lesAbsents = [];
        var checkboxes = $('.checkbox_absence:checked')
        Array.prototype.forEach.call(checkboxes, element => {
            lesAbsents.push(element.value)
        });
        $('#form_cours').validate({
            rules: {
                description: {
                    required: true,
                },
                prof: {
                    required: true,
                },
                matiere: {
                    required: true,
                },
                debut: {
                    required: true,
                },
                fin: {
                    required: true,
                },
            },
            messages: {
                description: {
                    required: "La description du cours est obligatoire!",
                },
                prof: {
                    required: "Précisez le professeur!",
                },
                matiere: {
                    required: "Précisez la matière!",
                },
                debut: {
                    required: "L'heure de début est obligatoire!",
                },
                fin: {
                    required: "L'heure de fin est obligatoire!",
                },
            },
            submitHandler: function() {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });

                $.ajax({
                    url: "/accueil/classe/" + classe_id + "/ajouterCours",
                    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: {
                        description: $('#description_id').val(),
                        prof: $('#prof_id').val(),
                        matiere: $('#matiere_id').val(),
                        debut: $('#debut_id').val(),
                        fin: $('#fin_id').val(),
                        classe_id: classe_id,
                        lesAbsents: lesAbsents,
                    },
                    success: function(response) {
                        if (response == true) {
                            window.location.replace(
                                '/accueil/classe/' + classe_slug);
                            classe_id = 0;
                            lesAbsents = [];
                            classe_slug = "";
                        }
                    },
                    error: function(response) {
                        console.log(response);
                    }
                });
            }
        });
    });

    //Suppression cours
    var cours_id = 0;
    var classe_libelle = "";
    var classe_slug = "";

    $('.supp_cours').on('click', () => {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            url: "/accueil/classe/" + classe_slug + "/coursHistory/" + cours_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: {
                cours_id: cours_id,
            },
            success: function(response) {
                if (response == true) {
                    window.location.replace(
                        '/accueil/classe/' + classe_slug + '/coursHistory');
                    cours_id = 0;
                    classe_libelle = "";
                    classe_slug = "";
                }
            },
            error: function(response) {
                console.log(response);
            }
        });
    });

    //Modifier Cours
    function getOldDescrip(oldDesc) {
        $('#nv_description_id').val(oldDesc);
    }

    function getOldProf(oldProf) {
        $('#nv_prof_id option[value=' + oldProf + ']').prop('selected', true);
    }

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

    function getOldDebut(oldDebut) {
        $('#nv_debut_id').val(oldDebut);
    }

    function getOldFin(oldFin) {
        $('#nv_fin_id').val(oldFin);
    }

    //Modifier Cours
    $('#submit_form_modifier_cours').on('click', function(e) {
        $('#form_modifier_cours').validate({
            rules: {
                nv_description: {
                    required: true,
                },
                nv_prof: {
                    required: true,
                },
                nv_matiere: {
                    required: true,
                },
                nv_debut: {
                    required: true,
                },
                nv_fin: {
                    required: true,
                },
            },
            messages: {
                nv_description: {
                    required: "La description du cours est obligatoire!",
                },
                nv_prof: {
                    required: "Précisez le professeur!",
                },
                nv_matiere: {
                    required: "Précisez la matière!",
                },
                nv_debut: {
                    required: "L'heure de début est obligatoire!",
                },
                nv_fin: {
                    required: "L'heure de fin est obligatoire!",
                },
            },
            submitHandler: function() {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                $.ajax({
                    url: "/accueil/classe/" + classe_slug + "/coursHistory/" + cours_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: {
                        cours_id: cours_id,
                        classe_libelle: classe_libelle,
                        nv_description: $('#nv_description_id').val(),
                        nv_prof: $('#nv_prof_id').val(),
                        nv_matiere: $('#nv_matiere_id').val(),
                        nv_debut: $('#nv_debut_id').val(),
                        nv_fin: $('#nv_fin_id').val(),
                    },
                    success: function(response) {
                        if (response == true) {
                            window.location.replace(
                                '/accueil/classe/' + classe_slug +
                                '/coursHistory/');
                            cours_id = 0;
                            classe_libelle = "";
                            classe_slug = "";
                        }
                    },
                    error: function(response) {
                        console.log(response);
                    }
                });
            }
        })
    });
</script>
