<script>
    var cleave = new Cleave('#montant_operation_id', {
        delimiter: ' ',
        numeral: true,
        numeralThousandsGroupStyle: 'thousand'
    });

    var cleave = new Cleave('#nv_montant_operation_id', {
        delimiter: ' ',
        numeral: true,
        numeralThousandsGroupStyle: 'thousand'
    });

    //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";
        }

    }


    if (location.pathname == '/accueil/operations') {
        openContent(event, 'tout');
    }


    //Ajout operation
    $('#btn_submit_form_ajouter_operation').on('click', function() {
        $('#form_ajouter_operation').validate({
            rules: {
                libelle_operation: {
                    required: true,
                },
                type_operation: {
                    required: true,
                },
                montant_operation: {
                    required: true,
                },
                description_operation: {
                    required: false,
                }
            },
            messages: {
                libelle_operation: {
                    required: "Le libellé est obligatoire!",
                },
                type_operation: {
                    required: "Le type est obligatoire!",
                },
                montant_operation: {
                    required: "Le montant est obligatoire!",
                },
            },
            submitHandler: function() {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });

                $.ajax({
                    url: "/accueil/operations/ajouterOperation",
                    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_operation: $('#libelle_operation_id').val(),
                        type_operation: $('#type_operation_id').val(),
                        montant_operation: Number($('#montant_operation_id').val().replace(
                            /\s/g, '')),
                        description_operation: $('#description_operation_id').val(),
                    },
                    success: function(response) {
                        if (response == true) {
                            if (<?php echo e(auth()->user()->administrateur); ?> == 1) {
                                window.location.replace('/accueil/operations');
                            } else {
                                window.location.replace('/accueil');
                            }
                        }
                    },
                    error: function(response) {
                        console.log(response);
                    }
                });
            }
        });
    });

    //Rapport
    $('#submitFormRapport').on('click', function() {
        $('#formRapport').validate({
            rules: {
                debut: {
                    required: true,
                },
                fin: {
                    required: true,
                }
            },
            messages: {
                debut: {
                    required: "La date de début est obligatoire!",
                },
                fin: {
                    required: "La date de fin est obligatoire!",
                },
            },
            submitHandler: function() {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });

                $.ajax({
                    url: "/accueil/operations/rapport",
                    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: {
                        debut: $('#debutId').val(),
                        fin: $('#finId').val(),
                    },
                    success: function(response) {
                        if (response == true) {
                            window.open('/accueil/operations/rapport/' + $('#debutId').val() + '/' + $('#finId').val(), '_blank');
                        } else {
                            window.location.replace('/accueil/operations');
                        }
                    },
                    error: function(response) {
                        console.log(response);
                    }
                });
            }
        });
    });

    //Recuperation des info de l'operation
    var operation_id = 0;
    var operation_libelle = '';
    var operation_type = '';
    var operation_description = '';
    var montant_operation = 0;
    var statut = '';

    function get_id_operation(id) {
        operation_id = id;
    }

    function get_libelle_operation(libelle) {
        operation_libelle = libelle;
        $('#nv_libelle_operation_id').val(operation_libelle);
    }

    function get_type_operation(type) {
        operation_type = type;
        if (operation_type == 'encaissement') {
            $('#value1').val('Encaissement');
            $('#value1').html('Encaissement');
            $('#value2').val('depense');
            $('#value2').html('Dépense');
        } else {
            $('#value1').val('depense');
            $('#value1').html('Dépense');
            $('#value2').val('encaissement');
            $('#value2').html('Encaissement');
        }
    }

    function get_montant(montant) {
        montant_operation = montant;
        $('#nv_montant_operation_id').val(montant_operation);
    }

    function get_descr(descr) {
        operation_description = descr;
        $('#nv_description_operation_id').val(operation_description);
    }

    //Modifier operation
    $('#btn_submit_form_modifier_operation').on('click', function(e) {
        $('#form_modifier_operation').validate({
            rules: {
                nv_libelle_operation: {
                    required: true,
                },
                nv_libelle_operation: {
                    required: true,
                },
                nv_montant_operation: {
                    required: true,
                },
                nv_description_operation: {
                    required: false
                }
            },
            messages: {
                nv_libelle_operation: {
                    required: "Donnez le libellé svp!",
                },
                nv_libelle_operation: {
                    required: "Précisez le type svp!",
                },
                nv_montant_operation: {
                    required: "Précisez le montant svp!",
                },
            },
            submitHandler: function() {
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                $.ajax({
                    url: "/accueil/operations/operation/" + operation_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_operation: $('#nv_libelle_operation_id').val(),
                        nv_type_operation: $('#nv_type_operation_id').val(),
                        nv_description_operation: $('#nv_description_operation_id').val(),
                        nv_montant_operation: Number($('#nv_montant_operation_id').val()
                            .replace(/\s/g, ''))
                    },
                    success: function(response) {
                        if (response == true) {
                            window.location.replace(
                                '/accueil/operations');
                        }
                    },
                    error: function(response) {
                        console.log('Transaction non modifiée');
                    }
                });
            }
        })
    });

    //Supprimer operation
    $('.btn_suppr_operation').on('click', () => {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            url: "/accueil/operations/operation/" + operation_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/operations');
                } else {
                    console.log('La transaction n\'est pas supprimée');
                }
            },
            error: function(response) {
                console.log(response);
            }
        });
    });

    //Get value for details operation
    function get_ref_det(ref) {
        $('#details_ref_operation').html(ref);
    }

    function get_libelle_det(libelle) {
        $('#details_libelle_operation').html(libelle);
    }

    function get_montant_det(montant) {
        $('#details_montant_operation').html(Intl.NumberFormat().format(montant));
    }

    function get_descr_det(descr) {
        $('#details_description_operation').html(descr);
    }

    function get_date_det(date) {
        $('#details_date_operation').html(date);
    }

    function get_type_det(type) {
        $('#details_type_operation').html(type);
    }

    function get_author_det(author) {
        $('#details_auteur_operation').html(author);
    }

    function get_update_det(update) {
        $('#details_update_operation').html(update);
    }

    function get_statut_det(statut) {
        $('#details_statut_operation').html(statut);
    }
</script>
<?php /**PATH /home/dide1435/luqman.upschool-sn.com/resources/views//partials/operationJs.blade.php ENDPATH**/ ?>