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

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

{% set active_menu = 'activity' %}
{% set has_filter_panel = true %}
{% set filter_panel_id = 'page-search-filters' %}
{% set filter_panel_open = request_has_filters %}
{% set can_manage_settings = has_permission('email', 'manage_settings') %}
{% set can_view_email_history = has_permission('email', 'view_email_history') %}
{% set can_delete_email_history = has_permission('email', 'delete_email_history') %}

{% 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 }}
            {% if can_manage_settings %}
                <a href="{{ 'extension/settings/email'|url }}">{{ 'Email Settings.'|trans }}</a>
            {% else %}
                {{ 'Email Settings.'|trans }}
            {% endif %}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="{{ 'Close'|trans }}"></button>
        </div>
    {% endif %}

<div class="card">
    <div class="card-header">
        <div>
            <h3 class="card-title">{{ 'Email History'|trans }}</h3>
            <p class="card-subtitle">{{ 'Review sent emails, inspect individual messages, and manage stored email logs.'|trans }}</p>
        </div>
    </div>

    {% if can_view_email_history %}
    {{ include('partial_search.html.twig', {
        search_body_class: filter_panel_open ? 'pb-2 border-bottom-0' : ''
    }) }}
    <section id="{{ filter_panel_id }}" class="card-body pt-0 px-3 pb-3 border-bottom collapse{% if filter_panel_open %} show{% endif %}">
        <form method="get" class="border border-top-0 rounded-top-0 rounded-bottom-3 bg-body-tertiary p-3">
            {% if request.search %}
                <input type="hidden" name="search" value="{{ request.search }}">
            {% endif %}
            <div class="row g-3">
                <div class="col-md-6 col-xl-3">
                    <div>
                        <label class="form-label text-muted small mb-1" for="email-history-id">{{ 'ID'|trans }}</label>
                        <input class="form-control" type="text" id="email-history-id" name="id" value="{{ request.id }}" placeholder="{{ 'Email ID'|trans }}">
                    </div>
                </div>
                <div class="col-md-6 col-xl-3">
                    <div>
                        <label class="form-label text-muted small mb-1" for="email-history-sender">{{ 'From'|trans }}</label>
                        <input class="form-control" type="text" id="email-history-sender" name="sender" value="{{ request.sender }}" placeholder="{{ 'Sender Email'|trans }}">
                    </div>
                </div>
                <div class="col-md-6 col-xl-3">
                    <div>
                        <label class="form-label text-muted small mb-1" for="email-history-recipient">{{ 'To'|trans }}</label>
                        <input class="form-control" type="text" id="email-history-recipient" name="recipient" value="{{ request.recipient }}" placeholder="{{ 'Recipient Email'|trans }}">
                    </div>
                </div>
                <div class="col-md-6 col-xl-3">
                    <div>
                        <label class="form-label text-muted small mb-1" for="email-history-subject">{{ 'Subject'|trans }}</label>
                        <input class="form-control" type="text" id="email-history-subject" name="subject" value="{{ request.subject }}" placeholder="{{ 'Email Subject'|trans }}">
                    </div>
                </div>
                <div class="col-md-6 col-xl-4">
                    <div>
                        <label class="form-label text-muted small mb-1" for="email-history-client">{{ 'Client'|trans }}</label>
                        <select class="form-control autocomplete-selector"
                                placeholder="{{ 'Start Typing the Client Name or ID'|trans }}"
                                id="email-history-client"
                                name="client_id"
                                data-resturl="/api/admin/client/get_pairs">
                        {% if request.client_id %}
                            {% set client = admin.client_get({ 'id': request.client_id }) %}
                            <option value="{{ request.client_id }}" selected>{{ client.first_name }} {{ client.last_name }} ({{ client.email }})</option>
                        {% endif %}
                        </select>
                    </div>
                </div>
                <div class="col-md-6 col-xl-4">
                    <div>
                        <label class="form-label text-muted small mb-1" for="email-history-date-range">{{ 'Date Range'|trans }}</label>
                        <div class="input-group">
                            <div class="input-icon w-100">
                                <input class="form-control datepicker"
                                       id="email-history-date-range"
                                       placeholder="{{ 'Select Date Range'|trans }}"
                                       value="{% if request.date_from %}{{ request.date_from|date('Y-m-d') }}{% endif %}{% if request.date_to %} to {{ request.date_to|date('Y-m-d') }}{% endif %}"
                                       data-name-from="date_from"
                                       data-name-to="date_to">
                                <span class="input-icon-addon">
                                    <svg class="icon">
                                        <use xlink:href="#calendar" />
                                    </svg>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="d-flex justify-content-end gap-2 mt-3">
                <a class="btn btn-outline-secondary" href="{{ request_path|url }}">{{ 'Reset'|trans }}</a>
                <button class="btn btn-primary" type="submit">
                    <svg class="icon">
                        <use xlink:href="#filter" />
                    </svg>
                    {{ 'Apply Filters'|trans }}
                </button>
            </div>
        </form>
    </section>
    <div class="table-responsive">
    <table class="table card-table table-vcenter table-striped text-nowrap">
        <thead>
            <tr>
                {% if can_delete_email_history %}
                <th class="w-1">
                    <input class="form-check-input m-0 align-middle batch-delete-master-checkbox" type="checkbox">
                </th>
                {% endif %}
                <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': 25, 'page': request.page }|merge(request)) %}
        {% for i, email in emails.list %}
        <tr>
            {% if can_delete_email_history %}
            <td>
                <input class="form-check-input m-0 align-middle batch-delete-checkbox" type="checkbox" data-item-id="{{ email.id }}">
            </td>
            {% endif %}
            <td>{{ email.recipients }}</td>
            <td>{{ email.sender }}</td>
            <td>
                <a href='{{ "email/#{email.id}"|url }}'>{{ email.subject|truncate(40) }}</a>
            </td>
            <td>{{ email.created_at|format_date }}</td>
            <td>
                <button class="btn btn-icon email-preview-btn"
                    data-email-subject="{{ email.subject }}"
                    data-email-content="{{ email.content_html|e('html_attr') }}"
                    title="{{ 'Preview'|trans }}">
                    <svg class="icon">
                        <use xlink:href="#eye" />
                    </svg>
                </button>
                <a class="btn btn-icon" href='{{ "email/#{email.id}"|url }}'>
                    <svg class="icon">
                        <use xlink:href="#edit" />
                    </svg>
                </a>
                {% if can_delete_email_history %}
                <a class="btn btn-icon" href="{{ 'email/email_delete'|api_url(query: { 'id': email.id }) }}"
                    {{ fb_api_link({modal: {type: 'confirm', title: 'Are you sure?'|trans}, redirect: 'email/history'|url}) }}>
                    <svg class="icon">
                        <use xlink:href="#delete" />
                    </svg>
                </a>
                {% endif %}
            </td>
        </tr>
        {% else %}
            <tr>
                <td class="text-muted text-center py-3" colspan="{{ can_delete_email_history ? 6 : 5 }}">{{ 'The list is empty'|trans }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    </div>

    <div class="card-footer d-flex align-items-center justify-content-between">
        {% if can_delete_email_history %}
            {% include "partial_batch_delete.html.twig" with { 'action': 'email/batch_delete' } %}
        {% endif %}
        {% include "partial_pagination.html.twig" with { 'list': emails, 'url': 'email/history' } %}
    </div>
    {% else %}
        <div class="card-body">
            <div class="alert alert-warning mb-0" role="alert">
                {{ 'You do not have permission to view email history.'|trans }}
            </div>
        </div>
    {% endif %}
</div>
{% endblock %}

{% block js %}
{{ include('partial_email_preview.html.twig') }}
{% endblock %}
