{# -*- mode: jinja-html -*- #}
{% extends 'base.j2' %}
{% import 'wtf.j2' as wtf %}

{% macro render_field(field, with_label=True) %}
<div>
    {% if with_label %}
    <span class="wtf-field">{{ field.label }} :</span>
    {% endif %}
    <span class="wtf-field">{{ field(**kwargs)|safe }}
        {% if field.errors %}
        <ul class=errors>
            {% for error in field.errors %}
            <li>{{ error }}</li>
            {% endfor %}
        </ul>
        {% endif %}
    </span>
</div>
{% endmacro %}

{% macro render_add_logo(add_logo_form) %}
<details {{ add_logo_form.id() }} {{ add_logo_form.opened() }}>
    <summary>
        <h3>Ajouter un logo</h3>
    </summary>
    <div>
        {{ render_field(add_logo_form.name) }}
        {{ render_field(add_logo_form.upload) }}
        {{ render_field(add_logo_form.do_insert, False, onSubmit="submit_form") }}
    </div>
</details>
{% endmacro %}

{% macro render_logo(dept_form, logo_form) %}
<details {{ logo_form.id() }} {{ logo_form.opened() }}>
    {{ logo_form.hidden_tag() }}
    <summary>
        {% if logo_form.titre %}
        <h3 class="titre_logo">{{ logo_form.titre }}</h3>
        {% if logo_form.description %}
        <div class="sco_help">{{ logo_form.description }}</div>
        {% endif %}
        {% else %}
        <h3 class="titre_logo">Logo personalisé: {{ logo_form.logo_id.data }}</h3>
        {% if logo_form.description %}
        <div class="sco_help">{{ logo_form.description }}</div>
        {% endif %}
        {% endif %}
    </summary>
    <div class="content">
        <div class="image_logo">
            <img src="{{ logo_form.logo.get_url_small() }}" alt="pas de logo chargé" />
        </div>
        <div class="infos_logo">
            <h4>{{ logo_form.logo.logoname }} (Format: {{ logo_form.logo.suffix }})</h4>
            Taille: {{ logo_form.logo.size }} px
            {% if logo_form.logo.mm %} &nbsp; / &nbsp; {{ logo_form.logo.mm }} mm {% endif %}<br />
            Aspect ratio: {{ logo_form.logo.aspect_ratio }}<br />
            Usage: <span style="font-family: system-ui">{{ logo_form.logo.get_usage() }}</span>
        </div>
        <div class="actions_logo">
            <div class="action_label">Modifier l'image</div>
            <div class="action_button">
                <span class="wtf-field">{{ render_field(logo_form.upload, False, onchange="submit_form()") }}</span>
            </div>
            {% if logo_form.can_delete %}
            <div class="action_label">Renommer</div>
            <div class="action_button">
                {{ render_field(logo_form.new_name, False) }}
                {{ render_field(logo_form.do_rename, False, onSubmit="submit_form()") }}
            </div>
            <div class="action_label">Supprimer l'image</div>
            <div class="action_button">
                {{ render_field(logo_form.do_delete, False, onSubmit="submit_form()") }}
            </div>
            {% endif %}
        </div>
    </div>
</details>
{% endmacro %}

{% macro render_logos(dept_form) %}
{% for logo_entry in dept_form.logos.entries %}
{% set logo_form = logo_entry.form %}
{{ render_logo(dept_form, logo_form) }}
{% else %}
<p class="logo-titre_logo">
<h3 class="titre_logo">Aucun logo défini en propre à ce département</h3>
</p>
{% endfor %}
{% endmacro %}

{% block app_content %}

<script src="/ScoDoc/static/jQuery/jquery.js"></script>
<script src="/ScoDoc/static/js/config_logos.js"></script>
<script src="/ScoDoc/static/js/detail_summary_persistence.js"></script>

<form id="config_logos_form" class="sco-form" action="" method="post" enctype="multipart/form-data" novalidate>
    {{ form.hidden_tag() }}

    <div class="configuration_logo">
        <h1>Bibliothèque de logos</h1>
        {% for dept_entry in form.depts.entries %}
        {% set dept_form = dept_entry.form %}
        {{ dept_entry.form.hidden_tag() }}
        <details {{ dept_form.id() }} {{ dept_form.opened() }}>
            <summary>
                <span class="entete_dept">
                    {% if dept_entry.form.is_local() %}
                    <h2>Département {{ dept_form.dept_name.data }}</h2>
                    <h3 class="effectifs">{{ dept_form.count() }}</h3>
                    <div class="sco_help">Les paramètres donnés sont spécifiques à ce département.<br />
                        Les logos du département se substituent aux logos de même nom définis globalement:</div>
                    {% else %}
                    <h2>Logos généraux</h2>
                    <h3 class="effectifs">{{ dept_form.count() }}</h3>
                    <div class="sco_help">Les images de cette section sont utilisé pour tous les départements,
                        mais peuvent être redéfinies localement au niveau de chaque département
                        (il suffit de définir un logo local de même nom)</div>
                    {% endif %}
                </span>
            </summary>
            <div>
                {{ render_logos(dept_form) }}
                {{ render_add_logo(dept_form.add_logo.form) }}
            </div>
        </details>
        {% endfor %}
    </div>
</form>
{% endblock %}
