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

{% macro statusLabel(status) %}
    {% if status == 'published' %}
        <span class="label label-success">Publiée</span>
    {% elseif status == 'archived' %}
        <span class="label label-default">Archivée</span>
    {% elseif status == 'draft' %}
        <span class="label label-warning">Brouillon</span>
    {% else %}
        <span class="label label-default">{{ status }}</span>
    {% endif %}
{% endmacro %}

{% macro actionLabel(metadata) %}
    {% set action = metadata.action|default(null) %}
    {% if action == 'create' %}
        <span class="label label-success">Création</span>
    {% elseif action == 'update' %}
        <span class="label label-info">Modification</span>
    {% elseif action == 'delete' %}
        <span class="label label-danger">Suppression</span>
    {% elseif action == 'schedule' %}
        <span class="label label-warning">Planification</span>
    {% elseif action %}
        <span class="label label-default">{{ action }}</span>
    {% else %}
        <span class="label label-default">-</span>
    {% endif %}
{% endmacro %}

{% macro shortClass(class) %}
    {{ class|split('\\')|last }}
{% endmacro %}

{% macro objectSummary(version) %}
    {% import _self as businessVersion %}

    <strong>{{ businessVersion.shortClass(version.objectClass) }}</strong><br>
    {% if version.scope == 'ecommerce.product_price' %}
        <small>
            Produit : {{ version.snapshot.product.label|default('Sans nom') }}<br>
            Variant : {{ version.snapshot.variant.label|default('Sans nom') }}<br>
            Groupe : {{ version.snapshot.group.label|default('Sans nom') }}<br>
            ID prix : <code>{{ version.objectId }}</code>
        </small>
    {% elseif version.scope == 'ecommerce.product_variant_availability' %}
        <small>
            Produit : {{ version.snapshot.product.label|default('Sans nom') }}<br>
            Variant : {{ version.snapshot.variant.label|default('Sans nom') }}<br>
            Dépublié : {{ version.snapshot.unPublished|default(false) ? 'Oui' : 'Non' }}<br>
            Épuisé : {{ version.snapshot.soldOut|default(false) ? 'Oui' : 'Non' }}<br>
            ID variant : <code>{{ version.objectId }}</code>
        </small>
    {% else %}
        <small><code>{{ version.objectId }}</code></small>
    {% endif %}
{% endmacro %}

{% block content %}
    {% import _self as businessVersion %}
    {% set pagination = versions.paginationData %}

    <h1 class="page-header">Versioning</h1>

    <div class="panel panel-default">
        <div class="panel-heading">
            Filtres
        </div>
        <div class="panel-body">
            <form method="get" action="{{ path('admin_business_version') }}">
                <div class="row">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="scope">Périmètre</label>
                            <select name="scope" id="scope" class="form-control">
                                <option value="" {% if filters.scope is null %}selected{% endif %}>Tous</option>
                                {% for scope in scopes %}
                                    <option value="{{ scope }}" {% if filters.scope == scope %}selected{% endif %}>{{ scope }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="status">Statut</label>
                            <select name="status" id="status" class="form-control">
                                <option value="" {% if filters.status is null %}selected{% endif %}>Tous</option>
                                {% for status, label in statuses %}
                                    <option value="{{ status }}" {% if filters.status == status %}selected{% endif %}>{{ label }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="objectClass">Classe</label>
                            <select name="objectClass" id="objectClass" class="form-control">
                                <option value="" {% if filters.objectClass is null %}selected{% endif %}>Toutes</option>
                                {% for objectClass in objectClasses %}
                                    <option value="{{ objectClass }}" {% if filters.objectClass == objectClass %}selected{% endif %}>{{ businessVersion.shortClass(objectClass) }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <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-2">
                        <div class="form-group">
                            <label for="createdBy">Utilisateur</label>
                            <input type="text" name="createdBy" id="createdBy" class="form-control" value="{{ filters.createdBy }}">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="createdFrom">Créée à partir du</label>
                            <input type="datetime-local" name="createdFrom" id="createdFrom" class="form-control" value="{{ filters.createdFromInput }}">
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="createdTo">Créée jusqu'au</label>
                            <input type="datetime-local" name="createdTo" id="createdTo" class="form-control" value="{{ filters.createdToInput }}">
                        </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_business_version') }}" class="btn btn-default btn-block">Réinitialiser</a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            Versions - {{ versions.totalItemCount }} résultat{{ versions.totalItemCount > 1 ? 's' : '' }}
        </div>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>Date</th>
                <th>Périmètre</th>
                <th>Objet</th>
                <th>Version</th>
                <th>Statut</th>
                <th>Action</th>
                <th>Publication</th>
                <th>Utilisateur</th>
                <th>Détail</th>
            </tr>
            </thead>
            <tbody>
            {% for version in versions %}
                <tr>
                    <td>{{ version.createdAt|date('d/m/Y H:i:s') }}</td>
                    <td><code>{{ version.scope }}</code></td>
                    <td>
                        {{ businessVersion.objectSummary(version) }}
                    </td>
                    <td>#{{ version.version }}</td>
                    <td>{{ businessVersion.statusLabel(version.status) }}</td>
                    <td>{{ businessVersion.actionLabel(version.metadata) }}</td>
                    <td>{{ version.validFrom ? version.validFrom|date('d/m/Y H:i:s') : '-' }}</td>
                    <td>{{ version.createdBy|default('-') }}</td>
                    <td>
                        <a href="{{ path('admin_business_version_show', filterQuery|merge({ id: version.id, page: pagination.current })) }}" class="btn btn-info btn-xs">
                            <i class="fas fa-search"></i> Voir
                        </a>
                    </td>
                </tr>
            {% else %}
                <tr>
                    <td colspan="9">Aucune version métier trouvée.</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    {% if pagination.pageCount > 1 %}
        <nav>
            <ul class="pager">
                <li class="previous {% if pagination.previous is not defined %}disabled{% endif %}">
                    {% if pagination.previous is defined %}
                        <a href="{{ path('admin_business_version', filterQuery|merge({ page: pagination.previous })) }}">&larr; Précédent</a>
                    {% else %}
                        <span>&larr; Précédent</span>
                    {% endif %}
                </li>
                <li>
                    Page {{ pagination.current }} / {{ pagination.pageCount }}
                </li>
                <li class="next {% if pagination.next is not defined %}disabled{% endif %}">
                    {% if pagination.next is defined %}
                        <a href="{{ path('admin_business_version', filterQuery|merge({ page: pagination.next })) }}">Suivant &rarr;</a>
                    {% else %}
                        <span>Suivant &rarr;</span>
                    {% endif %}
                </li>
            </ul>
        </nav>
    {% endif %}
{% endblock %}
