jQuery(document).ready(function () {
    initDeleteAction();

    initResetFilters();

    initAutocompleteField();

    initDropdown();
});

function initDeleteAction() {

    $('.action-delete').each(function () {
        $(this).on('click', function (e) {
            e.preventDefault();
            const id = $(this).parents('tr').first().data('id');

            $('#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', deleteForm.attr('action').replace('__id__', id));
                    deleteForm.trigger('submit');
                });
        });
    });


    $('.action-duplicate').each(function () {
        $(this).on('click', function (e) {
            return confirm("Press a button!");
        });
    });
}


function initResetFilters() {
    $('.lc-reset-filters').on('click', function () {
        $(this).parents('form').find('.form-control').val('').trigger('change');

    })

}

function initAutocompleteField() {


    var autocompleteFields = $('[data-lc-autocomplete-url]');

    autocompleteFields.each(function () {
        var $this = $(this),
            url = $this.data('lc-autocomplete-url');

        $this.autoComplete({
            noResultsText: 'Aucun résultat n\'a été trouvé.',
            resolverSettings: {
                url: url
            }
        });
    });
}


function initDropdown(){
    $('.dropdown-toggle').hover(function() {
        $(this).find('.dropdown-menu').stop(true, true).delay(50).fadeIn();
    }, function() {
        $(this).find('.dropdown-menu').stop(true, true).delay(50).fadeOut();
    });
}
