{% extends '@CpCreationVitiCore/admin/base.html.twig' %}

{% macro renderValue(value) %}
    {% if value is iterable %}
        <pre style="white-space: pre-wrap; margin: 0;">{{ value|json_encode(constant('JSON_PRETTY_PRINT')) }}</pre>
    {% elseif value is same as(null) %}
        <em>null</em>
    {% elseif value is same as(true) %}
        true
    {% elseif value is same as(false) %}
        false
    {% else %}
        {{ value }}
    {% endif %}
{% endmacro %}

{% macro renderDiffRows(field, changeset) %}
    {% import _self as audit %}

    {% if changeset.old is defined or changeset.new is defined %}
        <tr>
            <td><code>{{ field }}</code></td>
            <td>{{ audit.renderValue(changeset.old|default(null)) }}</td>
            <td>{{ audit.renderValue(changeset.new|default(null)) }}</td>
        </tr>
    {% else %}
        {% for childField, childChangeset in changeset %}
            {{ audit.renderDiffRows(field ~ '.' ~ childField, childChangeset) }}
        {% endfor %}
    {% endif %}
{% endmacro %}

{% block content %}
    {% import _self as audit %}

    <h1 class="page-header">Journal d'audit - {{ entity.label }}</h1>

    <div class="panel panel-default">
        <div class="panel-heading">
            Filtres
        </div>
        <div class="panel-body">
            <form method="get" action="{{ path('admin_audit_history', { entity: entity.param }) }}">
                <div class="row">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label for="objectId">ID objet</label>
                            <input type="text" name="objectId" id="objectId" class="form-control" value="{{ filters.objectId }}">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label for="type">Action</label>
                            <select name="type" id="type" class="form-control">
                                <option value="" {% if filters.type is null %}selected{% endif %}>Toutes</option>
                                <option value="insert" {% if filters.type == 'insert' %}selected{% endif %}>Création</option>
                                <option value="update" {% if filters.type == 'update' %}selected{% endif %}>Modification</option>
                                <option value="remove" {% if filters.type == 'remove' %}selected{% endif %}>Suppression</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <label>&nbsp;</label>
                        <button type="submit" class="btn btn-primary btn-block">Filtrer</button>
                    </div>
                    <div class="col-md-2">
                        <label>&nbsp;</label>
                        <a href="{{ path('admin_audit_history', { entity: entity.param }) }}" class="btn btn-default btn-block">Réinitialiser</a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            {{ entity.class }} - {{ paginator.numResults }} log{{ paginator.numResults > 1 ? 's' : '' }}
            <div class="pull-right">
                <a href="{{ path('admin_audit') }}" class="btn btn-default btn-xs">
                    Retour aux entités
                </a>
            </div>
        </div>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>Date</th>
                <th>Action</th>
                <th>Objet</th>
                <th>Utilisateur</th>
                <th>Détails</th>
            </tr>
            </thead>
            <tbody>
            {% for entry in paginator.results %}
                <tr>
                    <td>{{ entry.createdAt ? entry.createdAt|date('d/m/Y H:i:s') : '-' }}</td>
                    <td>
                        {% if entry.type == 'insert' %}
                            <span class="label label-success">Création</span>
                        {% elseif entry.type == 'update' %}
                            <span class="label label-info">Modification</span>
                        {% elseif entry.type == 'remove' %}
                            <span class="label label-danger">Suppression</span>
                        {% else %}
                            <span class="label label-default">{{ entry.type }}</span>
                        {% endif %}
                    </td>
                    <td><code>{{ entry.objectId }}</code></td>
                    <td>
                        {{ entry.username|default(entry.userId|default('-')) }}
                        {% if entry.ip %}
                            <br><small>{{ entry.ip }}</small>
                        {% endif %}
                    </td>
                    <td>
                        {% set diffs = entry.diffs %}
                        {% if diffs is not empty %}
                            <table class="table table-condensed table-bordered" style="margin-bottom: 0;">
                                <thead>
                                <tr>
                                    <th>Champ</th>
                                    <th>Ancienne valeur</th>
                                    <th>Nouvelle valeur</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% for field, changeset in diffs %}
                                    {{ audit.renderDiffRows(field, changeset) }}
                                {% endfor %}
                                </tbody>
                            </table>
                        {% else %}
                            <em>Aucun détail</em>
                        {% endif %}
                    </td>
                </tr>
            {% else %}
                <tr>
                    <td colspan="5">Aucun log trouvé.</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    {% if paginator.haveToPaginate %}
        <nav>
            <ul class="pager">
                <li class="previous {% if not paginator.hasPreviousPage %}disabled{% endif %}">
                    {% if paginator.hasPreviousPage %}
                        <a href="{{ path('admin_audit_history', { entity: entity.param, page: paginator.previousPage, objectId: filters.objectId, type: filters.type }) }}">&larr; Précédent</a>
                    {% else %}
                        <span>&larr; Précédent</span>
                    {% endif %}
                </li>
                <li>
                    Page {{ paginator.currentPage }} / {{ paginator.numPages }}
                </li>
                <li class="next {% if not paginator.hasNextPage %}disabled{% endif %}">
                    {% if paginator.hasNextPage %}
                        <a href="{{ path('admin_audit_history', { entity: entity.param, page: paginator.nextPage, objectId: filters.objectId, type: filters.type }) }}">Suivant &rarr;</a>
                    {% else %}
                        <span>Suivant &rarr;</span>
                    {% endif %}
                </li>
            </ul>
        </nav>
    {% endif %}
{% endblock %}
