<h1>Policies</h1>
<div class="section">
  <h2>User policies</h2>
  <div class="hider">
<%= filter_ui(policies) %>
  <div class="updatable">
<% if (policies.length > 0) { %>
<table class="list">
 <thead>
  <tr>
<% if (display.vhosts) { %>
    <th>Virtual Host</th>
<% } %>
    <th>Name</th>
    <th>Pattern</th>
    <th>Apply to</th>
    <th>Definition</th>
    <th>Priority</th>
  </tr>
 </thead>
 <tbody>
<%
 for (var i = 0; i < policies.length; i++) {
    var policy = policies[i];
%>
   <tr<%= alt_rows(i)%>>
<% if (display.vhosts) { %>
     <td><%= fmt_string(policy.vhost) %></td>
<% } %>
<% if (ac.isPolicyMakerUser()) { %>
     <td><%= link_policy(policy.vhost, policy.name) %></td>
<% } else { %>
     <td><%= fmt_string(policy.name) %></td>
<% } %>
     <td><%= fmt_string(policy.pattern) %></td>
     <td><%= fmt_string(policy['apply-to']) %></td>
     <td><%= fmt_table_short(policy.definition) %></td>
     <td><%= fmt_string(policy.priority) %></td>
   </tr>
<% } %>
 </tbody>
</table>
<% } else { %>
  <p>... no policies ...</p>
<% } %>
  </div>
  </div>
</div>

<% if (ac.isPolicyMakerUser() && ac.canAccessVhosts()) { %>

<div class="section-hidden">
  <h2>Add / update a policy</h2>
  <div class="hider">
    <form action="#/policies" method="put">
      <table class="form">
<% if (display.vhosts) { %>
        <tr>
          <th><label>Virtual host:</label></th>
          <td>
            <select name="vhost">
              <% for (var i = 0; i < vhosts.length; i++) { %>
              <option value="<%= fmt_string(vhosts[i].name) %>" <%= (vhosts[i].name === current_vhost) ? 'selected="selected"' : '' %>><%= fmt_string(vhosts[i].name) %></option>
              <% } %>
            </select>
          </td>
        </tr>
<% } else { %>
        <tr><td><input type="hidden" name="vhost" value="<%= fmt_string(vhosts[0].name) %>"/></td></tr>
<% } %>
        <tr>
          <th><label>Name:</label></th>
          <td><input type="text" name="name"/><span class="mand">*</span></td>
        </tr>
        <tr>
          <th><label>Pattern:</label></th>
          <td><input type="text" name="pattern"/><span class="mand">*</span></td>
        </tr>
        <tr>
          <th><label>Apply to:</label></th>
          <td>
            <select name="apply-to">
              <option value="all">Exchanges and queues</option>
              <option value="exchanges">Exchanges</option>
              <option value="queues">Queues</option>
              <% for (const [typename, type_config] of Object.entries(QUEUE_TYPE)) { %>
                  <% if (typename != "default" && type_config.policy_apply_to ) { %>
              <option value="<%= type_config.policy_apply_to %>"><%= type_config.label %> Queues</option>
                  <% } %>
              <% } %>
            </select>
          </td>
        </tr>
        <tr>
          <th><label>Priority:</label></th>
          <td><input type="text" name="priority"/></td>
        </tr>
        <tr>
          <th><label>Definition:</label></th>
          <td>
            <div class="multifield" id="definition"></div>
            <table class="argument-links">
              <tr>
                  <td>Queues [All types]</td>
                <td>
                  <span class="argument-link" field="definition" key="max-length" type="number">Max length</span> |
                  <span class="argument-link" field="definition" key="max-length-bytes" type="number">Max length bytes</span> |
                  <span class="argument-link" field="definition" key="overflow" type="string">Overflow behaviour</span> <span class="help" id="queue-overflow"></span> |
                  <span class="argument-link" field="definition" key="expires" type="number">Auto expire</span> </br>
                  <span class="argument-link" field="definition" key="dead-letter-exchange" type="string">Dead letter exchange</span> |
                  <span class="argument-link" field="definition" key="dead-letter-routing-key" type="string">Dead letter routing key</span><br/>
                  <span class="argument-link" field="definition" key="message-ttl" type="number">Message TTL</span><span class="help" id="queue-message-ttl"></span> |
                  <span class="argument-link" field="definition" key="consumer-timeout" type="number">Consumer Timeout</span><span class="help" id="queue-consumer-timeout"></span> |
                  <span class="argument-link" field="definition" key="queue-leader-locator" type="string">Leader locator</span><span class="help" id="queue-leader-locator"></span></br>
                </td>
                <% for (const [typename, type_config] of Object.entries(QUEUE_TYPE)) { %>
                    <% if (typename != "default" && type_config.tmpl.user_policy_arguments) { %>
                        <%= format(type_config.tmpl.user_policy_arguments, {}) %>
                  <% } %>
                <% } %>
              <tr>
                <td>Streams</td>
                <td>
                  <span class="argument-link" field="definition" key="max-age" type="string">Max age</span>
                  <span class="help" id="queue-max-age"></span> |
                  <span class="argument-link" field="definition" key="stream-filter-size-bytes" type="number">Filter size in bytes. Valid range: 16-255</span>
                  <span class="help" id="queue-stream-filter-size-bytes"></span> |
                </td>
              </tr>
              <tr>
                <td>Exchanges</td>
                <td>
                  <span class="argument-link" field="definition" key="alternate-exchange" type="string">Alternate exchange</span>
                  <span class="help" id="exchange-alternate"></span>
                </td>
              </tr>
              <tr>
                <td>Federation</td>
                <td>
                  <span class="argument-link" field="definition" key="federation-upstream-set" type="string">Federation upstream set</span> <span class="help" id="policy-federation-upstream-set"></span> |
                  <span class="argument-link" field="definition" key="federation-upstream" type="string">Federation upstream</span>
                  <span class="help" id="policy-federation-upstream"></span>
                </td>
              </tr>
            </table>
          </td>
          <td class="t"><span class="mand">*</span></td>
        </tr>
      </table>
      <input type="submit" value="Add / update policy"/>
    </form>
  </div>
</div>
<% } %>


