<script>
    var notes_eleves = []
    var lesEleves = []
    var classe_libelle = "";
    var classe_slug = "";

    function recupNotes() {
        notes_eleves = []
        lesEleves = []
        classe_libelle = ""
        Array.prototype.forEach.call($(':input.note_eleve'), element => {
            notes_eleves.push((element.value));
        })
        Array.prototype.forEach.call($(':input.note_eleve'), element => {
            lesEleves.push(parseInt(element.id, 10));
        })
    }

    function getClasseLibelle(libelle) {
        classe_libelle = libelle;
    }

    function get_classe_slug(slug) {
        classe_slug = slug;
    }

    <?php if($classe->type == 'Primaire'): ?>
        $('#matiere_evaluee_id').on('change', function() {
            let max = $('#matiere_evaluee_id option:selected').attr('class');

            $('.note_eleve').attr({
                'max': parseInt(max)
            });

            $('.note_eleve').attr({
                'placeholder': "/" + parseInt(max)
            })

        });
    <?php endif; ?>

    //Notes
    $('#submit_form_notes').on('click', function() {
        $('#form_notes').validate({
            rules: {
                matiere: {
                    required: true,
                },
                type: {
                    required: true,
                },
                date: {
                    required: true,
                },
                semestre: {
                    required: true,
                },
            },
            messages: {
                matiere: {
                    required: "La matière est obligatoire!",
                },
                type: {
                    required: "Le type est obligatoire!",
                },
                date: {
                    required: "La date est obligatoire!",
                },
                semestre: {
                    required: "Le champ est obligatoire!",
                },
            },
            submitHandler: function() {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]')
                            .attr('content')
                    }
                });

                $.ajax({
                    url: "/accueil/classe/" + classe_slug + "/enregistrerNotes",
                    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: {
                        matiere: $('#matiere_evaluee_id').val(),
                        type: $('#type_evaluation_id').val(),
                        semestre: $('#semestre_id').val(),
                        date: $('#date_evaluation_id').val(),
                        notes: notes_eleves,
                        eleves: lesEleves,
                        classe_libelle: classe_libelle,
                    },
                    success: function(response) {
                        if (response == true) {
                            window.location.replace(
                                '/accueil/classe/' + classe_slug);
                            notes_eleves = [];
                            classe_libelle = "";
                            lesEleves = [];
                            classe_slug = "";
                        }
                    },
                    error: function(response) {
                        console.log(response);
                    }
                });
            }
        });
    });

    var note_id = 0;
    var note_value = 0;
    var note_type = "";
    var eleve_id = 0;
    var eleve_slug = "";

    function get_note_id(id) {
        note_id = id;
    }

    function get_note_value(value) {
        $('#nv_valeur_id').val(value);
    }

    function get_note_type(type) {
        $('#nv_type_evaluation_id option[value=' + type + ']').prop('selected', true);
    }

    function get_eleve_id(id) {
        eleve_id = id;
    }

    function get_eleve_slug(slug) {
        eleve_slug = slug;
    }

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

    function getMax(max) {
        $('#nv_valeur_id').attr({
            'max': parseInt(max)
        });

        $('#nv_valeur_id').attr({
            'placeholder': "/" + parseInt(max)
        });
    }

    //Modifier note
    $('#submit_form_modifier_note').on('click', function(e) {
        $('#form_modifier_note').validate({
            rules: {
                nv_type_evaluation: {
                    required: true,
                },
                nv_valeur: {
                    required: true,
                },
            },
            messages: {
                nv_type_evaluation: {
                    required: "Précisez le type!",
                },
                nv_valeur: {
                    required: "Précisez la valeur!",
                },
            },
            submitHandler: function() {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                $.ajax({
                    url: "/accueil/classe/" + classe_slug + "/eleve/" + eleve_slug +
                        "/notes/" +
                        note_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: {
                        note_id: note_id,
                        nv_note_type: $('#nv_type_evaluation_id').val(),
                        nv_note_value: $('#nv_valeur_id').val(),
                    },
                    success: function(response) {
                        if (response == true) {
                            window.location.replace(
                                '/accueil/classe/' + classe_slug + '/eleve/' +
                                eleve_slug);
                            note_id = 0;
                            classe_slug = "";
                            eleve_slug = "";
                        }
                    },
                    error: function(response) {
                        console.log(response);
                    }
                });
            }
        })
    });

    //Ajouter note

    <?php if($classe->type == 'Primaire'): ?>
        $('#evaluation_id').on('change', function() {
            let max = $('#evaluation_id option:selected').attr('class');
            console.log(max)

            $('#valeur_note_id').attr({
                'max': parseInt(max)
            });

            $('#valeur_note_id').attr({
                'placeholder': "/" + parseInt(max)
            });

        });
    <?php endif; ?>

    $('#submit_form_ajouter_note').on('click', function(e) {
        $('#form_ajouter_note').validate({
            rules: {
                evaluation: {
                    required: true,
                },
                type_note: {
                    required: true,
                },
                valeur_note: {
                    required: true,
                },
            },
            messages: {
                evaluation: {
                    required: "Choisissez l'évaluation",
                },
                type_note: {
                    required: "Précisez le type!",
                },
                valeur_note: {
                    required: "Précisez la valeur!",
                },
            },
            submitHandler: function() {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                $.ajax({
                    url: "/accueil/classe/" + classe_slug + "/eleve/" + eleve_slug +
                        "/ajouterNote",
                    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: {
                        evaluation: $('#evaluation_id').val(),
                        note_type: $('#type_note_id').val(),
                        note_value: $('#valeur_note_id').val(),
                        eleve: eleve_id,
                    },
                    success: function(response) {
                        if (response == true) {
                            window.location.replace(
                                '/accueil/classe/' + classe_slug + '/eleve/' +
                                eleve_slug);
                        } else {
                            window.location.replace(
                                '/accueil/classe/' + classe_slug + '/eleve/' +
                                eleve_slug);
                        }
                    },
                    error: function(response) {
                        console.log(response);
                    }
                });
            }
        })
    });
</script>
<?php /**PATH /home/dide1435/luqman.upschool-sn.com/resources/views//partials/notesJs.blade.php ENDPATH**/ ?>