{% extends "freeform/notifications/_layout" %}

{% block actionButton %}

    {% if currentUser.can('freeform-notificationsManage') and settings.emailTemplateMethod != 'form' %}
        <div class="buttons">
            <div class="btngroup submit">
                <a class="btn submit add icon" href="{{ url('freeform/notifications/database/new') }}">
                    {{ "New Template"|t('freeform') }}
                </a>
            </div>
        </div>
    {% endif %}

{% endblock %}

{% set crumbs = [
    { label: craft.freeform.name, url: url("freeform") },
    { label: "Email Notifications"|t("freeform"), url: url("freeform/notifications") },
] %}

{% set content %}

    {% if notifications|length and settings.emailTemplateMethod != 'form' %}

        <div class="vue-admin-tablepane tablepane">
            <table data-file-templates class="data fullwidth collapsible">
                <thead>
                <th scope="col">{{ "Name"|t('freeform') }}</th>
                <th scope="col">{{ "Handle"|t('freeform') }}</th>
                <th scope="col">{{ "Description"|t('freeform') }}</th>
                <td class="thin"></td>
                </thead>
                <tbody>
                {# File based templates #}
                {% for id, notification in notifications %}
                    <tr data-id="{{ notification.id }}" data-name="{{ notification.name|t('freeform') }}">
                        <th scope="row" data-title="{{ 'Name'|t('freeform') }}">
                            {% if currentUser.can("freeform-notificationsManage") %}
                                <a href="{{ url('freeform/notifications/database/' ~ notification.id) }}">
                                    {{ notification.name }}
                                </a>
                            {% else %}
                                {{ notification.name }}
                            {% endif %}
                        </th>
                        <td data-title="{{ 'Handle'|t('freeform') }}">
                            <code>{{ notification.handle|truncater(40) }}</code>
                        </td>
                        <td style="width: 50%;">
                            {{ notification.description|truncater(80) }}
                        </td>
                        <td class="thin">
                            <div class='action-buttons'>
                                {% if currentUser.can("freeform-notificationsManage") %}
                                    <a class="clone icon" data-type="{{ type }}" data-id="{{ notification.id }}"
                                       title="{{ 'Duplicate'|t('freeform') }}" role="button"></a>
                                    <a class="delete icon" data-type="{{ type }}" data-id="{{ notification.id }}" title="{{ 'Delete'|t('freeform') }}" role="button"></a>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>

    {% elseif settings.emailTemplateMethod == 'form' %}

        <div class="zilch">
            {{ "Freeform is currently set to use only form-specific email notifications. You can create and manage them in each form's Notifications tab."|t('freeform') }}
        </div>

    {% else %}

        <div class="zilch">
            {{ "No database-based email notification templates exist yet."|t('freeform') }}
        </div>

    {% endif %}

{% endset %}


{% js %}
    var confirmDeleteMessage = '{{ "Are you sure you want to delete this notification?"|t('freeform') }}';

    var adminTable = new Craft.AdminTable({
    tableSelector: '#emailnotifications',
    noObjectsSelector: '#nonotifications',
    newObjectBtnSelector: '#newnotificationcontainer',
    deleteAction: 'freeform/notifications/database/delete',
    confirmDeleteMessage: confirmDeleteMessage,
    });

{% endjs %}