<div class="section">
  <h2>Operator policies</h2>
  <div class="hider">
<%= filter_ui(operator_policies) %>
  <div class="updatable">
<% if (operator_policies.length > 0) { %>
<table class="list">
 <thead>
  <tr>
<% if (display.vhosts) { %>
    <th>Virtual Host</th>
<% } %>
    <th>Name</th>
    <th>Pattern</th>
    <th>Apply to</th>
    <th>Definition</th>
    <th>Priority</th>
<% if (ac.isAdministratorUser() && is_op_policy_updating_enabled) { %>
    <th class="administrator-only">Clear</th>
<% } %>
  </tr>
 </thead>
 <tbody>
<%
 for (var i = 0; i < operator_policies.length; i++) {
    var policy = operator_policies[i];
%>
   <tr<%= alt_rows(i)%>>
<% if (display.vhosts) { %>
     <td><%= fmt_string(policy.vhost) %></td>
<% } %>
     <td><%= fmt_string(policy.name) %></td>
     <td><%= fmt_string(policy.pattern) %></td>
     <td><%= fmt_string(policy['apply-to']) %></td>
     <td><%= fmt_table_short(policy.definition) %></td>
     <td><%= fmt_string(policy.priority) %></td>
<% if (ac.isAdministratorUser && is_op_policy_updating_enabled) { %>
     <td class="administrator-only">
        <form action="#/operator_policies" method="delete" class="confirm">
            <input type="hidden" name="name" value="<%= fmt_string(policy.name) %>"/>
            <input type="hidden" name="vhost" value="<%= fmt_string(policy.vhost) %>"/>
            <input type="submit" value="Clear"/>
        </form>
      </td>
<% } %>
   </tr>
<% } %>
 </tbody>
</table>
<% } else { %>
  <p>... no policies ...</p>
<% } %>
  </div>
  </div>
</div>

<% if (ac.isAdministratorUser() && ac.canAccessVhosts() > 0 && is_op_policy_updating_enabled) { %>

<div class="section-hidden">
  <h2>Add / update an operator policy</h2>
  <div class="hider">
    <form action="#/operator_policies" method="put">
      <table class="form">
<% if (display.vhosts) { %>
        <tr>
          <th><label>Virtual host:</label></th>
          <td>
            <select name="vhost">
              <% for (var i = 0; i < vhosts.length; i++) { %>
              <option value="<%= fmt_string(vhosts[i].name) %>"><%= fmt_string(vhosts[i].name) %></option>
              <% } %>
            </select>
          </td>
        </tr>
<% } else { %>
        <tr><td><input type="hidden" name="vhost" value="<%= fmt_string(vhosts[0].name) %>"/></td></tr>
<% } %>
        <tr>
          <th><label>Name:</label></th>
          <td><input type="text" name="name"/><span class="mand">*</span></td>
        </tr>
        <tr>
          <th><label>Pattern:</label></th>
          <td><input type="text" name="pattern"/><span class="mand">*</span></td>
        </tr>
        <tr>
          <th><label>Apply to:</label></th>
          <td>
            <select name="apply-to">
              <option value="queues">Queues</option>
              <% for (const [typename, type_config] of Object.entries(QUEUE_TYPE)) { %>
                  <% if (typename != "default"  && type_config.policy_apply_to  ) { %>
                      <option value="<%= type_config.policy_apply_to %>"><%= type_config.label %> Queues</option>
                  <% } %>
              <% } %>
            </select>
          </td>
        </tr>
        <tr>
          <th><label>Priority:</label></th>
          <td><input type="text" name="priority"/></td>
        </tr>
        <tr>
          <th><label>Definition:</label></th>
          <td>
            <div class="multifield" id="definitionop"></div>
            <table class="argument-links">
                <% for (const [typename, type_config] of Object.entries(QUEUE_TYPE)) { %>
                    <% if (typename != "default" && type_config.tmpl.operator_policy_arguments) { %>
                        <%= format(type_config.tmpl.operator_policy_arguments, {}) %>
                    <% } %>
                <% } %>
            </table>
          </td>
          <td class="t"><span class="mand">*</span></td>
        </tr>
      </table>
      <input type="submit" value="Add / update operator policy"/>
    </form>
  </div>
</div>
<% } %>
