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

{% block content %}
    <div class="panel panel-default">
        <div class="panel-heading">
            Filtres
        </div>
        <div class="panel-body">
            <form method="get" action="{{ path('admin_promo') }}">
                <div class="row">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="search">Recherche</label>
                            <input
                                type="text"
                                name="search"
                                id="search"
                                class="form-control"
                                value="{{ filters.search_input }}"
                                placeholder="Code, email client, email notif"
                            >
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="application">Application</label>
                            <select name="application" id="application" class="form-control">
                                <option value="all" {% if filters.application == 'all' %}selected{% endif %}>Toutes</option>
                                <option value="auto" {% if filters.application == 'auto' %}selected{% endif %}>Auto</option>
                                <option value="manual" {% if filters.application == 'manual' %}selected{% endif %}>Code promo</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="assignment">Destinataire</label>
                            <select name="assignment" id="assignment" class="form-control">
                                <option value="all" {% if filters.assignment == 'all' %}selected{% endif %}>Tous</option>
                                <option value="user" {% if filters.assignment == 'user' %}selected{% endif %}>Client réservé</option>
                                <option value="notification" {% if filters.assignment == 'notification' %}selected{% endif %}>Email notif</option>
                                <option value="unassigned" {% if filters.assignment == 'unassigned' %}selected{% endif %}>Sans destinataire</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="benefit">Avantage</label>
                            <select name="benefit" id="benefit" class="form-control">
                                <option value="all" {% if filters.benefit == 'all' %}selected{% endif %}>Tous</option>
                                <option value="cart" {% if filters.benefit == 'cart' %}selected{% endif %}>Sur le panier</option>
                                <option value="shipping" {% if filters.benefit == 'shipping' %}selected{% endif %}>Frais de port</option>
                                <option value="gift" {% if filters.benefit == 'gift' %}selected{% endif %}>Produit offert</option>
                            </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="all" {% if filters.status == 'all' %}selected{% endif %}>Tous</option>
                                <option value="published" {% if filters.status == 'published' %}selected{% endif %}>Publiés</option>
                                <option value="unpublished" {% if filters.status == 'unpublished' %}selected{% endif %}>Dépubliés</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="origin">{{ 'admin.promo.filters.origin.label'|trans({}, 'messages', 'fr') }}</label>
                            <select name="origin" id="origin" class="form-control">
                                <option value="all" {% if filters.origin == 'all' %}selected{% endif %}>{{ 'admin.promo.filters.origin.all'|trans({}, 'messages', 'fr') }}</option>
                                <option value="abandoned_cart" {% if filters.origin == 'abandoned_cart' %}selected{% endif %}>{{ 'admin.promo.filters.origin.abandoned_cart'|trans({}, 'messages', 'fr') }}</option>
                                <option value="other" {% if filters.origin == 'other' %}selected{% endif %}>{{ 'admin.promo.filters.origin.other'|trans({}, 'messages', 'fr') }}</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_promo') }}" class="btn btn-default btn-block">Reinitialiser</a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading">
            Code Remise ({{ items|length }})
            <div class="pull-right">
                <div class="btn-group">
                    <a href="{{ path('admin_promo_new') }}" type="button" class="btn btn-default btn-xs">
                        Ajouter
                    </a>
                </div>
            </div>
        </div>
        <div class="table-responsive">
            <table class="table table-bordered dataTable" width="100%" cellspacing="0">
                <thead>
                <tr>
                    <th>#</th>
                    <th>Code</th>
                    <th>Type</th>
                    <th>Conditions</th>
                    <th>Début</th>
                    <th>Fin</th>
                    <th>Utilisé</th>
                    <th>Auto</th>
                    <th>Publier</th>
                    <th>Action</th>
                </tr>
                </thead>
                <tfoot>
                <tr>
                    <th>#</th>
                    <th>Code</th>
                    <th>Type</th>
                    <th>Conditions</th>
                    <th>Début</th>
                    <th>Fin</th>
                    <th>Utilisé</th>
                    <th>Auto</th>
                    <th>Publier</th>
                    <th>Action</th>
                </tr>
                </tfoot>
                <tbody>
                {% for item in items %}
                    {% set condition_line_1 = [] %}
                    {% set condition_line_2 = [] %}
                    {% set assignment_line = [] %}

                    {% if item.minEquivalency is not null %}
                        {% set condition_line_1 = condition_line_1|merge(['Min: ' ~ item.minEquivalency]) %}
                    {% endif %}
                    {% if item.maxEquivalency is not null %}
                        {% set condition_line_1 = condition_line_1|merge(['Max: ' ~ item.maxEquivalency]) %}
                    {% endif %}
                    {% if item.minSubtotalTTC is not null %}
                        {% set condition_line_1 = condition_line_1|merge([
                            'admin.promo.condition_cart_min'|trans({'%amount%': (item.minSubtotalTTC / 100)|number_format(2, ',', ' ')})
                        ]) %}
                    {% endif %}
                    {% if item.maxSubtotalTTC is not null %}
                        {% set condition_line_1 = condition_line_1|merge([
                            'admin.promo.condition_cart_max'|trans({'%amount%': (item.maxSubtotalTTC / 100)|number_format(2, ',', ' ')})
                        ]) %}
                    {% endif %}

                    {% if item.maxCountByCustomer is not null %}
                        {% set condition_line_2 = condition_line_2|merge(['Client: ' ~ item.maxCountByCustomer]) %}
                    {% endif %}
                    {% if item.maxCount is not null %}
                        {% set condition_line_2 = condition_line_2|merge(['Total: ' ~ item.maxCount]) %}
                    {% endif %}
                    {% if item.cumulative %}
                        {% set condition_line_2 = condition_line_2|merge(['Cumulable']) %}
                    {% endif %}
                    {% if item.multipleByQuantity %}
                        {% set condition_line_2 = condition_line_2|merge(['Multiples']) %}
                    {% endif %}
                    {% if item.restrictedCountryCodes is not empty %}
                        {% set delivery_countries = [] %}
                        {% for country_code in item.restrictedCountryCodes %}
                            {% set delivery_countries = delivery_countries|merge([country_code|country_name]) %}
                        {% endfor %}
                        {% set condition_line_2 = condition_line_2|merge([
                            'admin.promo.condition_delivery_countries'|trans({'%countries%': delivery_countries|join(', ')})
                        ]) %}
                    {% endif %}
                    {% if item.assignedUser %}
                        {% set assignment_line = assignment_line|merge(['Client réservé : ' ~ item.assignedUser.email]) %}
                    {% endif %}
                    {% if item.notificationEmail %}
                        {% set assignment_line = assignment_line|merge(['Notification : ' ~ item.notificationEmail]) %}
                    {% endif %}

                    <tr>
                        <td>{{ loop.index }}</td>
                        <td>{{ item.title }}</td>
                        <td>
                            {% if item.offeredVariant %}
                                <strong>Cadeau</strong><br>
                                <small class="text-muted">{{ item.offeredVariant.name }}</small>
                            {% elseif item.percent is not null %}
                                {% set percent_value = item.percent * 100 %}
                                {% set percent_decimals = percent_value == percent_value|round ? 0 : 2 %}
                                <strong>Remise</strong><br>
                                <small class="text-muted">{{ percent_value|number_format(percent_decimals, ',', ' ') }} %</small>
                            {% elseif item.amountTTC is not null %}
                                <strong>Montant</strong><br>
                                <small class="text-muted">{{ (item.amountTTC / 100)|number_format(2, ',', ' ') }} €</small>
                            {% elseif item.percentShippingCost is not null %}
                                {% set shipping_percent_value = item.percentShippingCost * 100 %}
                                {% set shipping_percent_decimals = shipping_percent_value == shipping_percent_value|round ? 0 : 2 %}
                                <strong>Livraison</strong><br>
                                <small class="text-muted">{{ shipping_percent_value|number_format(shipping_percent_decimals, ',', ' ') }} % port</small>
                            {% else %}
                                <strong>-</strong>
                            {% endif %}
                        </td>
                        <td>
                            <small class="text-muted">
                                {% if condition_line_1 is not empty %}
                                    {{ condition_line_1|join('  |  ') }}
                                {% else %}
                                    Aucune condition de seuil
                                {% endif %}
                            </small>
                            <br>
                            <small class="text-muted">
                                {% if condition_line_2 is not empty %}
                                    {{ condition_line_2|join('  |  ') }}
                                {% else %}
                                    Aucune autre limite
                                {% endif %}
                            </small>
                            <br>
                            <small class="text-muted">
                                {% if assignment_line is not empty %}
                                    {{ assignment_line|join('  |  ') }}
                                {% else %}
                                    Aucun destinataire spécifique
                                {% endif %}
                            </small>
                        </td>
                        <td>{{ item.beginAt | date('d-m-Y H:i') }}</td>
                        <td>{{ item.endAt | date('d-m-Y H:i') }}</td>
                        <td>{{ item.count }}</td>
                        <td>{% if item.auto %}Oui{% else %}Non{% endif %}</td>
                        <td>
                            {% if item.disabled %}
                                <a href="{{ path('admin_promo_enable', { id: item.id}) }}" data-toggle="tooltip" title="Activer le code remise {{ item.title }}">
                                    <i class="fas fa-circle fa-2x color_red"></i>
                                </a>
                            {% else %}
                                <a href="{{ path('admin_promo_disable', { id: item.id}) }}" data-toggle="tooltip" title="Désactiver le code remise {{ item.title }}">
                                    <i class="fas fa-circle fa-2x color_green"></i>
                                </a>
                            {% endif %}
                        </td>
                        <td>
                            <a href="{{ path('admin_promo_edit', { id: item.id}) }}" class="btn btn-info btn-xs"
                               data-toggle="tooltip" title="Editer le code remise {{ item.title }}"><i
                                        class="fas fa-edit fa-2x"></i></a>

                            <a href="{{ path('admin_promo_delete', { id: item.id }) }}"
                               class="confirm-deletable btn btn-danger btn-xs"
                               data-toggle="tooltip" title="Supprimer la récompense {{ item.title }}" data-title="Supprimer le code remise{{ item.title }}">
                                <i class="fa fa-2x fa-trash"></i></a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}
