{#
/**
 * 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 js_chosen_id is not defined %}
    {% set js_chosen_id = "#id_adh" %}
{% endif %}
    var _current_page = 1;

    var _chosenPages = function() {
            // Hide or enable pagination buttons.
            {% if members.filters.pages is defined and members.filters.pages > 1 %}
                $('.next-results').removeClass('disabled');
                $('.next-results, .prev-results').removeClass('displaynone');
            {% else %}
                $('.next-results, .prev-results').addClass('displaynone');
            {% endif %}

            // Next results
            $('{{ js_chosen_id }} .next-results').off().on('click', function() {
                var _data = {
                    page: _current_page + 1,
                };
                var _selected_value = $('{{ js_chosen_id }}').dropdown('get value');
                var _selected_text = $('{{ js_chosen_id }}').dropdown('get text');
                {% if members.filters.pages is defined %}
                    var _pages = {{ members.filters.pages }};
                {% endif %}

                $.ajax({
                    url: '{{ url_for("contributionMembers") }}',
                    method: 'post',
                    data: _data,
                    {% include "elements/js/loader.js.twig" with {
                        loader: "action",
                        loader_text: "",
                        selector: ".menu.visible",
                        extra_beforesend: "$('.next-results').addClass('disabled');",
                        extra_complete: "if (_pages > _current_page) {$('.next-results').removeClass('disabled');}"
                    } %},
                    success: function(res) {
                        // Reload dropdown's items with new results.
                        var _values = Object.values(res)[0];
                        $('{{ js_chosen_id }}').dropdown('change values', _values);

                        // Restore selected item.
                        if (_selected_value != '') {
                            $('{{ js_chosen_id }}').dropdown('set value', _selected_value);
                            $('{{ js_chosen_id }}').dropdown('set text', _selected_text);
                        }

                        // New current page number.
                        _current_page += 1;

                        // Disable/Enable required pagination buttons.
                        if (_pages == _current_page) {
                            $('.next-results').addClass('disabled');
                        }
                        if (_current_page > 1) {
                            $('.prev-results').removeClass('disabled');
                        }
                    },
                    error: function() {
                        {% include "elements/js/modal.js.twig" with {
                            modal_title_twig: _T("An error occurred retrieving members :(")|e("js"),
                            modal_without_content: true,
                            modal_class: "mini",
                            modal_deny_only: true,
                            modal_cancel_text: _T("Close")|e("js"),
                            modal_classname: "redalert",
                        } %}
                    }
                });
            });

            // Previous results
            $('{{ js_chosen_id }} .prev-results').off().on('click', function() {
                var _data = {
                    page: _current_page - 1,
                };
                var _selected_value = $('{{ js_chosen_id }}').dropdown('get value');
                var _selected_text = $('{{ js_chosen_id }}').dropdown('get text');
                {% if members.filters.pages is defined %}
                    var _pages = {{ members.filters.pages }};
                {% endif %}

                $.ajax({
                    url: '{{ url_for("contributionMembers") }}',
                    method: 'post',
                    data: _data,
                    {% include "elements/js/loader.js.twig" with {
                        loader: "action",
                        loader_text: "",
                        selector: ".menu.visible",
                        extra_beforesend: "$('.prev-results').addClass('disabled');",
                        extra_complete: "if (_current_page > 1) {$('.prev-results').removeClass('disabled');}"
                    } %},
                    success: function(res) {
                        // Reload dropdown's items with new results.
                        var _values = Object.values(res)[0];
                        $('{{ js_chosen_id }}').dropdown('change values', _values);

                        // Restore selected item.
                        if (_selected_value != '') {
                            $('{{ js_chosen_id }}').dropdown('set value', _selected_value);
                            $('{{ js_chosen_id }}').dropdown('set text', _selected_text);
                        }

                        // New current page number.
                        _current_page -= 1;

                        // Disable/Enable required pagination buttons.
                        if (_pages > _current_page) {
                            $('.next-results').removeClass('disabled');
                        }
                        if (_current_page == 1) {
                            $('.prev-results').addClass('disabled');
                        }
                    },
                    error: function() {
                        {% include "elements/js/modal.js.twig" with {
                            modal_title_twig: _T("An error occurred retrieving members :(")|e("js"),
                            modal_without_content: true,
                            modal_class: "mini",
                            modal_deny_only: true,
                            modal_cancel_text: _T("Close")|e("js"),
                            modal_classname: "redalert",
                        } %}
                    }
                });
            });
    }

    var _adhselect = function() {
        $('{{ js_chosen_id }}').dropdown({
            match: 'text',
            apiSettings: {
                cache: false,
                url: '{{ url_for("contributionMembers", {"page": 1, "search": "{query}"}) }}',
                method: 'post',
                beforeSend: function (settings) {
                    if (settings.urlData.query == '') {
                        settings.url = settings.url.replace(/\/{query}/, '');
                    }
                    return settings;
                },
                onFailure: function(response, element, xhr) {
                    console.log(response);
                },
                onError: function(errorMessage, element, xhr) {
                    {% include "elements/js/modal.js.twig" with {
                        modal_title_twig: _T("An error occurred :(")|e("js"),
                        modal_content: "errorMessage",
                        modal_class: "tiny",
                        modal_content_class: "scrolling",
                        modal_deny_only: true,
                        modal_cancel_text: _T("Close")|e("js"),
                        modal_classname: "redalert",
                    } %}
                }
            },
            onShow: function() {
                _chosenPages();
            },
            onChange: function(text, value) {
                _chosenPages();
                {% if new_contrib_onchange is defined %}
                    var _member = $('#id_adh_input').val();
                    var _fee    = $('#id_type_cotis_input').val();
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        url : '{{ url_for("contributionDates") }}',
                        data: {
                            member_id: _member,
                            fee_id: _fee
                        },
                        {% include "elements/js/loader.js.twig" with {
                            selector: '#contribdetails'
                        } %},
                        success: function(res){
                            $('#contribution-rangeend').calendar('set date', res.date_debut_cotis);
                            $('#membership-rangeend').calendar('set date', res.date_fin_cotis);
                        },
                        error: function() {
                            {% include "elements/js/modal.js.twig" with {
                                modal_title_twig: _T("An error occurred retrieving dates :(")|e("js"),
                                modal_without_content: true,
                                modal_class: "mini",
                                modal_deny_only: true,
                                modal_cancel_text: _T("Close")|e("js"),
                                modal_classname: "redalert",
                            } %}
                        }
                    });
                {% endif %}
            },
            onHide: function() {
                // Reset to defaults.
                _current_page = 1;
                $('.next-results, .prev-results').addClass('disabled');
            },
        });

        var _chosen_id_input = $('{{ js_chosen_id }}_input');
        if ( typeof(_chosen_id_input) != 'undefined' && _chosen_id_input != null ) {
            _chosen_id_input.prop('type', 'hidden');
        }
    }

    $(function() {
        _adhselect();
    });
