{#
/**
 * 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/>.
 */
#}
{% extends (mode == 'ajax') ? "ajax.html.twig" : "page.html.twig" %}

{% block content %}
{% if members.list is defined or require_mass %}
    {% if mode != 'ajax' %}
        <form action="{% if contribution.id %}{{ url_for("doEditContribution", {"type": type, "id": contribution.id}) }}{% else %}{{ url_for("doAddContribution", {"type": type}) }}{% endif %}" enctype="multipart/form-data" method="post" class="ui form">
    {% endif %}
            <div class="ui styled fluid accordion field">
                <div class="active title">
                    <i class="jsonly displaynone icon dropdown" aria-hidden="true"></i>
    {% if type == constant('Galette\\Entity\\Contribution::TYPE_FEE') %}
                    {{ _T("Select contributor and membership fee type") }}
    {% else %}
                    {{ _T("Select contributor and donation type") }}
    {% endif %}
    {% if contribution.isTransactionPart() %}
                    <span class="ui teal horizontal label">
                        {{ _T("Transaction related") }}
                    </span>
    {% endif %}
    {% if contribution.hasSchedule() %}
                    <span class="ui teal horizontal label">
                        {{ _T("Has scheduled payments") }}
                    </span>
    {% endif %}
                </div>
                <div class="active content">
                    <div class="ui mobile reversed stackable grid">
                        <div class="{% if contribution.isTransactionPart() or contribution.hasSchedule() %}six wide {% endif %}column">
    {% if not require_mass %}
                            {% include 'components/forms/member_dropdown.html.twig' with {
                                'required': true,
                                'label': _T("Contributor:"),
                                'id': 'id_adh',
                                'value': contribution.member,
                                'component_class': 'inline field'
                            } %}
    {% endif %}
                            <div class="inline field{% if required.id_type_cotis is defined and required.id_type_cotis == 1 %} required{% endif %}">
                                <label for="id_type_cotis">{{ _T("Contribution type:") }}</label>
                                <div id="id_type_cotis" class="ui dropdown selection">
                                    {% if contribution.type %}
                                        {% set selectedid = contribution.type.id %}
                                    {% else %}
                                        {% set selectedid = null %}
                                    {% endif %}
                                    <input id="id_type_cotis_input" type="hidden" name="id_type_cotis" value="{{ selectedid }}"{% if required.id_type_cotis is defined and required.id_type_cotis == 1 %} required="required"{% endif %}>
                                    <i class="dropdown icon"></i>
                                    <div class="text">{% for key, values in type_cotis_options %}{% if key == selectedid %}{{ values.label }}{% endif %}{% endfor %}</div>
                                    <div class="menu">
                                    {% for key, values in type_cotis_options %}
                                        <div class="item{% if key == selectedid %} active selected{% endif %}" data-value="{{ key }}">{{ values.label }}</div>
                                    {% endfor %}
                                    </div>
                                </div>
                                {% if contribution.isTransactionPart() and not contribution.id %}
                                    {% set inverted_type %}
                                        {%- if type == constant('Galette\\Entity\\Contribution::TYPE_FEE') -%}
                                            {{ constant('Galette\\Entity\\Contribution::TYPE_DONATION') }}
                                        {%- else -%}
                                            {{ constant('Galette\\Entity\\Contribution::TYPE_FEE') }}
                                        {%- endif -%}
                                    {% endset %}
                                    {% set switch_title %}
                                        {%- if inverted_type == constant('Galette\\Entity\\Contribution::TYPE_FEE') -%}
                                            {{ _T('Switch to fee') }}
                                        {%- else -%}
                                            {{ _T('Switch to donation') }}
                                        {%- endif -%}
                                    {% endset %}
                                    <a
                                        href="{{ url_for("doAddContribution", {'type': inverted_type}) }}?trans_id={{ contribution.transaction.id }}&id_adh={{ contribution.member }}"
                                        class="ui basic icon button"
                                        title="{{ switch_title }}"
                                    >
                                        <i class="exchange icon" aria-hidden="true"></i>
                                        <span class="visually-hidden">{{ switch_title }}</span>
                                    </a>
                                {% endif %}
                            </div>
                        </div>
    {% if contribution.isTransactionPart() or contribution.hasSchedule() %}
                        <div class="ten wide column">
        {% if contribution.isTransactionPart() %}
                            <div class="ui yellow segment">
                                <div class="ui tiny header">
                                    <a
                                        href="{{ url_for("editTransaction", {"id": contribution.transaction.id}) }}"
                                        class="ui mini icon blue compact button tooltip"
                                    >
                                        <i class="eye icon tooltip" aria-hidden="true"></i>
                                        <span class="ui special popup">{{ _T("View transaction") }}</span>
                                    </a>
                                    {{ _T("Related transaction information") }}
                                </div>
                                <div class="content">
                                    <div class="ui relaxed horizontal list">
                                        <div class="item">
                                            <div class="content">
                                                <div class="header">{{ _T("Date") }}</div>
                                                {{ contribution.transaction.date }}
                                            </div>
                                        </div>
                                        <div class="item">
                                            <div class="content">
                                                <div class="header">{{ _T("Member") }}</div>
                                                {{ memberName({'id': contribution.transaction.member}) }}
                                            </div>
                                        </div>
                                        <div class="item">
                                            <div class="content">
                                                <div class="header">{{ _T("Amount") }}</div>
                                                {{ contribution.transaction.amount }}
                                            </div>
                                        </div>
            {% if contribution.transaction.getMissingAmount() > 0 %}
                                        <div class="item">
                                            <div class="content">
                                                <div class="header">{{ _T("Not dispatched amount") }}</div>
                                                {{ contribution.transaction.getMissingAmount() }}
                {% if contribution.id != '' %}
                                                    <a
                                                        href="{{ url_for("addContribution", {"type": constant('Galette\\Entity\\Contribution::TYPE_FEE')}) }}?trans_id={{ contribution.transaction.id }}"
                                                        class="ui mini icon green compact button tooltip"
                                                        title="{{ _T("Create a new fee that will be attached to the current transaction") }}"
                                                    >
                                                        <i class="plus tiny icon" aria-hidden="true"></i>
                                                        <i class="user check icon" aria-hidden="true"></i>
                                                        <span class="visually-hidden">{{ _T("Create a new fee that will be attached to the current transaction") }}</span>
                                                    </a>
                                                    <a
                                                        href="{{ url_for("addContribution", {"type": constant('Galette\\Entity\\Contribution::TYPE_DONATION')}) }}?trans_id={{ contribution.transaction.id }}"
                                                        class="ui mini icon green compact button tooltip"
                                                        title="{{ _T("Create a new donation that will be attached to the current transaction") }}"
                                                    >
                                                        <i class="plus tiny icon" aria-hidden="true"></i>
                                                        <i class="gift icon" aria-hidden="true"></i>
                                                        <span class="visually-hidden">{{ _T("Create a new donation that will be attached to the current transaction") }}</span>
                                                    </a>
                {% endif %}
                                            </div>
                                        </div>
            {% endif %}
                                    </div>
            {% if contribution.id == '' %}
                                    <div class="inline field">
                                        <label>{{ _T("Dispatch type:") }}</label>
                                        <i class="circular inverted primary small icon info tooltip" title="{{ _T("Select a contribution type to create for dispatch transaction") }}" aria-hidden="true"></i>
                                        <input type="radio" name="contrib_type" id="contrib_type_fee" value="{{ constant('Galette\\Entity\\Contribution::TYPE_FEE') }}"{% if type == constant('Galette\\Entity\\Contribution::TYPE_FEE') %} checked="checked"{% endif %}/> <label for="contrib_type_fee">{{ _T("Membership fee") }}</label>
                                        <input type="radio" name="contrib_type" id="contrib_type_donation" value="{{ constant('Galette\\Entity\\Contribution::TYPE_DONATION') }}"{% if type == constant('Galette\\Entity\\Contribution::TYPE_DONATION') %} checked="checked"{% endif %}/> <label for="contrib_type_donation">{{ _T("Donation") }}</label>
                                    </div>
            {% endif %}
                                </div>
                            </div>
        {% endif %}
        {% if contribution.hasSchedule() %}
            {% set scheduled_amount = scheduled.getAllocation(contribution.id) %}
                            <div class="ui yellow segment">
                                <div class="ui tiny header">
                                    <a
                                        href="#"
                                        class="ui mini icon blue compact button tooltip"
                                        id="scheduledslist"
                                    >
                                        <i class="eye icon tooltip" aria-hidden="true"></i>
                                        <span class="ui special popup">{{ _T("View scheduled payments") }}</span>
                                    </a>
                                    {{ _T("Scheduled payments") }}
                                </div>
            {% if not contribution.isScheduleFullyAllocated() %}
                                <div class="content">
                                    <div class="ui relaxed horizontal list">
                                        <div class="item">
                                            <div class="content">
                                                <div class="header">{{ _T("Amount") }}</div>
                                                {{ scheduled_amount }}
                                            </div>
                                        </div>
                                        <div class="item">
                                            <div class="content">
                                                <div class="header">{{ _T("Not dispatched amount") }}</div>
                                                {{ contribution.amount - scheduled_amount }}
                                                <a
                                                    href="{{ url_for("addScheduledPayment", {"id_cotis": contribution.id}) }}"
                                                    class="ui mini icon green compact button tooltip"
                                                    title="{{ _T("Create a new scheduled payment") }}"
                                                >
                                                    <i class="plus tiny icon" aria-hidden="true"></i>
                                                    <i class="money bill wave icon" aria-hidden="true"></i>
                                                    <span class="visually-hidden">{{ _T("Create a new scheduled payment") }}</span>
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
            {% endif %}
                            </div>
        {% endif %}
                        </div>
    {% endif %}
                    </div>
                </div>
            </div>

            <div class="ui styled fluid accordion field">
                <div class="active title">
                    <i class="jsonly displaynone icon dropdown" aria-hidden="true"></i>
    {% if type == constant('Galette\\Entity\\Contribution::TYPE_FEE') %}
                    {{ _T("Details of membership fee") }}
    {% else %}
                    {{ _T("Details of donation") }}
    {% endif %}
                </div>
                <div class="active content field">
                    <div id="contribdetails" class="ui basic fitted segment">
                        <div class="two fields">
                            <div class="field{% if required.montant_cotis is defined and required.montant_cotis == 1 %} required{% endif %}">
                                <label for="montant_cotis">{{ _T("Amount:") }}</label>
                                <input type="text" name="montant_cotis" id="montant_cotis" value="{{ contribution.amount }}" maxlength="10"{% if required.montant_cotis is defined and required.montant_cotis == 1 %} required="required"{% endif %}/>
                            </div>
                            {# payment type #}
                            {% set ptype = contribution.payment_type %}
                            {% if ptype == null %}
                                {% set ptype = preferences.pref_default_paymenttype %}
                            {% endif %}
                            {% include 'components/forms/payment_types.html.twig' with {
                                    'current': ptype,
                                    'varname': 'type_paiement_cotis',
                                    'disabled': contribution.hasSchedule()
                            } %}
                        </div>
                        <div class="{% if type == constant('Galette\\Entity\\Contribution::TYPE_FEE') %}three{% else %}two{% endif %} fields">
                            <div class="field{% if required.date_enreg is defined and required.date_enreg == 1 %} required{% endif %}">
                                <label for="date_enreg">
                                    {{ _T("Record date:") }}
                                </label>
                                <div class="ui calendar" id="contribution-rangestart">
                                    <div class="ui input left icon">
                                        <i class="calendar icon" aria-hidden="true"></i>
                                        <input type="text" name="date_enreg" id="date_enreg" value="{{ contribution.date }}" maxlength="10"{% if required.date_enreg is defined and required.date_enreg == 1 %} required="required"{% endif %} placeholder="{{ _T('yyyy-mm-dd format') }}" />
                                    </div>
                                </div>
                            </div>
                            <div class="field{% if required.date_debut_cotis is defined and required.date_debut_cotis == 1 %} required{% endif %}">
                                <label for="date_debut_cotis">
                                {% if type == constant('Galette\\Entity\\Contribution::TYPE_FEE') %}
                                    {{ _T("Start date of membership:") }}
                                {% else %}
                                    {{ _T("Date of contribution:") }}
                                {% endif %}
                                </label>
                                <div class="ui calendar" id="contribution-rangeend">
                                    <div class="ui input left icon">
                                        <i class="calendar icon" aria-hidden="true"></i>
                                        <input type="text" name="date_debut_cotis" id="date_debut_cotis" value="{{ contribution.begin_date }}" maxlength="10"{% if required.date_debut_cotis == 1 %} required="required"{% endif %} placeholder="{{ _T('yyyy-mm-dd format') }}" />
                                    </div>
                                </div>
                            </div>
            {% if type == constant('Galette\\Entity\\Contribution::TYPE_FEE') %}
                            <div class="field{% if required.date_fin_cotis is defined and required.date_fin_cotis == 1 %} required{% endif %}">
                {% if preferences.pref_membership_ext != "" %}
                                <label for="duree_mois_cotis">{{ _T("Membership extension:") }}</label>
                                <input type="text" name="duree_mois_cotis" id="duree_mois_cotis" value="{{ contribution.duration }}" maxlength="3"{% if required.date_fin_cotis is defined and required.date_fin_cotis == 1 %} required="required"{% endif %}/>
                                <span class="exemple">{{ _T("months") }}</span>
                {% else %}
                                <label for="date_fin_cotis">{{ _T("End date of membership:") }}</label>
                                <div class="ui calendar" id="membership-rangeend">
                                    <div class="ui input left icon">
                                        <i class="calendar icon" aria-hidden="true"></i>
                                        <input type="text" name="date_fin_cotis" id="date_fin_cotis" value="{{ contribution.end_date }}" maxlength="10"{% if required.date_fin_cotis is defined and required.date_fin_cotis == 1 %} required="required"{% endif %} placeholder="{{ _T('yyyy-mm-dd format') }}" />
                                    </div>
                                </div>
                {% endif %}
                            </div>
            {% endif %}
                        </div>
                        <div class="field{% if required.info_cotis is defined and required.info_cotis == 1 %} required{% endif %}">
                            <label for="info_cotis">{{ _T("Comments:") }}</label>
                            <textarea name="info_cotis" id="info_cotis" cols="61" rows="6"{% if required.info_cotis is defined and required.info_cotis == 1 %} required="required"{% endif %}>{{ contribution.info }}</textarea>
                        </div>
                    </div>
                </div>
            </div>

        {% include 'components/dynamic_fields.html.twig' with {'object': contribution} %}

    {% if not contribution.id and preferences.pref_mail_method != constant('Galette\\Core\\GaletteMail::METHOD_DISABLED') %}
        {% if not require_mass %}
            <div class="ui center aligned yellow segment">
                <div class="inline field">
                    <div class="ui toggle checkbox">
                        <input type="checkbox" name="mail_confirm" id="mail_confirm" value="1"{% if preferences.pref_bool_mailowner or contribution.sendEMail() %} checked="checked"{% endif %}/>
                        <label for="mail_confirm">
                            {{ _T("Notify member") }}
                        </label>
                        <br/>
                        <span class="exemple">
                            {{ _T("Member will receive a notification by email, if he has an address.") }}
                        </span>
                    </div>
                </div>
            </div>
        {% endif %}
    {% endif %}
    {% if not require_mass %}
        <div class="ui basic center aligned segment">
            <button type="submit" name="valid" class="ui labeled icon primary button action">
                <i class="save icon" aria-hidden="true"></i> {{ _T("Save") }}
            </button>
            <input type="hidden" name="id_cotis" value="{{ contribution.id }}"/>
            <input type="hidden" name="valid" value="1"/>
            <input type="hidden" name="trans_id" value="{% if contribution.transaction != NULL %}{{ contribution.transaction.id }}{% endif %}"/>
        </div>
    {% endif %}
    {% if mode != 'ajax' %}
            {% include "components/forms/csrf.html.twig" %}
        </form>
    {% endif %}
{% elseif mode != 'ajax' %} {# No members #}
    <div class="ui warning message" id="warningbox">
        <h3>{{ _T("No member registered!") }}</h3>
        <p>
            {{ _T("Unfortunately, there is no member in your database yet,") }}
            <br/>
            <a href="{{ url_for("addMember") }}">{{ _T("please create a member") }}</a>
        </p>
    </div>
{% endif %}
{% endblock %}

{% block javascripts %}
<script type="text/javascript">
    {% if type == constant('Galette\\Entity\\Contribution::TYPE_FEE') and not contribution.id %}
        {% include "elements/js/choose_adh.js.twig" with {
            new_contrib_onchange: true
        } %}
    {% else %}
        {% include "elements/js/choose_adh.js.twig" %}
    {% endif %}

    $(function() {
    {% if not contribution.id -%}
        var _types_amounts = {
            {%- for key, values in type_cotis_options -%}
               {%- if values.amount > 0 -%}
                    {{ key }}: {{ values.amount }},
                {%- endif -%}
            {%- endfor -%}
        };
        $('#id_type_cotis').dropdown({
            onChange: function(value) {
                //change to predefined amount if there is one, and if user has not entered data himself
                if ($('#montant_cotis').data('userset') != true && _types_amounts[value]) {
                    var _amount = _types_amounts[value];
                    $('#montant_cotis').val(_amount);
                }
            }
        });
        $('#montant_cotis').on('keyup', function() {
            //keep trace of user entered data
            if ($(this).val() == '') {
                $(this).data('userset', false);
            } else {
                $(this).data('userset', true);
            }
        });
        {%- endif %}

        {% if contribution.isTransactionPart() and contribution.transaction.getMissingAmount() %}
        $('#transaction_related').hide();
        $('#montant_cotis').on('change', function() {
            var _amount = {{ contribution.transaction.getMissingAmount() }};
            var _current = $(this).val();
            if (_current < _amount) {
                $('#transaction_related').show();
            } else if (_current > _amount) {
                {% include "elements/js/modal.js.twig" with {
                    modal_title_twig: _T("Contribution amount should not be greater than %max")|replace({'%max': contribution.transaction.getMissingAmount()})|e("js"),
                    modal_without_content: true,
                    modal_class: "mini",
                    modal_deny_only: true,
                    modal_cancel_text: _T("Close")|e("js"),
                    modal_classname: "redalert",
                } %}
            } else {
                $('#transaction_related').hide();
            }
        });
    {% endif %}

    {% if contribution.hasSchedule() %}
            {# Contributions popup #}
            var _btnuser_mapping = function(){
                $('#scheduledslist').click(function(){
                    $.ajax({
                        url: '{{ url_for("scheduledPayments") }}',
                        type: "GET",
                        data: {
                            ajax: true,
                            id_cotis: '{{ contribution.id }}'
                        },
                        {% include "elements/js/loader.js.twig" with {
                            selector: '#scheduledslist',
                            loader: 'button'
                        } %},
                        success: function(res){
                            _contribs_dialog(res, '{{ contribution.id }}');
                        },
                        error: function() {
                            {% include "elements/js/modal.js.twig" with {
                                modal_title_twig: _T("An error occurred displaying scheduled payments :(")|e("js"),
                                modal_without_content: true,
                                modal_class: "mini",
                                modal_deny_only: true,
                                modal_cancel_text: _T("Close")|e("js"),
                                modal_classname: "redalert",
                            } %}
                        }
                    });
                });
            }
            _btnuser_mapping();

            var _contribs_dialog = function(res, id_cotis){
                {% include "elements/js/modal.js.twig" with {
                    modal_title_twig: _T("Scheduled payments")|e("js"),
                    modal_content: "res",
                    modal_class: "scheduledpayments fullscreen",
                    modal_content_class: "scrolling",
                    modal_deny_only: true,
                    modal_cancel_text: _T("Close")|e('js')
                } %}
                _contribs_ajax_mapper(res, id_cotis);
            }

            var _contribs_ajax_mapper = function(res, id_cotis){
                /*$('.scheduledpayments .filter.icon').remove();
                $('.scheduledpayments .infoline .button').remove();
                $('.scheduledpayments .contribution_row input[type=checkbox]').hide();

                //Initialize Fomantic components
                $('.scheduledpayments .dropdown').dropdown();
                {% include "elements/js/calendar.js.twig" %}

                //Deactivate contributions list links
                $('.scheduledpayments tbody a').click(function(){
                    //for links in body (members links), we de nothing
                    return false;
                });

                //Use JS to send forms
                $('.scheduledpayments form').on('submit', function(){
                    var _form = $(this);
                    $.ajax({
                        url: _form.attr('action'),
                        type: "POST",
                        data: _form.serialize(),
                        {% include "elements/js/loader.js.twig" with {
                            selector: '.scheduledpayments'
                        } %},
                        success: function(res){
                            $('#main-container').remove();
                            $('.scheduledpayments .content').append(res);
                            _contribs_ajax_mapper(res, max_amount);
                        },
                        error: function() {
                            {% include "elements/js/modal.js.twig" with {
                                modal_title_twig: _T("An error occurred displaying contributions :(")|e("js"),
                                modal_without_content: true,
                                modal_class: "mini",
                                modal_deny_only: true,
                                modal_cancel_text: _T("Close")|e("js"),
                                modal_classname: "redalert",
                            } %}
                        }
                    });
                    return false;
                });

                _bindDropdownsAutosubmit();*/

                //Bind pagination and ordering links
                /*$('.scheduledpayments .pagination a, .scheduledpayments thead a').click(function() {
                    $.ajax({
                        url: this.href,
                        type: "GET",
                        data: {
                            ajax: true,
                            max_amount: max_amount
                        },
                        {% include "elements/js/loader.js.twig" with {
                            selector: '.scheduledpayments'
                        } %},
                        success: function(res){
                            $('#main-container').remove();
                            $('.scheduledpayments .content').append(res);
                            _contribs_ajax_mapper(res, max_amount);
                        },
                        error: function() {
                            {% include "elements/js/modal.js.twig" with {
                                modal_title_twig: _T("An error occurred displaying contributions :(")|e("js"),
                                modal_without_content: true,
                                modal_class: "mini",
                                modal_deny_only: true,
                                modal_cancel_text: _T("Close")|e("js"),
                                modal_classname: "redalert",
                            } %}
                        },
                    });
                    return false;
                });*/

                //Bind reset filters button
                /*$('#clear_filter').click(function(event) {
                    var _this = $(this);
                    _this.closest('form').submit(function(event) {
                        var _form = $(this);
                        $.ajax({
                            url: _form.attr('action'),
                            type: "POST",
                            data: {
                                clear_filter: true
                            },
                            {% include "elements/js/loader.js.twig" with {
                                selector: '.scheduledpayments'
                            } %},
                            success: function(res){
                                $('#main-container').remove();
                                $('.scheduledpayments .content').append(res);
                                _contribs_ajax_mapper(res, max_amount);
                            },
                            error: function() {
                                {% include "elements/js/modal.js.twig" with {
                                    modal_title_twig: _T("An error occurred displaying contributions :(")|e("js"),
                                    modal_without_content: true,
                                    modal_class: "mini",
                                    modal_deny_only: true,
                                    modal_cancel_text: _T("Close")|e("js"),
                                    modal_classname: "redalert",
                                } %}
                            }
                        });
                    });
                });*/
            }
    {% endif %}
    });
</script>
{% endblock %}
