{% extends "base.html" %}

{% load promgen %}
{% load i18n %}

{% block title %}
Promgen / Rule / {{ rule.name }}
{% endblock %}

{% block content %}
<div class="page-header">
    <h1>Rule: {{ rule.name }}</h1>
</div>

{% breadcrumb rule  %}

<div class="panel panel-danger" v-cloak v-if="activeRuleAlerts.has('{{rule.name}}')" data-service="{{rule.name}}">
    <div class="panel-heading">
        <a @click="toggleComponent('alerts-service-{{rule.name|slugify}}')" class="btn btn-danger btn-sm" role="button">Alerts</a>
    </div>
    <table v-if="components['alerts-service-{{rule.name|slugify}}']" class="table table-bordered table-condensed">
        <tr v-for="alert in activeRuleAlerts.get('{{rule.name}}')">
            {% include 'promgen/alert_row.html' %}
        </tr>
    </table>
</div>

<div class="panel panel-primary">
    <div class="panel-heading">{{rule.name}}</div>
    <div class="panel-body">
{% if rule.description %}
        <div>
            <label>Description:</label>
            <p>{{rule.description}}</p>
        </div>
{% endif %}
        <pre v-pre>{{rule|rule_dict|pretty_yaml}}</pre>
    </div>
    <div class="panel-footer">
        <a href="{% url 'rule-edit' rule.pk %}" class="btn btn-warning btn-sm">{% trans "Edit Rule" %}</a>
        <a href="{% urlqs 'alert-list' alertname=rule.name %}" class="btn btn-info btn-sm">{% trans "Alert History" %}</a>
    </div>
</div>

{% if rule.parent %}
<div class="panel panel-default">
    <div class="panel-heading">Parent</div>
    <table class="table">
        <tr>
            <td>
                <a href="{{rule.parent.get_absolute_url}}">{{rule.parent.name}}</a>
            </td>
            <td>
                <code v-pre data-href="{% url 'rule-test' rule.parent.pk %}">
                    {{ rule.parent|rulemacro }}
                </code>
            </td>
        </tr>
    </table>
</div>
{% endif %}

{% if rule.overrides.count %}
<div class="panel panel-default">
    <div class="panel-heading">Child Rules</div>
    <table class="table">
        <tr>
            <th>Rule</th>
            <th>Clause</th>
            <th>For</th>
            <th>Owner</th>
        </tr>
        {% for r in rule.overrides.all %}
        <tr {% if not r.enabled %}class="promgen-disabled" {% endif %}>
            <td class="col-xs-2">
                <a href="{% url 'rule-detail' r.pk %}">{{ r.name }}</a>
                <ul>
                    {% for k,v in r.labels.items|dictsort:0 %}
                    <li class="label label-primary">{{k}}:{{v}}</li>
                    {% endfor %}
                </ul>
            </td>
            <td class="col-xs-8 promgen-clause">
                {{ r|rulemacro }}
            </td>
            <td>
                {{r.duration}}
            </td>
            <td class="col-xs-2"><a href="{{ r.content_object.get_absolute_url }}">{{ r.content_object.name }}</a></td>
        </tr>
        {% endfor %}
    </table>
</div>
{% endif %}

{% endblock %}
