window.addEventListener('load', (event) => {

    // @TODO : cela met le focus sur le premier select2 du document
    /*$(document).on('select2:open', () => {
        document.querySelector('.select2-search__field').focus();
    });*/

    SovNotification.init();

    SovWidgets.setDateRange();

    $('.btn-confirm-js, .action-confirm').click(function () {
        return confirm('Êtes-vous sûr de vouloir réaliser cette action ?');
    });

    /* Tooltip */
    $('[data-toggle="tooltip"]').tooltip();

    if ($('.form-select, .select2, select.form-control').length) {


        $('form .form-widget>select.form-control, .select2, .form-select').each(function (i, elm) {
            if (!$(this).hasClass('disable-select2')) {
                SovWidgets.setSelect2($(elm));
            }
        });

        $('form select.form-control').each(function (i, elm) {
            if (!$(this).hasClass('disable-select2')) {
                SovWidgets.setSelect2($(elm));
            }
        });
    }

    $('.action-delete').on('click', function (e) {
        e.preventDefault();
        const formAction = $(this).attr('formaction');

        $('#modal-delete').modal({backdrop: true, keyboard: true})
            .off('click', '#modal-delete-button')
            .on('click', '#modal-delete-button', function () {
                let deleteForm = $('#delete-form');
                deleteForm.attr('action', formAction);
                deleteForm.submit();
            });
    });


    var elements = $('.lc-ckeditor');

    if (elements.length) {
        for (var i = 0; i < elements.length; ++i) {
            if ($(elements[i]).data('config') === 'simple_config') {
                CKEDITOR.replace(elements[i], {
                    "toolbar": [
                        {
                            items: ['Bold', 'Italic', 'Underline', 'Strike']
                        },
                        {
                            items: ["TextColor"]
                        }
                    ],
                    "language": "fr",
                    'height': 100
                });
            } else {
                CKEDITOR.replace(elements[i], {
                    "toolbar": [
                        {
                            name: "styles",
                            items: ["Format", 'Bold', 'Italic', 'Underline', 'Strike', "Link", "BulletedList"]
                        },
                        {
                            items: ["TextColor"]
                        },
                        {name: 'paragraph', items: ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock']},
                        {name: 'insert', items: ['Image', 'SpecialChar']},
                        {name: "document", items: ["Source"]},
                    ],
                    "language": "fr"
                });
            }

        }
    }

    SovWidgets.setAutoCompleteField();


});
