{% requireAdmin %}

{% extends '_layouts/cp.twig' %}

{% set crumbs = [
    { label: "Settings"|t('app'), url: url('settings') }
] %}

{% set fullPageForm = true %}
{% set title = "Email Settings"|t('app') %}

{% import '_includes/forms.twig' as forms %}

{% do view.registerTranslations('app', [
    "Email sent successfully! Check your inbox.",
]) %}


{% if settings is not defined %}
    {% set settings = craft.app.projectConfig.get('email') %}
    {% set freshSettings = true %}
{% else %}
    {% set freshSettings = false %}
{% endif %}


{% block content %}
    {% if customMailerFiles|length %}
        <div class="readable">
            <blockquote class="note warning">
                <p>
                    {{ 'It looks like these settings are being overridden by {paths}.'|t('app', {
                        paths: customMailerFiles|join(', ')
                    }) }}
                </p>
            </blockquote>
        </div>
        <hr>
    {% endif %}

    {{ actionInput('system-settings/save-email-settings') }}
    {{ redirectInput('settings') }}

    {{ forms.autosuggestField({
        first: true,
        label: "System Email Address"|t('app'),
        instructions: "The email address Craft CMS will use when sending email."|t('app'),
        id: 'fromEmail',
        name: 'fromEmail',
        suggestEnvVars: true,
        value: settings.fromEmail,
        autofocus: true,
        required: true,
        errors: (freshSettings ? null : settings.getErrors('fromEmail'))
    }) }}

    {{ forms.autosuggestField({
        first: true,
        label: 'Reply-To Address'|t('app'),
        instructions: 'The Reply-To email address Craft CMS should use when sending email.'|t('app'),
        id: 'replyToEmail',
        name: 'replyToEmail',
        suggestEnvVars: true,
        value: settings.replyToEmail,
        errors: (freshSettings ? null : settings.getErrors('replyToEmail'))
    }) }}

    {{ forms.autosuggestField({
        label: "Sender Name"|t('app'),
        instructions: "The “From” name Craft CMS will use when sending email."|t('app'),
        id: 'fromName',
        name: 'fromName',
        suggestEnvVars: true,
        value: settings.fromName,
        required: true,
        errors: (freshSettings ? null : settings.getErrors('fromName'))
    }) }}

    {% if CraftEdition == CraftPro %}
        {{ forms.autosuggestField({
            label: "HTML Email Template"|t('app'),
            instructions: "The template Craft CMS will use for HTML emails"|t('app'),
            id: 'template',
            name: 'template',
            suggestions: craft.cp.getTemplateSuggestions(),
            suggestEnvVars: true,
            value: settings.template,
            errors: (freshSettings ? null : settings.getErrors('template'))
        }) }}
    {% endif %}

    <hr>

    {{ forms.selectField({
        label: "Transport Type"|t('app'),
        instructions: "How should Craft CMS send the emails?"|t('app'),
        id: 'transportType',
        name: 'transportType',
        options: transportTypeOptions,
        value: className(adapter),
        errors: adapter.getErrors('type') ?? null,
        toggle: true
    }) }}


    {% for _adapter in allTransportAdapters %}
        {% set isCurrent = (className(_adapter) == className(adapter)) %}
        <div id="{{ className(_adapter)|id }}"{% if not isCurrent %} class="hidden"{% endif %}>
            {% namespace 'transportTypes['~className(_adapter)~']' %}
                {{ (isCurrent ? adapter : _adapter).getSettingsHtml()|raw }}
            {% endnamespace %}
        </div>
    {% endfor %}

    <hr>

    <div class="buttons">
        <button type="button" id="test" class="btn formsubmit" data-action="system-settings/test-email-settings">{{ "Test"|t('app') }}</button>
    </div>
{% endblock %}
