{% extends 'formie/_layouts' %}

{% set crumbs = [
    { label: craft.formie.getPluginName() | t('formie'), url: url('formie') },
    { label: 'Settings' | t('app'), url: url('formie/settings') },
    { label: 'Email Templates' | t('formie'), url: url('formie/settings/email-templates') },
] %}

{% set selectedSubnavItem = 'settings' %}

{% set fullPageForm = true %}

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

{% block content %}

<input type="hidden" name="action" value="formie/email-templates/save">
{{ redirectInput('formie/settings/email-templates') }}

{% if template.id %}
    <input type="hidden" name="sortOrder" value="{{ template.sortOrder }}">
    <input type="hidden" name="id" value="{{ template.id }}">
{% endif %}

{{ forms.textField({
    label: 'Name' | t('formie'),
    first: true,
    instructions: 'What this template will be called in the control panel.' | t('formie'),
    id: 'name',
    name: 'name',
    value: template.name,
    errors: template.getErrors('name'),
    required: true,
}) }}

{{ forms.textField({
    label: 'Handle' | t('formie'),
    instructions: 'How you’ll refer to this template in the templates.' | t('formie'),
    id: 'handle',
    class: 'code',
    name: 'handle',
    value: template.handle,
    errors: template.getErrors('handle'),
    required: true,
}) }}

{{ forms.autosuggestField({
    label: 'HTML Template' | t('formie'),
    instructions: 'The template that will be used to render the email.' | t('formie'),
    id: 'template',
    name: 'template',
    suggestions: craft.cp.getTemplateSuggestions(),
    suggestEnvVars: false,
    value: template.template,
    errors: template.getErrors('template'),
    required: true,
}) }}

{% if not template.id %}
    {{ forms.lightswitchField({
        label: 'Copy Templates' | t('formie'),
        instructions: 'Enable this option to automatically copy template files into the template directory.' | t('formie'),
        tip: 'The directory must be empty for files to be copied.' | t('formie'),
        name: 'copyTemplates',
        on: template.copyTemplates,
    }) }}
{% endif %}

{% endblock %}

{% js %}
    {% if not template.handle %}new Craft.HandleGenerator('#name', '#handle');{% endif %}
{% endjs %}
