<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">

  <xi:include href="master.html" />
  <xi:include href="lib.html" />

  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>
    <title>Project Permissions for $c.project.shortname</title>

    <py:def function="role_select(name)">
      <select name="$name">
        <option py:for="r in c.project.roles"
                value="$r._id">${r.display()}</option>
      </select>
    </py:def>
    <link rel="stylesheet" type="text/css" media="screen" href="${g.app_static('css/admin.css')}"/>
  </head>

  <body>
    <h1 class="title">Project Permissions for $c.project.shortname</h1>
    <div py:if="c.project.deleted" class="notice">This project has been deleted and is not visible to non-admin users</div>
    <form method="POST" action="update_user_roles" id="roles_form">
      <input py:for="i,r in enumerate(c.project.roles)" py:if="r.name in ['Developer','Admin']"
        type="hidden"
        name="role-${i}.id"
        value="$r._id"/>
      <table>
        <thead>
          <tr>
            <th>Role</th>
            <th>Users</th>
            <th>Add User</th>
            <th/>
          </tr>
        </thead>
        <tbody>
          <tr py:for="i, r in enumerate(c.project.roles)" py:if="r.name in ['Developer','Admin']" class="${i%2 and 'even' or ''}">
            <td>${r.display()}</td>
            <td>
              <span py:for="j, u in enumerate(r.users_with_role())" class="removable">
                ${u.display_name}
                <input type="hidden" name="role-${i}.users-${j}.id" value="$u._id"/>
              </span>
            </td>
            <td class="add_role">
              ${c.user_select.display(name="role-%s.new.id" % i,value='')}
            </td>
          </tr>
        </tbody>
      </table>
      <input type="submit" value="Save Changes"/>
    </form>
  </body>
    <script type="text/javascript">
      /*<![CDATA[*/
      $('span.removable').click(function(e){
        var vals = $('#roles_form').serialize();
        var del_name = $('input', this)[0].name.replace('.id','.delete');
        $.post($('#roles_form')[0].action, vals+'&'+del_name+'=Del', function(){
          e.target.parentNode.removeChild(e.target);
        });
      });
      $('#roles_form').submit(function(e){
        var vals = $(this).serialize();
        $('td.add_role input', this).each(function(){
          if(this.value){
            vals = vals+'&'+this.name.replace('.id','.add')+'=Add'
          }
        });
        $.post(this.action, vals, function(){
          window.location.reload();
        });
        return false;
      });
      /*]]>*/
    </script>

</html>
