{% macro _render_link_regform_button(regform, tpl) %}
    <a data-href="{{ url_for('designer.link_regform', regform, template_id=tpl.id) }}"
       data-method="POST"
       data-update="#template-list">
        {{ regform.title }}
    </a>
{% endmacro %}

{% macro _render_badge_form_button(regform, tpl, format='text', text='') %}
    {% set no_registrations = not regform.registrations|rejectattr('is_deleted')|list %}
    {% set no_registrations_msg %}
        {% trans %}Registration Form has no registrations{% endtrans %}
    {% endset %}
    {% set print_title -%}
        {%- if tpl.is_ticket -%}
            {% trans %}Print tickets{% endtrans %}
        {%- else -%}
            {% trans %}Print badges{% endtrans %}
        {%- endif -%}
    {%- endset %}
    {% set endpoint = 'registrations_config_tickets' if tpl.is_ticket else 'registrations_config_badges' %}
    {% if format == 'icon' %}
        <a title="{{ no_registrations_msg if no_registrations else print_title }}"
           class="i-button icon-printer icon-only text-color borderless highlight
                  {% if no_registrations -%}
                      disabled
                  {%- endif %}"
           data-href="{{ url_for('event_registration.' + endpoint, regform, template_id=tpl.id) }}"
           data-title="{{ print_title }}"
           data-method="POST"
           data-ajax-dialog
           {% for attrs in template_hook('badge-form-button-extra-params', as_list=true, tpl=tpl, regform=regform) %}
               {{ attrs | html_params }}
           {% endfor %}>
        </a>
    {% else %}
        {% if no_registrations %}
            <span class="text-not-important" title="{{ no_registrations_msg }}">
                {{ text }}
            </span>
        {% else %}
            <a data-href="{{ url_for('event_registration.' + endpoint, regform, template_id=tpl.id) }}"
               data-title="{{ print_title }}"
               data-method="POST"
               data-ajax-dialog
               {% if no_registrations %}
                   title="{{ no_registrations_msg }}"
               {% endif %}
               {% for attrs in template_hook('badge-form-button-extra-params', as_list=true, tpl=tpl, regform=regform) %}
                   {{ attrs | html_params }}
               {% endfor %}>
                {{ text }}
            </a>
        {% endif %}
    {% endif %}
{% endmacro %}

