{% extends request.ajax ? 'layout_blank.html.twig' : 'layout_default.html.twig' %}

{% import 'macro_functions.html.twig' as mf %}

{% block meta_title %}{{ 'Email history'|trans }}{% endblock %}

{% set active_menu = 'activity' %}

{% block content %}
    {% set config = admin.extension_config_get({ 'ext': 'mod_email' }) %}
    {% if config.log_enabled is not defined or config.log_enabled != 1 %}
        <div class="alert alert-primary alert-dismissible fade show mb-3" role="alert">
            <strong>{{ 'Information'|trans }}:</strong> {{ 'Email logging is not enabled. If you want to log sent mails to database, enable it in'|trans }}
                <a href="{{ 'extension/settings/email'|alink }}"> {{ 'email settings.'|trans }}</a>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="{{ 'Close'|trans }}"></button>
        </div>
    {% endif %}

<div class="card">
    <div class="card-body">
        <h3>{{ 'Email history'|trans }}</h3>
    </div>

    {{ include('partial_search.html.twig') }}
    <table class="table card-table table-vcenter table-striped text-nowrap">
        <thead>
            <tr>
                <th class="w-1">
                    <input class="form-check-input m-0 align-middle batch-delete-master-checkbox" type="checkbox">
                </th>
                <th>{{ 'To'|trans }}</th>
                <th>{{ 'From'|trans }}</th>
                <th>{{ 'Subject'|trans }}</th>
                <th>{{ 'Date'|trans }}</th>
                <th class="w-1"></th>
            </tr>
        </thead>
        <tbody>
        {% set emails = admin.email_email_get_list({ 'per_page': 30, 'page': request.page }|merge(request)) %}
        {% for i, email in emails.list %}
        <tr>
            <td>
                <input class="form-check-input m-0 align-middle batch-delete-checkbox" type="checkbox" data-item-id="{{ email.id }}">
            </td>
            <td>{{ email.recipients }}</td>
            <td>{{ email.sender }}</td>
            <td>
                <a href="{{ '/email'|alink }}/{{ email.id }}">{{ email.subject|truncate(40) }}</a>
            </td>
            <td>{{ email.created_at|format_date }}</td>
            <td>
                <a class="btn btn-icon" href="{{ '/email'|alink }}/{{ email.id }}">
                    <svg class="icon">
                        <use xlink:href="#edit" />
                    </svg>
                </a>
                <a class="btn btn-icon api-link"
                    href="{{ 'api/admin/email/email_delete'|link({ 'id': email.id }) }}"
                    data-api-redirect="{{ 'email/history'|alink }}"
                    data-api-confirm="{{ 'Are you sure?'|trans }}">
                    <svg class="icon">
                        <use xlink:href="#delete" />
                    </svg>
                </a>
            </td>
        </tr>
        {% else %}
            <tr>
                <td class="text-muted" colspan="7">{{ 'The list is empty'|trans }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>

    <div class="card-footer d-flex align-items-center justify-content-between">
        {{ include('partial_batch_delete.html.twig', { 'action': 'admin/email/batch_delete' }) }}
        {{ include('partial_pagination.html.twig', { 'list': emails, 'url': 'email/history' }) }}
    </div>
</div>
{% endblock %}
