{% from 'forms/_form.html' import form_header, form_footer, form_fieldset, form_rows %}
{% from 'message_box.html' import message_box %}
{% set editable = not reminder or not reminder.is_sent %}
{% set disabled_fields = () if editable else form._fields.viewkeys() %}
{% set widget_attrs = form.default_widget_attrs %}

<div class="reminders">
    {% if reminder and reminder.is_overdue %}
        {% call message_box('warning', fixed_width=true) %}
            {% trans %}This reminder is overdue and will be sent within a few minutes.{% endtrans %}
        {% endcall %}
    {% endif %}

    {{ form_header(form, id='reminder-form') }}
    {{ form_rows(form, fields=form.schedule_fields, widget_attrs=widget_attrs, disable=disabled_fields) }}
    {% call form_fieldset(_('Recipients')) %}
        {{ form_rows(form, fields=form.recipient_fields, widget_attrs=widget_attrs, disable=disabled_fields) }}
    {% endcall %}
    {% call form_fieldset(_('Misc')) %}
        {{ form_rows(form, skip=form.schedule_recipient_fields, widget_attrs=widget_attrs, disable=disabled_fields) }}
    {% endcall %}
    {% call form_footer(form) %}
        {% if editable %}
            <input id="save-button" class="i-button big highlight" type="submit"
                   value="{% trans %}Save{% endtrans %}" data-disabled-until-change>
            <input id="preview-reminder" class="i-button big" type="button"
                   value="{%- trans %}Preview email{% endtrans -%}"
                   data-href="{{ url_for('.preview', event) }}">
        {% endif %}
        <a href="{{ url_for('.list', event) }}" class="i-button big" data-button-back>
            {%- trans %}Back{% endtrans -%}
        </a>
    {% endcall %}

    {% if editable %}
        <script>
            (function() {
                'use strict';

                $('#absolute_date').datepicker({minDate: 0});
                $('#preview-reminder').ajaxDialog({
                    title: $T('Reminder Preview'),
                    method: 'POST',
                    data: function() {
                        return {
                            include_summary: $('#include_summary').prop('checked') ? '1' : '0',
                            include_description: $('#include_description').prop('checked') ? '1' : '0',
                            message: $('#message').val()
                        };
                    }
                });
            })();
        </script>
    {% endif %}
</div>