{% macro _render_template(tpl, target, linkable_forms, inherited=false, event=none, default_ticket=none, default_badge=none,
                          not_deletable_templates=[]) -%}
    <tr>
        <td class="i-table id-column">
            <i class="designer-template-type-icon template-icon-{{ tpl.type.name }}"
               title="{{ tpl.type.title }}"></i>
        </td>
        <td>
            {{ tpl.title }}
        </td>
        <td class="text-superfluous">
            {% if inherited %}
                {% if not tpl.category.is_root %}
                    {% trans title=tpl.category.title -%}
                        from category "{{ title }}"
                    {% endtrans %}
                {% endif %}
            {% endif %}
        </td>
        <td class="flexrow f-a-center">
            {% if tpl.registration_form %}
                <span>
                    {{- pgettext('Designer template linked to registration form', 'Linked to') -}}
                    <a href="{{ url_for('event_registration.manage_regform', tpl.registration_form) }}">
                        {{ tpl.registration_form.title }}
                    </a>
                </span>
                {% set unlinkable = tpl.is_unlinkable %}
                <button class="i-button icon-cross icon-only text-color borderless danger {{ 'not-unlinkable' if not unlinkable }}"
                        style="padding-left: 0.2em;"
                        {% if unlinkable %}
                            title="{% trans %}Unlink registration form{% endtrans %}"
                            data-href="{{ url_for('designer.unlink_regform', tpl) }}"
                            data-method="POST"
                            data-update="#template-list"
                        {% else %}
                            title="{% trans %}This template cannot be unlinked because it contains placeholders referencing the linked registration form.{% endtrans %}"
                        {% endif %}>
                </button>
            {% endif %}
        </td>
        <td>
            <div class="thin toolbar right">
                <div class="group">
                    {% if not tpl.registration_form and linkable_forms[tpl] %}
                        <button class="i-button icon-quill icon-only highlight text-color js-dropdown borderless"
                                title="{% trans %}
                                            Link to a registration form. This will enable you to use all
                                            registration form fields in the template.
                                       {% endtrans %}"
                                data-toggle="dropdown"></button>
                        <ul class="i-dropdown">
                            {% for regform in linkable_forms[tpl] %}
                                <li>
                                    {{ _render_link_regform_button(regform, tpl) }}
                                </li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                    {% block designer_tpl_ticket_action scoped %}
                        {% if not event and tpl.type.name == 'badge' and tpl.is_ticket %}
                            {% set inherited_default = default_ticket == tpl and tpl.category != target and
                                    not target.default_ticket_template %}
                            {% set global_default = default_ticket == tpl and target.is_root %}
                            <button class="i-button icon-ticket icon-only borderless hide-if-locked toggle-default
                                    {{ 'default-on-category' if default_ticket == tpl }}
                                    {{ 'inherited' if inherited_default }}
                                    {{ 'global-default' if global_default }}"
                                    {% if inherited_default %}
                                        title="{% trans %}This is the inherited default ticket template{% endtrans %}"
                                    {% elif global_default %}
                                        title="{% trans %}This is the global default ticket template{% endtrans %}"
                                    {% else %}
                                        title="{% trans %}Toggle default ticket template for this category{% endtrans %}"
                                        data-href="{{ url_for('designer.toggle_category_default_ticket', target, template_id=tpl.id) }}"
                                        data-method="POST"
                                        data-update="#template-list"
                                    {% endif %}>
                            </button>
                        {% endif %}
                    {% endblock %}
                    {% block designer_tpl_badge_action scoped %}
                        {% if not event and tpl.type.name == 'badge' and not tpl.is_ticket %}
                            {% set inherited_default = default_badge == tpl and tpl.category != target and
                                    not target.default_badge_template %}
                            {% set global_default = default_badge == tpl and target.is_root %}
                            <button class="i-button icon-tag icon-only borderless hide-if-locked toggle-default
                                    {{ 'default-on-category' if default_badge == tpl }}
                                    {{ 'inherited' if inherited_default }}
                                    {{ 'global-default' if global_default }}"
                                    {% if inherited_default %}
                                        title="{% trans %}This is the inherited default badge template{% endtrans %}"
                                    {% elif global_default %}
                                        title="{% trans %}This is the global default badge template{% endtrans %}"
                                    {% else %}
                                        title="{% trans %}Toggle default badge template for this category{% endtrans %}"
                                        data-href="{{ url_for('designer.toggle_category_default_badge', target, template_id=tpl.id) }}"
                                        data-method="POST"
                                        data-update="#template-list"
                                    {% endif %}>
                            </button>
                        {% endif %}
                    {% endblock %}
                    {% set clonable = tpl.is_clonable or (not event and tpl.owner == target) %}
                    <button class="i-button icon-copy icon-only text-color borderless hide-if-locked
                        {{ 'not-clonable' if not clonable }}"
                        {% if not clonable %}
                            title="{% trans %}This template cannot be cloned{% endtrans %}">
                        {% else %}
                            data-href="{{ url_for('designer.clone_template', target, template_id=tpl.id) }}"
                            title="{% trans %}Clone this template{% endtrans %}"
                            data-update="#template-list"
                            {% if event %}
                                data-method="POST"
                                data-confirm="{% trans title=tpl.title -%}Are you sure you want to clone template '{{ title }}'?{%- endtrans %}">
                            {% else %}
                                data-title="{% trans title=tpl.title %}Clone '{{ title }}' template into a category{% endtrans %}"
                                data-ajax-dialog>
                            {% endif %}
                        {% endif %}
                    </button>
                    {% if not inherited %}
                        <a class="i-button icon-edit icon-only text-color borderless hide-if-locked"
                           href="{{ url_for('designer.edit_template', tpl) }}"
                           title="{% trans %}Edit template{% endtrans %}"></a>
                    {% endif %}
                    {% if not inherited %}
                        <button class="i-button icon-remove icon-only text-color borderless danger hide-if-locked
                                {{ 'not-deletable' if tpl in not_deletable_templates }}"
                                {% if tpl in not_deletable_templates %}
                                    {% if tpl.is_system_template %}
                                        title="{% trans %}This template cannot be deleted.{% endtrans %}"
                                    {% else %}
                                        title="{% trans %}This template cannot be deleted because it is used in a registration form or as a backside for another template.{% endtrans %}"
                                    {% endif %}
                                {% else %}
                                    title="{% trans %}Delete template{% endtrans %}"
                                    data-href="{{ url_for('designer.delete_template', tpl) }}"
                                    data-method="DELETE"
                                    data-update="#template-list"
                                    data-confirm="{% trans title=tpl.title -%}
                                                      Are you sure you want to delete the template '{{ title }}'?
                                                  {%- endtrans %}">
                                {% endif %}
                        </button>
                    {%- endif %}
                </div>
                <div class="group">
                    {% if tpl.type.name == 'poster' and event %}
                        <button class="i-button icon-printer icon-only highlight text-color borderless"
                                title="{% trans %}Print poster for the event{% endtrans %}"
                                data-href="{{ url_for('event_management.poster_settings', event, template_id=tpl.id) }}"
                                data-title="{% trans %}Poster Printing Settings{% endtrans %}"
                                data-ajax-dialog>
                        </button>
                    {% elif event %}
                        {% block print_action scoped %}
                            {% set single_form = event.registration_forms|first if event.registration_forms|length == 1
                                                else tpl.registration_form %}
                            {% if single_form %}
                                {{ _render_badge_form_button(single_form, tpl, format='icon') }}
                            {% else %}
                                {% set print_title -%}
                                    {%- if tpl.is_ticket -%}
                                        {% trans %}Print tickets{% endtrans %}
                                    {%- else -%}
                                        {% trans %}Print badges{% endtrans %}
                                    {%- endif -%}
                                {%- endset %}
                                <button class="i-button icon-printer icon-only highlight text-color js-dropdown borderless
                                                        {{ 'disabled' if not event.registration_forms }}"
                                        title="{{ print_title if event.registration_forms
                                                    else _('There are no registration forms in this event') }}"
                                        data-toggle="dropdown"></button>
                                <ul class="i-dropdown">
                                    {% for regform in event.registration_forms %}
                                        <li>
                                            {{ _render_badge_form_button(regform, tpl, format='text', text=regform.title) }}
                                        </li>
                                    {% endfor %}
                                </ul>
                            {% endif %}
                        {% endblock %}
                    {% endif %}
                </div>
            </div>
        </td>
    </tr>
{% endmacro -%}

