<%inherit file="/template.html" />
<%namespace name="components" file="/components.html" />
<%namespace name="forms" file="/forms.html"/>
<%namespace name="badge_tiles" file="/badge/tiles.html" />
<%def name="title()">${c.form_title}</%def>

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

<%block name="main_content">

${self.form()}
</%block>

<%def name="form()">
  <% h.need.spectrum %>
  <form name="update_badge" action="${h.site.current_url()}" class="inplace" method="POST" accept-charset="utf-8" enctype="multipart/form-data">

    ${components.flashmessages()}

    ${h.field_token()|n}
    <input type="hidden" name="came_from" value="${c.came_from}" />

    <label for="badge_form_title" class="">${_("Category title") if c.badge_type == 'category' else _("Badge title")}</label>
    <input name="title" id="badge_form_title" class="" style="width:20em"
           value="" autofocus />

    <%forms:textarea label="${_('Category description') if c.badge_type == 'category' else _('Badge description')}"
                     name="description" rows="8" value="">
    </%forms:textarea>

    %if c.badge_type == 'category':
    %if c.instance is not None and c.instance.display_category_pages:
    <%forms:textarea label="${_('Long category description')}" name="long_description" rows="10" value=""
                     help="${_('Here you can add a more detailed description of your category with markdown.')}">
    </%forms:textarea>
    ${components.formatting()}
    %else:
    <input type="hidden" name="long_description" />
    %endif
    %endif

    <label for="color"
           class="">${_("The color that is used to show the category") if c.badge_type == 'category' else _("The color that is used to show the badge")}</label>
    <input name="color" style="width: 8em" id="color" class=""
           value=""/>
    <span class="hint only-no-js">${_("Like '#a4a4a4'")}</span>

    %if c.badge_type != 'thumbnail':
    <span id="badge-preview" class="hidden">
        ${_('Preview:')} &nbsp;&nbsp; ${badge_tiles.badge('dummy')}
    </span>
    %endif
    <script>
        $(document).ready(function() {
            adhocracy.helpers.initializeBadgeColorPicker('#color', '#badge_form_visible', '#badge_form_title', '${h.site.domain()}.colors.badge')})
    </script>

    %if c.badge_type == 'thumbnail' or (c.badge_type == 'category' and c.instance is not None and c.instance.display_category_pages):
    <fieldset class="logo">
        <div class="alert error only-no-formdata">
            ${_('Image upload does not work in your browser. Please update.')}
        </div>
        <%
            if c.badge_type == 'thumbnail':
                label = _('Image')
                name = 'thumbnail'
                help = _('Upload image. This is used instead of color to show the badge')
                delete_name = 'delete_thumbnail'
                delete_value = _('Delete current thumbnail image')
            elif c.badge_type == 'category':
                label = _('Image')
                name = 'image'
                help = _('Upload category image')
                delete_name = 'delete_image'
                delete_value = _('Delete current category image')
        %>
        <%forms:input label="${label}" name="${name}" type="file"
                      help="${help}">
        </%forms:input>
        %if c.logo:
        <div class="current">
            ${h.literal(c.logo)}
            <br />
            <input type="submit" name="${delete_name}" value="${delete_value}" />
        </div>
        %endif
    </fieldset>
    %endif

    <input type="checkbox" name="visible" id="badge_form_visible" />
    <label for="badge_form_visible" style="display: inline;" title="${_("Check to hide category in user interface") if c.badge_type == 'category' else _('Check to hide badge in user interface')}">${_("visible")}</label>

    %if c.badge_type == 'user':
    <label for="group" class="">
        ${_("Group")}
    </label>

    <select name="group">
        <option value="">${_("No Group")}</option>
        %for group in c.groups:
        <option value="${group.code}">${_(group.group_name)}</option>
        %endfor
    </select>
    %if h.has_permission('global.admin'):
    <span class="hint">${_("Automatically assign all users with this badge to the selected group for all instances in the adhocracy site.")}</span>
    %else:
    <span class="hint">${_("Automatically assign all users with this badge to the selected group.")}</span>
    %endif

    <label for="display_group"
           class="">${_("Display group in instances")}</label>
    <input type="checkbox"  name="display_group" id="display_group" />
    <span class="hint">${_("Display this badge instead of the normal user group.")}</span>
    %endif

    %if h.has_permission('global.admin'):
    <label for="instance" class="">
        ${_('Instance')}
    </label>
    <select name="instance">
        <option value="">${_('Global')}</option>
        %if c.instance is not None:
        <option value="${c.instance.key}">${c.instance.label}</option>
        %endif
    </select>
    %else:
    <input type="hidden" name="instance" value="${c.instance.key}" />
    %endif

    %if c.badge_type == 'category':
    <label for="parent" class="">${_("Parent Category")}</label>
    <div class="help-block">${_('Link this category to a superordinate category')}</div>
    <select name="parent">
        <option value="">${_('No Parent Category')}</option>
        %if h.has_permission('global.admin'):
        <optgroup label="${_('Global categories')}">
        %for (id, title) in c.global_category_parents:
        <option value="${id}">${title}</option>
        %endfor
        </optgroup>
        %if c.instance:
        <optgroup label="${_('Local instance categories')}">
        %endif
        %endif
        %for (id, title) in c.local_category_parents:
        <option value="${id}">${title}</option>
        %endfor
        %if h.has_permission('global.admin'):
        </optgroup>
        %endif
    </select>

    <label for="select_child_description" class="">${_("Description to select children of this category (irrelevant if this category doesn't have any children)")}</label>
    <input name="select_child_description" id="badge_select_child_description" class="" style="width:20em"
           value="" />
    %endif

    %if c.badge_type == 'user':
    %if lib.behavior.behavior_enabled():
    <h3>Behaviors</h3>
    <label>${('Default proposal sort order:')}
        ${components.proposal_sort_order('behavior_proposal_sort_order', c.sorting_orders, include_empty=True)}
    </label>
    %endif
    %endif

    %if c.badge_type in ['delegateable', 'category', 'thumbnail']:
    ${forms.input(_(u'Impact'), 'impact', type='number', min=-10, max=10)}
    %endif

    <div class="mainbar">
      ${components.savebox(c.came_from, _('Save'))}
    </div>
  </form>

</%def>
