{% 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 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 shortClass(class) %}
    {{ class|split('\\')|last }}
{% endmacro %}

{% macro moneyCents(value) %}
    {% if value is same as(null) %}
        -
    {% else %}
        {{ (value / 100)|number_format(2, ',', ' ') }} €
    {% endif %}
{% endmacro %}

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

    <h1 class="page-header">Version #{{ version.version }}</h1>

    <div class="panel panel-default">
        <div class="panel-heading">
            Informations
            <div class="pull-right">
                {% if version.status == 'draft' %}
                    <a href="{{ path('admin_business_version_delete', returnParams|merge({ id: version.id, returnUrl: path('admin_business_version', returnParams) })) }}" class="btn btn-danger btn-xs" onclick="return confirm('Supprimer cette planification ?');">
                        <i class="fa fa-trash"></i> Supprimer la planification
                    </a>
                {% endif %}
                <a href="{{ path('admin_business_version', returnParams) }}" class="btn btn-default btn-xs">
                    Retour à la liste
                </a>
            </div>
        </div>
        <table class="table table-bordered">
            <tbody>
            <tr>
                <th style="width: 220px;">Périmètre</th>
                <td><code>{{ version.scope }}</code></td>
            </tr>
            <tr>
                <th>Classe</th>
                <td>
                    <strong>{{ businessVersion.shortClass(version.objectClass) }}</strong><br>
                    <small>{{ version.objectClass }}</small>
                </td>
            </tr>
            <tr>
                <th>ID objet</th>
                <td><code>{{ version.objectId }}</code></td>
            </tr>
            <tr>
                <th>Version</th>
                <td>#{{ version.version }}</td>
            </tr>
            <tr>
                <th>Statut</th>
                <td>{{ businessVersion.statusLabel(version.status) }}</td>
            </tr>
            <tr>
                <th>Créée par</th>
                <td>{{ version.createdBy|default('-') }}</td>
            </tr>
            <tr>
                <th>Créée le</th>
                <td>{{ version.createdAt|date('d/m/Y H:i:s') }}</td>
            </tr>
            <tr>
                <th>Valable à partir du</th>
                <td>{{ version.validFrom ? version.validFrom|date('d/m/Y H:i:s') : '-' }}</td>
            </tr>
            <tr>
                <th>Valable jusqu'au</th>
                <td>{{ version.validTo ? version.validTo|date('d/m/Y H:i:s') : '-' }}</td>
            </tr>
            <tr>
                <th>Publiée le</th>
                <td>{{ version.publishedAt ? version.publishedAt|date('d/m/Y H:i:s') : '-' }}</td>
            </tr>
            </tbody>
        </table>
    </div>

    {% if version.scope == 'ecommerce.product_price' %}
        <div class="panel panel-default">
            <div class="panel-heading">Prix produit</div>
            <table class="table table-bordered">
                <tbody>
                <tr>
                    <th style="width: 220px;">Produit</th>
                    <td>{{ version.snapshot.product.label|default('Sans nom') }}</td>
                </tr>
                <tr>
                    <th>Variant</th>
                    <td>{{ version.snapshot.variant.label|default('Sans nom') }}</td>
                </tr>
                <tr>
                    <th>Groupe de prix</th>
                    <td>{{ version.snapshot.group.label|default('Sans nom') }}</td>
                </tr>
                <tr>
                    <th>Prix</th>
                    <td>{{ businessVersion.moneyCents(version.snapshot.price|default(null)) }}</td>
                </tr>
                <tr>
                    <th>Prix remisé</th>
                    <td>{{ businessVersion.moneyCents(version.snapshot.priceDiscount|default(null)) }}</td>
                </tr>
                <tr>
                    <th>Prix final</th>
                    <td>{{ businessVersion.moneyCents(version.snapshot.finalPrice|default(null)) }}</td>
                </tr>
                </tbody>
            </table>
        </div>
    {% endif %}

    {% if version.scope == 'ecommerce.product_variant_availability' %}
        <div class="panel panel-default">
            <div class="panel-heading">État du variant</div>
            <table class="table table-bordered">
                <tbody>
                <tr>
                    <th style="width: 220px;">Produit</th>
                    <td>{{ version.snapshot.product.label|default('Sans nom') }}</td>
                </tr>
                <tr>
                    <th>Variant</th>
                    <td>{{ version.snapshot.variant.label|default('Sans nom') }}</td>
                </tr>
                <tr>
                    <th>Dépublié</th>
                    <td>{{ version.snapshot.unPublished|default(false) ? 'Oui' : 'Non' }}</td>
                </tr>
                <tr>
                    <th>Épuisé</th>
                    <td>{{ version.snapshot.soldOut|default(false) ? 'Oui' : 'Non' }}</td>
                </tr>
                </tbody>
            </table>
        </div>
    {% endif %}

    <div class="row">
        <div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading">Metadata</div>
                <div class="panel-body">
                    {{ businessVersion.renderValue(version.metadata) }}
                </div>
            </div>
        </div>
        <div class="col-md-8">
            <div class="panel panel-default">
                <div class="panel-heading">Snapshot</div>
                <div class="panel-body">
                    {{ businessVersion.renderValue(version.snapshot) }}
                </div>
            </div>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            Historique de cet objet
        </div>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>Version</th>
                <th>Statut</th>
                <th>Créée le</th>
                <th>Utilisateur</th>
                <th>Action</th>
                <th>Détail</th>
            </tr>
            </thead>
            <tbody>
            {% for item in history %}
                <tr {% if item.id == version.id %}class="info"{% endif %}>
                    <td>#{{ item.version }}</td>
                    <td>{{ businessVersion.statusLabel(item.status) }}</td>
                    <td>{{ item.createdAt|date('d/m/Y H:i:s') }}</td>
                    <td>{{ item.createdBy|default('-') }}</td>
                    <td>{{ item.metadata.action|default('-') }}</td>
                    <td>
                        {% if item.id == version.id %}
                            <span class="btn btn-default btn-xs disabled">Version affichée</span>
                        {% else %}
                            <a href="{{ path('admin_business_version_show', returnParams|merge({ id: item.id })) }}" class="btn btn-info btn-xs">
                                <i class="fas fa-search"></i> Voir
                            </a>
                        {% endif %}
                    </td>
                </tr>
            {% else %}
                <tr>
                    <td colspan="6">Aucune autre version.</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
{% endblock %}
