{#
/**
 * Copyright © 2003-2024 The Galette Team
 *
 * This file is part of Galette (https://galette.eu).
 *
 * Galette is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Galette is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
 */
#}
{% if selector is not defined %}
    {% set selector = '.delete' %}
{% endif %}
{% if loader is not defined %}
    {% set loader = 'action' %}
{% endif %}
{% if loader_selector is not defined %}
    {% set loader_selector = '.loader_selector' %}
{% endif %}

    //handle removals
{% if single_action is defined %}
    var _removeItems = function() {
        $('{{ selector }}').off('click').on('click', function(event) {
            event.preventDefault();
            var _this = $(this);
{% endif %}
{% if extra_check is defined %}
            {{ extra_check }}
{% endif %}
{% if method is not defined %}
    {% set method = 'GET' %}
{% endif %}
            $.ajax({
                url: {% if deleteurl is not defined %}_this.attr('href'){% else %}'{{ deleteurl|raw }}'{% endif %},
                type: "{{ method }}",
                data: {
                    ajax: true,
{% if extra_data is defined %}
                    {{ extra_data|raw }}
{% endif %}
                },
                datatype: 'json',
                {% include 'elements/js/loader.js.twig' with {
                    loader: loader,
                    selector: loader_selector
                } %},
                success: function(res){
                    var _res = $(res);
                    _res.find('form').on('submit', function(e) {
                        e.preventDefault();
                        var _form = $(this);
                        var _data = _form.serialize();
                        $.ajax({
                            url: _form.attr('action'),
                            type: "POST",
                            data: _data,
                            datatype: 'json',
                            {% include 'elements/js/loader.js.twig' with {
                                loader: loader,
                                selector: loader_selector
                            } %},
                            success: function(res){
                                if (res.success) {
                                    window.location.href = _form.find('input[name=redirect_uri]').val();
                                } else {
                                    $.ajax({
                                        url: '{{ url_for("ajaxMessages") }}',
                                        method: "GET",
                                        success: function (values) {
                                            for (var type in values) {
                                                var dtime = 0;
                                                if (type == 'success') {
                                                    dtime = 'auto';
                                                }
                                                $('body')
                                                    .toast({
                                                        displayTime: dtime,
                                                        minDisplayTime: 5000,
                                                        wordsPerMinute: 80,
                                                        showProgress: 'bottom',
                                                        closeIcon: true,
                                                        position: 'top attached',
                                                        title: values[type]['title'],
                                                        message: values[type]['messages'].join('<br/>'),
                                                        showIcon: values[type]['icon'],
                                                        class: type
                                                    })
                                                ;
                                            }
                                        }
                                    });
                                }
                            },
                            error: function() {
                                {% include "elements/js/modal.js.twig" with {
                                    modal_title_twig: _T("An error occurred :(")|e("js"),
                                    modal_without_content: true,
                                    modal_class: "mini",
                                    modal_deny_only: true,
                                    modal_cancel_text: _T("Close")|e("js"),
                                    modal_classname: "redalert",
                                } %}
                            }
                        });
                    });

                    var _title = _res.find('.header');

                    {% include "elements/js/modal.js.twig" with {
                        modal_title: "_title",
                        modal_class: "tiny",
                        modal_content: "_res",
                        modal_onapprove: "$('#confirm_removal form').submit();",
                        modal_approve_text: _T("Remove")|e('js'),
                        modal_approve_icon: 'trash',
                        modal_approve_color: 'red',
                        modal_classname: "redalert"
                    } %}
                },
                error: function() {
                    {% include "elements/js/modal.js.twig" with {
                        modal_title_twig: _T("An error occurred :(")|e("js"),
                        modal_without_content: true,
                        modal_class: "mini",
                        modal_deny_only: true,
                        modal_cancel_text: _T("Close")|e("js"),
                        modal_classname: "redalert",
                    } %}
                }
            });
{% if single_action is defined %}
        });
    }
    _removeItems();
{% endif %}
