<div class="section">
  <h2>Topic permissions</h2>
  <div class="hider">
    <h3>Current topic permissions</h3>
    <% if (topic_permissions.length > 0) { %>
    <table class="list">
      <thead>
        <tr>
<% if (mode == 'vhost') { %>
          <th>User</th>
<% } else { %>
          <th>Virtual host</th>
<% } %>
          <th>Exchange</th>
          <th>Write regexp</th>
          <th>Read regexp</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
<%
for (var i = 0; i < topic_permissions.length; i++) {
    var permission = topic_permissions[i];
%>
           <tr<%= alt_rows(i)%>>
<% if (mode == 'vhost') { %>
             <td><%= link_user(permission.user) %></td>
<% } else { %>
             <td><%= link_vhost(permission.vhost) %></td>
<% } %>
             <td><%= fmt_exchange(permission.exchange) %></td>
             <td><%= fmt_string(permission.write) %></td>
             <td><%= fmt_string(permission.read) %></td>
             <td class="c">
               <form action="#/topic-permissions" method="delete" class="confirm">
                 <input type="hidden" name="username" value="<%= fmt_string(permission.user) %>"/>
                 <input type="hidden" name="vhost" value="<%= fmt_string(permission.vhost) %>"/>
                 <input type="hidden" name="exchange" value="<%= fmt_exchange_url(permission.exchange) %>"/>
                 <input type="submit" value="Clear"/>
               </form>
             </td>
           </tr>
           <% } %>
      </tbody>
    </table>
    <% } else { %>
      <p>... no topic permissions ...</p>
    <% } %>

<h3>Set topic permission</h3>
    <form action="#/topic-permissions" method="put">
      <table class="form">
        <tr>
<% if (mode == 'vhost') { %>
          <th>User</th>
          <td>
            <input type="hidden" name="vhost" value="<%= fmt_string(parent.name) %>"/>
            <select name="username">
              <% for (var i = 0; i < users.length; i++) { %>
              <option value="<%= fmt_string(users[i].name) %>"><%= fmt_string(users[i].name) %></option>
              <% } %>
            </select>
          </td>
<% } else { %>
          <th><label>Virtual Host:</label></th>
          <td>
            <input type="hidden" name="username" value="<%= fmt_string(parent.name) %>"/>
            <select name="vhost" class="list-exchanges">
              <% for (var i = 0; i < vhosts.length; i++) { %>
              <option value="<%= fmt_string(vhosts[i].name) %>"><%= fmt_string(vhosts[i].name) %></option>
              <% } %>
            </select>
          </td>
<% } %>
        </tr>
        <tr>
          <th><label>Exchange:</label></th>
          <td>
          <div id='list-exchanges'>
            <%= format('list-exchanges', {'exchanges': exchanges}) %>
          </div>
          </td>
        </tr>
        <tr>
          <th><label>Write regexp:</label></th>
          <td><input type="text" name="write" value=".*"/></td>
        </tr>
        <tr>
          <th><label>Read regexp:</label></th>
          <td><input type="text" name="read" value=".*"/></td>
        </tr>
      </table>
      <input type="submit" value="Set topic permission"/>
    </form>
  </div>
</div>
