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

{% block content %}
    <div class="page-header clearfix">
        <h1 class="pull-left">{{ 'emailing.admin.title'|trans }}</h1>
        <div class="pull-right">
            {% if is_granted('ROLE_SUPER_ADMIN') %}
                <a class="btn btn-default" href="{{ path('admin_marketing_emailing_configuration') }}"><i class="fas fa-key"></i> {{ 'emailing.configuration.title'|trans }}</a>
                <a class="btn btn-default" href="{{ path('admin_marketing_emailing_domains') }}"><i class="fas fa-globe"></i> {{ 'emailing.domain.title'|trans }}</a>
            {% endif %}
            <a class="btn btn-info" href="{{ path('admin_marketing_emailing_lists') }}"><i class="fas fa-users"></i> {{ 'emailing.list.title'|trans }}</a>
            <a class="btn btn-primary" href="{{ path('admin_marketing_emailing_campaign_new') }}"><i class="fas fa-plus"></i> {{ 'emailing.campaign.new'|trans }}</a>
        </div>
    </div>

    {% if not configuration or not configuration.enabled %}
        <div class="alert alert-warning">{{ 'emailing.configuration.disabled_warning'|trans }}</div>
    {% endif %}

    <div class="panel panel-default">
        <div class="panel-heading">{{ 'emailing.campaign.history'|trans }}</div>
        <div class="table-responsive">
            <table class="table table-striped">
                <thead><tr><th>{{ 'emailing.campaign.title'|trans }}</th><th>{{ 'emailing.campaign.subject'|trans }}</th><th>{{ 'emailing.campaign.status'|trans }}</th><th>{{ 'emailing.campaign.created_at'|trans }}</th><th></th></tr></thead>
                <tbody>
                {% for campaign in campaigns %}
                    <tr>
                        <td>{{ campaign.title }}</td>
                        <td>{{ campaign.subject }}</td>
                        <td>
                            <span class="label {{ campaign.status == 'sent' ? 'label-success' : (campaign.status == 'failed' ? 'label-danger' : 'label-info') }}">
                                {{ ('emailing.campaign.status_' ~ campaign.status)|trans }}
                            </span>
                            {% if campaign.lastError %}<div class="text-danger small">{{ campaign.lastError }}</div>{% endif %}
                        </td>
                        <td>{{ campaign.createdAt|date('d/m/Y H:i') }}</td>
                        <td class="text-right">
                            {% if campaign.brevoCampaignId and campaign.status != 'sent' %}
                                <form method="post" action="{{ path('admin_marketing_emailing_campaign_send', {id: campaign.id}) }}" onsubmit="return confirm('{{ 'emailing.campaign.send_confirm'|trans|e('js') }}')">
                                    <input type="hidden" name="_token" value="{{ csrf_token('brevo_campaign_send_' ~ campaign.id) }}">
                                    <button class="btn btn-success btn-sm" type="submit"><i class="fas fa-paper-plane"></i> {{ 'emailing.campaign.send'|trans }}</button>
                                </form>
                            {% endif %}
                        </td>
                    </tr>
                {% else %}
                    <tr><td colspan="5" class="text-center text-muted">{{ 'emailing.campaign.empty'|trans }}</td></tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}