{% macro render_template_list(templates, target, linkable_forms, inherited_templates=[], event=none, default_ticket=none,
                              default_badge=none, not_deletable_templates=[]) -%}
    {% if inherited_templates %}
        <section>
            <h3>{% trans %}Inherited templates{% endtrans %}</h3>
            <table class="i-table-widget">
                <tbody>
                    {% for tpl in inherited_templates | sort(attribute='title') | sort(attribute='type') %}
                        {{ _render_template(tpl, target, linkable_forms, inherited=true, event=event,
                                            default_ticket=default_ticket, default_badge=default_badge,
                                            not_deletable_templates=not_deletable_templates) }}
                    {% endfor %}
                </tbody>
            </table>
        </section>
    {% endif %}

    <section class="custom-template-list {{ 'hide-if-locked' if not templates }}">
        <div class="flexrow f-a-center f-j-space-between">
            <h3>{% trans %}Custom templates{% endtrans %}</h3>
            <button class="i-button highlight icon-plus hide-if-locked"
                    data-href="{{ url_for('designer.add_template', target) }}"
                    data-title="{% trans %}Add a new blank designer template{% endtrans %}"
                    data-update="#template-list"
                    data-ajax-dialog>
                {% trans 'Custom template' %}Add new{% endtrans %}
            </button>
        </div>
        {% if templates %}
            <table class="i-table-widget">
                <tbody>
                    {% for tpl in templates | sort(attribute='title') | sort(attribute='type') %}
                        {{ _render_template(tpl, target, linkable_forms, event=event, default_ticket=default_ticket,
                                            default_badge=default_badge,
                                            not_deletable_templates=not_deletable_templates) }}
                    {% endfor %}
                </tbody>
            </table>
        {% else %}
            <div class="italic text-not-important">
                {% trans %}No templates{% endtrans %}
            </div>
        {% endif %}
    </section>
{% endmacro -%}
