## The contents of this file are subject to the Common Public Attribution
## License Version 1.0. (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License at
## http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
## License Version 1.1, but Sections 14 and 15 have been added to cover use of
## software over a computer network and provide for limited attribution for the
## Original Developer. In addition, Exhibit A has been modified to be
## consistent with Exhibit B.
##
## Software distributed under the License is distributed on an "AS IS" basis,
## WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
## the specific language governing rights and limitations under the License.
##
## The Original Code is reddit.
##
## The Original Developer is the Initial Developer.  The Initial Developer of
## the Original Code is reddit Inc.
##
## All portions of the code written by reddit are Copyright (c) 2006-2013
## reddit Inc. All Rights Reserved.
###############################################################################

<%namespace file="utils.html" import="error_field"/>
<% from r2.lib.template_helpers import static %>

<%def name="add_form(title, dest, add_type, container_name, verb=None)">
  <% from r2.models import ModeratorPermissionSet %>
  <% from r2.lib.pages import ModeratorPermissions %>
  <form action="/post/${dest}"
        method="post" class="pretty-form medium-text friend-add"
        onsubmit="return post_form(this, '${dest}')"
        id="${add_type}">
    <h1>${title}</h1>

    <input type="hidden" name="action" value="add">
    <input type="hidden" name="container" value="${container_name}">
    <input type="hidden" name="type" value="${add_type}">
    %if add_type in ("banned", "wikibanned"):
        <label for="name">${_('who to ban?')} &nbsp;</label>
        <input type="text" onfocus="$(this).parent('form').addClass('edited')" class="friend-name" name="name" id="name">
        <span class="ban-reason">
            <label for="note">${_('why the ban?')}</label>
            <input type="text" maxlength="300" name="note" id="note">
            <span>${_('(will not be visible to user)')}</span>
        </span>
    %else:
        <input type="text" name="name" id="name">
    %endif
    %if add_type == "moderator_invite":
      ${ModeratorPermissions(None, 'moderator',
                             ModeratorPermissionSet(all=True),
                             editable=True, embedded=True)}
      &#32;
      <span class="permissions-edit">
        (<a href="javascript:void(0)">${_('change')}</a>)
      </span>
    %endif
    <button class="btn" type="submit">${verb or _("add")}</button>
    <span class="status"></span>
    ${error_field("USER_DOESNT_EXIST", "name")}
    %if caller:
      ${caller.body()}
    %endif
  </form>
</%def>

<%def name="userlist(title, row_type, rows, headers=None)">
  <div class="${row_type}-table" style="${'display:none' if not rows else ''}">
    <h1>
      ${title}
    </h1>

    <table>
      %if headers:
        <tr>
        %for header in headers:
           <th>${header}</th>
        %endfor
        </tr>
      %endif
      %if rows:
        %for item in rows:
          ${item}
        %endfor
      %else:
        <tr><td></td></tr>
      %endif
    </table>
  </div>
</%def>

<div class="${thing._class} usertable">
  %if thing.addable:
    ${add_form(thing.form_title, thing.destination, thing.type, thing.container_name)}
  %endif

  ${userlist(thing.table_title, thing.type, thing.user_rows, thing.table_headers)}
</div>
