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

{% block content %}
    <div class="page-header clearfix">
        <h1 class="pull-left">{{ 'emailing.domain.title'|trans }}</h1>
        <a class="btn btn-default pull-right" href="{{ path('admin_marketing_emailing') }}">{{ 'emailing.common.back'|trans }}</a>
    </div>
    <form class="form-inline panel panel-default" method="post" action="{{ path('admin_marketing_emailing_domain_create') }}">
        <input type="hidden" name="_token" value="{{ csrf_token('brevo_domain_create') }}">
        <div class="panel-body">
            <label for="brevo-domain">{{ 'emailing.domain.name'|trans }}</label>
            <input id="brevo-domain" class="form-control" type="text" name="domain" placeholder="example.com" required>
            <button class="btn btn-primary" type="submit">{{ 'emailing.domain.add'|trans }}</button>
        </div>
    </form>

    {% for item in domains %}
        {% set domain_name = item.domain ?? item.domain_name ?? '' %}
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <strong>{{ domain_name }}</strong>
                <span class="label {{ item.authenticated|default(false) ? 'label-success' : 'label-warning' }}">{{ item.authenticated|default(false) ? 'emailing.domain.authenticated'|trans : 'emailing.domain.pending'|trans }}</span>
                <form class="pull-right" method="post" action="{{ path('admin_marketing_emailing_domain_authenticate', {domain: domain_name}) }}">
                    <input type="hidden" name="_token" value="{{ csrf_token('brevo_domain_authenticate_' ~ domain_name) }}">
                    <button class="btn btn-info btn-sm" type="submit">{{ 'emailing.domain.verify_dns'|trans }}</button>
                </form>
            </div>
            <div class="table-responsive">
                <table class="table">
                    <thead><tr><th>{{ 'emailing.domain.record'|trans }}</th><th>{{ 'emailing.domain.type'|trans }}</th><th>{{ 'emailing.domain.host'|trans }}</th><th>{{ 'emailing.domain.value'|trans }}</th><th>{{ 'emailing.domain.status'|trans }}</th></tr></thead>
                    <tbody>
                    {% for record_name, record in item.dns_records|default([]) %}
                        <tr>
                            <td>{{ record_name }}</td><td><code>{{ record.type|default('TXT') }}</code></td>
                            <td><code>{{ record.host_name|default('') ?: '@' }}</code></td>
                            <td><code style="white-space:normal;word-break:break-all">{{ record.value|default('') }}</code></td>
                            <td>{{ record.status|default(false) ? 'emailing.domain.valid'|trans : 'emailing.domain.missing'|trans }}</td>
                        </tr>
                    {% else %}
                        <tr><td colspan="5" class="text-muted">{{ 'emailing.domain.no_records'|trans }}</td></tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    {% else %}
        <div class="alert alert-info">{{ 'emailing.domain.empty'|trans }}</div>
    {% endfor %}
{% endblock %}
