{% extends 'base.html.twig' %}

{% block title %}Hello PublicController!{% endblock %}

{% block stylesheets %}
    {{ parent() }}
{% endblock %}

{% block body %}
<div class="container">
    <h1>Modification du lieu {{ commerce.tags_converted.name is defined and commerce.tags_converted.name is not empty ? commerce.tags_converted.name : '(sans nom)' }}!</h1>

    {% if status == "Les tags ont été mis à jour avec succès" %}
        <span class="badge bg-success p-4">{{status}}</span>

    {# Tableau comparatif des tags avant/après modification #}
    {% if tags_before_modif is defined and tags_after_modif is defined %}
        <h3 class="mt-4">Comparatif des tags avant/après modification</h3>
        <div class="table-responsive mb-4">
            <table class="table table-bordered table-sm align-middle">
                <thead class="table-light">
                    <tr>
                        <th>Tag</th>
                        <th>Avant</th>
                        <th>Après</th>
                    </tr>
                </thead>
                <tbody>
                {# Construction manuelle de la liste des clés sans filtre unique #}
                {% set all_keys = [] %}
                {% for k in tags_before_modif|keys %}{% if k not in all_keys %}{% set all_keys = all_keys|merge([k]) %}{% endif %}{% endfor %}
                {% for k in tags_after_modif|keys %}{% if k not in all_keys %}{% set all_keys = all_keys|merge([k]) %}{% endif %}{% endfor %}
                {% set all_keys = all_keys|sort %}
                {% for key in all_keys %}
                    {% set before = tags_before_modif[key]|default(null) %}
                    {% set after = tags_after_modif[key]|default(null) %}
                    {% if before is not null and (after is null or after == '') %}
                        {# Suppression #}
                        <tr>
                            <td><code>{{ key }}</code></td>
                            <td class="bg-danger text-white">{{ before }}</td>
                            <td class="bg-danger text-white">(supprimé)</td>
                        </tr>
                    {% elseif before is null and after is not null and after != '' %}
                        {# Ajout #}
                        <tr>
                            <td><code>{{ key }}</code></td>
                            <td class="bg-success text-white">(nouveau)</td>
                            <td class="bg-success text-white">{{ after }}</td>
                        </tr>
                    {% elseif before != after and after != '' %}
                        {# Modification #}
                        <tr>
                            <td><code>{{ key }}</code></td>
                            <td class="bg-warning">{{ before }}</td>
                            <td class="bg-warning">{{ after }}</td>
                        </tr>
                    {% else %}
                        <tr>
                            <td><code>{{ key }}</code></td>
                            <td>{{ before }}</td>
                            <td>{{ after }}</td>
                        </tr>
                    {% endif %}
                {% endfor %}
                </tbody>
            </table>
            <small class="text-muted">
                <span class="bg-danger text-white px-2">&nbsp;</span> supprimé &nbsp;
                <span class="bg-warning px-2">&nbsp;</span> modifié &nbsp;
                <span class="bg-success text-white px-2">&nbsp;</span> ajouté
            </small>
        </div>
        {% if excluded_post_fields is defined and excluded_post_fields|length > 0 %}
            <div class="alert alert-info">
                <b>Champs du formulaire non pris en compte dans OpenStreetMap :</b>
                <ul class="mb-0">
                    {% for field in excluded_post_fields %}
                        <li><code>{{ field }}</code></li>
                    {% endfor %}
                </ul>
            </div>
        {% endif %}
    {% endif %}

    {# Affichage en lecture seule des nouveaux tags #}
    <h3 class="mt-4">Nouveaux tags enregistrés</h3>
    <div class="table-responsive mb-4">
        <table class="table table-bordered table-sm align-middle">
            <thead class="table-light">
                <tr>
                    <th>Tag</th>
                    <th>Valeur</th>
                </tr>
            </thead>
            <tbody>
            {% for key, value in tags_after_modif %}
                {% if value is not empty %}
                <tr>
                    <td><code>{{ key }}</code></td>
                    <td>{{ value }}</td>
                </tr>
                {% endif %}
            {% endfor %}
            </tbody>
        </table>
    </div>

    <h4 class="mt-4">Tags OSM (texte à copier-coller)</h4>
    {% include 'public/_tags_textarea.html.twig' with { 'tags': tags_after_modif } %}

    <p class="p-4 col-12 col-lg-8">
        Merci d'avoir contribué à l'amélioration de la base de données OSM, votre contribution sera visible sur de nombreux sites web et dans la base adresse nationale des lieux et commerces.

        <br>
        Compléter les informations de votre commerce?
        Complétez les informations ici ou bien réutilisez plus tard le lien secret que vous avez reçu par email.
        <br>

        Envie de contribuer ailleurs ?
        Créez votre propre compte OpenStreetMap sur <a href="https://osm.org" >osm.org</a>, et venez discuter avec les autres contributeurs sur le forum de la communauté OSM sur <a href="https://forum.openstreetmap.fr" >forum.openstreetmap.fr</a>.

        <hr>

        <form action="{{ path('app_public_submit', {'osm_object_id': commerce_overpass['@attributes'].id, 'version': commerce_overpass['@attributes'].version, 'changesetID': commerce_overpass['@attributes'].changeset }) }}" method="post" class="needs-validation">
            <input type="hidden" name="osm_kind" value="{{ place.osmKind }}">


        {% include 'public/edit/yenaunpeuplusjevouslemets.html.twig' %}
        {% include 'public/edit/social_media.html.twig' %}
        {% include 'public/edit/address.html.twig' %}
        {% include 'public/edit/clim.html.twig' %}
        {% include 'public/edit/ask_angela.html.twig' %}
        {% include 'public/edit/wheelchair.html.twig' %}

        <div id="validation_messages" class="alert alert-danger d-none"></div>
        <div class="d-grid gap-2 d-md-flex justify-content-md-end mt-4">
                                <button type="submit" class="btn btn-primary  col-12 col-md-5">
                                    <i class="bi bi-send"></i>
                                    {{ 'display.submit'|trans }}
                                </button>
    </div>

        </form>
    </p>
    {% else %}
        <span class="badge bg-danger p-4">{{status}}</span>
    {% endif %}
    <br>
    <a href="{{ path('app_public_index') }}">Retour à la page d'accueil</a>
</div>

{% endblock %} 

    {% block javascripts %}
        {{ parent() }} 
        <script> 

check_validity ? check_validity() : null;

        </script> 
{% endblock %}
