$(function() {
    $('.delete-group').click(function() {
        if($(this).hasClass('disabled')) return false;

        const $delete   = $(this);
        const deleteUrl = $delete.data('deleteurl');
        const checkUrl  = $delete.data('checkurl');

        $delete.addClass('disabled');

        $.ajaxSettings.async = false;
        $.get(checkUrl, function(checkResult) {
            if(checkResult) {
                new $.zui.ModalTrigger({title: $delete.data('title'), custom: checkResult}).show();
            } else {
                bootbox.confirm(v.lang.confirmDelete, function(result) {
                    if(result) {
                        $.getJSON(deleteUrl, function(response)
                        {
                            if(response.result == 'success')
                            {
                                bootbox.alert(response.message, function(){location.reload()});
                            }
                            else
                            {
                                bootbox.alert(response.message);
                            }
                        })
                    };
                });
            }
        });
        $.ajaxSettings.async = true;

        $delete.removeClass('disabled');

        return false;
    });
})
