<script>
    //Fonction pour le panel tab
    function openContent(evt, contentId) {

        var i, tabcontent, tablinks;


        tabcontent = document.getElementsByClassName("contenu-block");

        for (i = 0; i < tabcontent.length; i++) {
            tabcontent[i].style.display = "none";
            //console.log(tabcontent[i])
        }


        tablinks = document.getElementsByClassName("tab");
        for (i = 0; i < tablinks.length; i++) {
            tablinks[i].className = tablinks[i].className.replace("is-active", "");
            //console.log(tablinks[i])
        }


        document.getElementById(contentId).style.display = "flex";
        if (evt == null) {
            document.getElementById('default').className += " is-active";
        } else {
            evt.currentTarget.className += " is-active";
        }

    }
    openContent(event, 'tout');

    //Ajout matiere
    $('#btn_submit_form_ajouter_matiere').on('click', function() {
        $('#form_ajouter_matiere').validate({
            rules: {
                libelle_matiere: {
                    required: true,
                },
                niveau_matiere: {
                    required: true,
                },
                coef_matiere: {
                    required: true,
                },
            },
            messages: {
                libelle_matiere: {
                    required: "Le libellé est obligatoire!",
                },
                niveau_matiere: {
                    required: "Le niveau est obligatoire!",
                },
                coef_matiere: {
                    required: "Le coefficient est obligatoire!",
                },
            },
            submitHandler: function() {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });

                $.ajax({
                    url: "/accueil/matieres/ajouterMatiere",
                    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: {
                        libelle_matiere: $('#libelle_matiere_id').val(),
                        niveau_matiere: $('#niveau_matiere_id').val(),
                        coef_matiere: $('#coef_matiere_id').val(),
                    },
                    success: function(response) {
                        if (response == true) {
                            window.location.replace('/accueil/matieres');
                        }
                    },
                    error: function(response) {
                        console.log(response);
                    }
                });
            }
        });
    });

    //Recuperation des info de la matiere
    var matiere_id = 0;
    var matiere_libelle = '';
    var matiere_niveau = '';
    var matiere_coef = 0;

    function get_id_matiere(id) {
        matiere_id = id;
    }

    function get_libelle_matiere(libelle) {
        matiere_libelle = libelle;
        $('#nv_libelle_matiere_id').val(matiere_libelle);
    }

    function get_niveau_matiere(niveau) {
        matiere_niveau = niveau;
        if (matiere_niveau == 'Primaire') {
            $('#value1').val('Primaire');
            $('#value1').html('Primaire');
            $('#value2').val('Secondaire');
            $('#value2').html('Secondaire');
        } else {
            $('#value1').val('Secondaire');
            $('#value1').html('Secondaire');
            $('#value2').val('Primaire');
            $('#value2').html('Primaire');
        }
    }

    function get_coef(coef) {
        matiere_coef = coef;
        $('#nv_coef_matiere_id').val(matiere_coef);
    }

    //Modifier matiere
    $('#btn_submit_form_modifier_matiere').on('click', function(e) {
        $('#form_modifier_matiere').validate({
            rules: {
                libelle: {
                    required: true,
                },
                niveau: {
                    required: true,
                }
            },
            messages: {
                libelle: {
                    required: "Donnez le libellé svp!",
                },
                niveau: {
                    required: "Précisez le niveau svp!",
                },
            },
            submitHandler: function() {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                $.ajax({
                    url: "/accueil/matieres/matiere/" + matiere_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: {
                        nv_libelle_matiere: $('#nv_libelle_matiere_id').val(),
                        nv_niveau_matiere: $('#nv_niveau_matiere_id').val(),
                        nv_coef_matiere: $('#nv_coef_matiere_id').val(),
                    },
                    success: function(response) {
                        if (response == true) {
                            window.location.replace(
                                '/accueil/matieres/');
                        }
                    },
                    error: function(response) {
                        console.log('Matière non modifiée');
                    }
                });
            }
        })
    });

    //Supprimer matiere
    $('.btn_suppr').on('click', () => {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            url: "/accueil/matieres/matiere/" + matiere_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: {},
            success: function(response) {
                if (response == true) {
                    window.location.replace(
                        '/accueil/matieres');
                } else {
                    console.log('La matière n\'est pas supprimée');
                }
            },
            error: function(response) {
                console.log(response);
            }
        });
    });
</script>
<?php /**PATH /home/dide1435/luqman.upschool-sn.com/resources/views//partials/matieresJs.blade.php ENDPATH**/ ?>