{#
 * Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
 *
 * This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
#}

{# CustomFields Title #}
{% block custom_field_title_widget %}
    <span class="cf-{{ form.vars.attr.type }}">
        {{ form.vars.attr.title }}
    </span>
{% endblock custom_field_title_widget %}

{# CustomFields Choice #}
{# render an alement in a choice list #}
{% block cf_choices_list_widget %}
    {{ form_row(form.name) }}
    {{ form_row(form.active) }}
    {{ form_row(form.slug) }}
{% endblock cf_choices_list_widget %}

{# CFChoice : render the different elements in a choice list #}
{% block cf_choices_row %}
    <h3>{{ 'Choices'|trans }}</h3>

    <div id="{{ form.vars.id }}" data-prototype="{{- form_row(form.vars.prototype.children.name)
        ~ form_row(form.vars.prototype.children.active)
        ~ form_row(form.vars.prototype.children.slug)  -}}">
    <table><tbody>
    {% for choice in form %}
        <tr><td>
        {{ form_row(choice.name) }}
        {{ form_row(choice.active) }}
        {{ form_row(choice.slug) }}
        </td></tr>
    {% endfor %}
    </tbody></table>
    </div>


    {# we use javascrit to add an additional element. All functions are personnalized with the id ( = form.vars.id) #}
    <script type="text/javascript">
        function addElementInDiv(div_id) {
            var div = $('#' + div_id);console.log(div);
            var prototype = div.data('prototype');console.log(prototype);
            var index = div.data('index');console.log(index);
            var add_element_link = $('#' + div_id + '_add_element_link');
            var new_fields = prototype.replace(/__name__label__/g, index);
            var new_fields = prototype.replace(/__name__/g, index);

            div.data('index', index + 1);
            console.log(index);
            add_element_link.before(new_fields);
        }

        function initializeCFChoiceOptionsChoices(div_id) {
            var add_element_link = $('<a id="' + div_id + '_add_element_link"" href="#" class="btn btn-submit">{{ 'Add an element'|trans }}</a>');
            var div = $('#' + div_id);
            div.append(add_element_link);
            div.data('index', div.find('td').length);
            console.log(div.data('index'));

            add_element_link.on('click', function (e) {
                e.preventDefault();
                addElementInDiv(div_id);
            });
        }

        document.addEventListener('DOMContentLoaded', function(event) {
            jQuery(document).ready(initializeCFChoiceOptionsChoices('{{ form.vars.id }}'));
        });
    </script>
{%  endblock cf_choices_row %}

{# extend the number type to add post_text extension #}
{% block number_widget %}
{%- if post_text is defined and post_text is not empty-%}
<div class="input_with_post_text">
{%- endif -%}
{{ block('form_widget') }}
{%- if post_text is defined and post_text is not empty-%}
<span class="post_text">{{ post_text }}</span>
</div>
{%- endif -%}
{% endblock %}

{# extend the number type to add post_text extension #}
{% block integer_widget %}
{%- if post_text is defined and post_text is not empty-%}
<div class="input_with_post_text">
{%- endif -%}
{{ block('form_widget') }}
{%- if post_text is defined and post_text is not empty-%}
<span class="post_text">{{ post_text }}</span>
</div>
{%- endif -%}
{% endblock %}

{# The choice_with_other_widget widget is defined in the main bundle #}

{% block pick_address_row %}
    {{ form_label(form) }}
    {{ form_errors(form) }}
    {{ form_widget(form) }}
{% endblock %}

{% block pick_address_widget %}
    {{ form_widget(form) }}
    <div data-input-address-container="{{ form.vars.uniqid }}"></div>
{% endblock %}
