<%inherit file="/template.html" />
<%namespace name="tiles" file="/badge/tiles.html"/>
<%namespace name="components" file="/components.html"/>
<%def name="title()">${_("Badges")}</%def>

<%def name="breadcrumbs()">
${h.badge.breadcrumbs(None)|n}
</%def>


<%def name="edit_button(base_url, badge_id, is_global)">
%if (is_global and can.badge.edit_global()) or (not is_global and can.badge.edit_instance()):
<a href="${base_url}/edit/${badge_id}${'?came_from=' + h.url.quote(c.came_from) if c.came_from else ''}" target="_self">${_('Edit')}</a>
%endif
</%def>


<%def name="delete_button(base_url, badge_id, is_global)">
%if (is_global and can.badge.manage_global()) or (not is_global and can.badge.manage_instance()):
<a href="${base_url}/delete/${badge_id}${'?came_from=' + h.url.quote(c.came_from) if c.came_from else ''}" target="_self">${_('Delete')}</a>
%endif
</%def>


<%def name="new_button(base_url, type_, is_global)">
%if (is_global and can.badge.manage_global()) or (not is_global and can.badge.manage_instance()):
<a class="button_small plus"
    href="${'%s/%s/add?global=%s' % (base_url, type_, str(is_global))}${'&came_from=' + h.url.quote(c.came_from) if c.came_from else ''}" target="_self">${_("New")}</a>
%endif
</%def>


<%def name="render_table(badges, base_url, type_, show_header, is_global)">
%if badges is not None:
<%
table_def = {
    'user': user_badges_table,
    'delegateable': delegateable_badges_table,
    'category': category_badges_table,
    'thumbnail': thumbnail_badges_table,
    'instance': instance_badges_table,
    }.get(type_)
%>
%if show_header:
<h5>${_(u'In all instances') if is_global else _(u'In instance "%s"') % c.instance.label}${new_button(c.badge_base_url, type_, is_global)}</h5>
%endif
%if badges:
${table_def(base_url, badges)}
%else:
<p>${_("No badges added yet.")}</p>
%endif
%endif
</%def>


<%def name="render_context_tables()">
${render_tables(c.global_badges, c.instance_badges, c.badge_base_url, c.badge_header, c.badge_type)}
</%def>


<%def name="render_tables(global_badges, instance_badges, base_url, title, type_)">
<% single_table = global_badges is None or instance_badges is None %>
<h4>${title}
  %if single_table:
  %if global_badges is not None:
  ${new_button(base_url, type_, True)}
  %elif instance_badges is not None:
  ${new_button(base_url, type_, False)}
  %endif
  %endif
</h4>
%if type_ == 'user':
<p>${_("User badges allow you to organize your members according to certain groups.")}</p>
%elif type_ == 'delegateable':
<p>${_("Proposal badges may be used to link different attributes to a proposal (e.g. official, first phase, etc.).")}</p>
%elif type_ == 'category':
<p>${_("Add new categories to allow your users to categorize their proposals according to a topic.")}</p>
%elif type_ == 'thumbnail':
<p>${_("Status badges may be used to indicate different statuses of proposals in your participation process (e.g. accepted, rejected, in discussion, etc.) with an image.")}</p>
%endif
${render_table(global_badges, base_url, type_, not single_table, True)}
${render_table(instance_badges, base_url, type_, not single_table, False)}
</%def>


<%def name="user_badges_table(base_url, badge_items)">
<table>
    <thead>
        <th>${_("Title")}</th>
        <th>${_("Description")}</th>
        <th>${_("Preview")}</th>
        <th>${_("Assigned Group")}</th>
        <th>${_("Display")}</th>
        <th>${_("Actions")}</th>
    </thead>
    %for badge in badge_items:
    <tr class="tile">
        <td>${badge.title}</td>
        <td>${badge.description and (badge.description[:20] + u'...') or ''}</td>
        <td>${tiles.badge(badge)}</td>
        <td>${badge.group and _(badge.group.group_name) or ""}</td>
        <td>${badge.display_group and _("yes") or _("no")}</td>
        <td>${edit_button(base_url, badge.id, badge.instance is None)}
            ${delete_button(base_url, badge.id, badge.instance is None)}</td>
    </tr>
    %endfor
</table>
</%def>


<%def name="delegateable_badges_table(base_url, badge_items)">
<table>
    <thead>
        <th>${_("Title")}</th>
        <th>${_("Description")}</th>
        <th>${_("Preview")}</th>
        <th>${_("Impact")}</th>
        <th>${_("Actions")}</th>
    </thead>
    %for badge in badge_items:
    <tr class="tile">
        <td>${badge.title}</td>
        <td>${badge.description and (badge.description[:20] + u'...') or ''}</td>
        <td>${tiles.badge(badge)}</td>
        <td>${badge.impact}</td>
        <td>${edit_button(base_url, badge.id, badge.instance is None)}
            ${delete_button(base_url, badge.id, badge.instance is None)}</td>
    </tr>
    %endfor
</table>
</%def>


<%def name="instance_badges_table(base_url, badge_items)">
<table>
    <thead>
        <th>${_("Title")}</th>
        <th>${_("Description")}</th>
        <th>${_("Preview")}</th>
        <th>${_("Actions")}</th>
    </thead>
    %for badge in badge_items:
    <tr class="tile">
        <td>${badge.title}</td>
        <td>${badge.description and (badge.description[:20] + u'...') or ''}</td>
        <td>${tiles.badge(badge)}</td>
        <td>${edit_button(base_url, badge.id, badge.instance is None)}
            ${delete_button(base_url, badge.id, badge.instance is None)}</td>
    </tr>
    %endfor
</table>
</%def>


<%def name="category_badges_table(base_url, badge_items)">
<table>
    <thead>
        <th>${_("Title")}</th>
        <th>${_("Description")}</th>
        <th>${_("Preview")}</th>
        <th>${_("Impact")}</th>
        <th>${_("Parent")}</th>
        <th>${_("Actions")}</th>
    </thead>
    %for badge in badge_items:
    <tr class="tile">
        <td>${badge.title}</td>
        <td>${badge.description and (badge.description[:20] + u'...') or ''}</td>
        <td>${tiles.badge(badge)}</td>
        <td>${badge.impact}</td>
        <td>${badge.parent.title if badge.parent else ""}</td>
        <td>${edit_button(base_url, badge.id, badge.instance is None)}
            ${delete_button(base_url, badge.id, badge.instance is None)}</td>
    </tr>
    %endfor
</table>
</%def>


<%def name="thumbnail_badges_table(base_url, badge_items)">
<table>
    <thead>
        <th>${_("Title")}</th>
        <th>${_("Description")}</th>
        <th>${_("Preview")}</th>
        <th>${_("Impact")}</th>
        <th>${_("Actions")}</th>
    </thead>
    %for badge in badge_items:
    <tr class="tile">
        <td>${badge.title}</td>
        <td>${(badge.description[:20] + u'...' if len(badge.description) > 20 else badge.description) if badge.description else u''}</td>
        <td>${tiles.badge(badge)}</td>
        <td>${badge.impact}</td>
        <td>${edit_button(base_url, badge.id, badge.instance is None)}
            ${delete_button(base_url, badge.id, badge.instance is None)}</td>
    </tr>
    %endfor
</table>
</%def>


<%block name="main_content">
${components.flashmessages()}
${render_context_tables()}
</%block>
