{#
 # ---------------------------------------------------------------------
 # Formcreator is a plugin which allows creation of custom forms of
 # easy access.
 # ---------------------------------------------------------------------
 # LICENSE
 #
 # This file is part of Formcreator.
 #
 # Formcreator 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 2 of the License, or
 # (at your option) any later version.
 #
 # Formcreator 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 Formcreator. If not, see <http://www.gnu.org/licenses/>.
 # ---------------------------------------------------------------------
 # @copyright Copyright © 2011 - 2021 Teclib'
 # @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
 # @link      https://github.com/pluginsGLPI/formcreator/
 # @link      https://pluginsglpi.github.io/formcreator/
 # @link      http://plugins.glpi-project.org/#/plugin/formcreator
 # ---------------------------------------------------------------------
 #}

{% set lastSectionOrder = call('PluginFormcreatorCommon::getMax', [item, { 'plugin_formcreator_forms_id': item.fields['plugin_formcreator_forms_id']}, 'order']) %}

<li class="plugin_formcreator_section" data-itemtype="PluginFormcreatorSection" data-id="{{ item.fields['id'] }}">
    {% set conditionsCount = call('PluginFormcreatorCondition::countForItem', [item]) %}
    <a href="#" onclick="plugin_formcreator.showSectionForm({{ item.fields['plugin_formcreator_forms_id'] }}, {{ item.fields['id'] }})" data-field="name">
        {# TODO : Show count of conditions #}
        <sup class="plugin_formcreator_conditions_count" title="{{ __('Count of conditions', 'formcreator') }}">{{ conditionsCount }}</sup>
        {% if item.fields['name'] is empty %}
            {% set name = '(' ~ item.fields['id'] ~ ')' %}
        {% else %}
            {% set name = call('Glpi\\Toolbox\\Sanitizer::unsanitize', [item.fields['name']]) %}
        {% endif %}
        <span>{{ name }}</span>
    </a>

    {# Delete a section #}
    <span class='form_control pointer'>
    <i class="far fa-trash-alt" onclick="plugin_formcreator.deleteSection(this)"></i>
    </span>

    {# Clone a section #}
    <span class='form_control pointer'>
    <i class="far fa-clone" onclick="plugin_formcreator.duplicateSection(this)"></i>
    </span>

    {# Move down a section #}
    {% if item.fields['order'] < lastSectionOrder %}
        {% set display = 'initial' %}
    {% else %}
        {% set display = 'none' %}
    {% endif %}
    <span class="form_control pointer moveDown" style="display: {{ display }}">
      <i class="fas fa-sort-down" onclick="plugin_formcreator.moveSection(this, 'down')"></i>
    </span>

    {# Move up a section #}
    {% if item.fields['order'] > 1 %}
        {% set display = 'initial' %}
    {% else %}
        {% set display = 'none' %}
    {% endif %}
    <span class="form_control pointer moveUp" style="display: {{ display }}">
    <i class="fas fa-sort-up" onclick="plugin_formcreator.moveSection(this, 'up')"></i>
    </span>

    {# Section content #}
    {% set columns = call('constant', ['PluginFormcreatorSection::COLUMNS']) %}
    <div class="grid-stack grid-stack-{{ columns }}" data-gs-animate="yes" data-gs-width="{{ columns }}" data-id="{{ item.fields['id'] }}"></div>

    {# Add a question #}
    <div class="plugin_formcreator_question">
        <a href="#" onclick="plugin_formcreator.showQuestionForm({{ item.fields['id'] }});">
            <i class="fas fa-plus"></i>&nbsp;{{ __('Add a question', 'formcreator') }}
        </a>
    </div>
    <script type="text/javascript">
        $(function () {
            plugin_formcreator.initGridStack({{ item.fields['id'] }});
         });
    </script>
</li>
