{% extends "base.html" %}
{% load promgen %}
{% load i18n %}
{% block content %}

<div class="page-header">
  <h1>{{ rule.content_type.name }}: {{ rule.content_object.name }}</h1>
</div>

{% include 'promgen/breadcrumb.html' with rule=rule active='Edit Rule' only %}

<div style="display:none" data-alertname="{{rule.name}}" class="panel panel-danger promgen-alert">
  <div class="panel-heading">Alerts</div>
  <table class="table table-bordered table-condensed"></table>
</div>

{% if rule.parent %}
<div class="panel panel-default">
  <div class="panel-heading">
    <button class="btn btn-default" type="button" data-toggle="collapse" data-target="#rule-parent" aria-expanded="false" aria-controls="collapseExample">
      Parent Rule
    </button>
    <a class="btn btn-default" href="{% url 'rule-edit' rule.parent.pk %}" />Edit Parent</a>
  </div>
  <div id="rule-parent" class="panel-body collapse">
    <pre>{% include "promgen/prometheus.rule" %}</pre>
    <input id="parent-clause" type="hidden" value="{{rule.parent.clause}}">
    <a
      class="btn btn-primary btn-sm"
      data-href="{% url 'rule-test' rule.parent.id %}"
      data-source="#parent-clause"
      data-target="#ajax-clause-check"
      >Test Parent</a>
  </div>
</div>
{% endif %}

{% if rule.overrides.count %}
  <div class="panel panel-default">
    <div class="panel-heading">
      <button class="btn btn-default" type="button" data-toggle="collapse" data-target="#rule-children" aria-expanded="false" aria-controls="collapseExample">
        Overridden By
      </button>
    </div>
    <table class="table collapse" id="rule-children">
  {% for r in rule.overrides.all %}
      <tr>
        <td class="col-xs-2"><a href="{{ r.content_object.get_absolute_url }}">{{ r.content_object }}</a></td>
        <td class="col-xs-2"><a href="{% url 'rule-edit' r.pk %}">{{ r.name }}</a></td>
        <td class="col-xs-8">
          <code data-href="{% url 'rule-test' r.id %}" data-source="self" data-target="#ajax-clause-check">
            {{ r.clause|rulemacro:r }}
          </code>
        </td>
      </tr>
  {% endfor %}
    </table>
  </div>
{% endif %}

<form action="{% url 'rule-edit' rule.pk %}" class="input-group" method="post">{% csrf_token %}
<div class="row">

  {% if form.non_field_errors  %}
  <div class="panel panel-danger">
    <div class="panel-heading">Errors</div>
      {% for error in form.non_field_errors %}
        <div class="panel-body">{{ error|linebreaks }}</div>
      {% endfor %}
  </div>
  {% endif %}

<div class="col-md-6">
{% include 'promgen/rule_form_block.html' %}
</div>

<div class="col-md-6">
  <div class="panel panel-primary">
    <div class="panel-heading">Labels - Control routing through AlertManager and Promgen</div>
    {{ label_set.management_form }}
    <table class="table">
        <tr>
          <th>Label</th>
          <th>Value</th>
          <th>Delete?</th>
        </tr>
      {% for label_form in label_set %}
        <tr>
          <td>{{ label_form.id }} {{ label_form.name }}</td>
          <td>{{ label_form.value }}</td>
          <td>{{ label_form.DELETE }}</td>
        </tr>
      {% endfor %}
    </table>
    <ul class="list-group">
      <li class="list-group-item">Examples</li>
      <li class="list-group-item">
        <code>{"severity":"major"}</code>
      </li>
    </ul>
  </div>

  <div class="panel panel-primary">
    <div class="panel-heading">Annotations - Provide extra details for notifications</div>
    {{ annotation_set.management_form }}
    <table class="table">
        <tr>
          <th>Annotation</th>
          <th>Value</th>
          <th>Delete?</th>
        </tr>
      {% for annotation_form in annotation_set %}
        <tr>
          <td>{{ annotation_form.id }} {{ annotation_form.name }}</td>
          <td>{{ annotation_form.value }}</td>
          <td>{{ annotation_form.DELETE }}</td>
        </tr>
      {% endfor %}
    </table>
    <ul class="list-group">
      <li class="list-group-item">Examples</li>
      <li class="list-group-item">
        <code>summary: High load on {% templatetag openvariable %} $labels.instance {% templatetag closevariable %}</pre></code>
      </li>
    </ul>
  </div>
</div>
</div>

<div class="panel panel-primary">
  <div class="panel-body">
    <button class="btn btn-primary">Save Rule</button>
    <a href="{% url 'audit-list'  %}?rule={{rule.id}}" class="btn btn-info">{% trans "Edit History" %}</a>
  </div>
</div>
</form>

<div class="panel panel-danger">
  <div class="panel-body">
    <form method="post" action="{% url 'rule-delete' rule.id %}" onsubmit="return confirm('{% trans "Delete this Rule?" %}')" style="display: inline">
      {% csrf_token %}
      <button class="btn btn-danger">{% trans "Delete Rule" %}</button>
    </form>
  </div>
</div>

{% endblock %}
