{% extends "admin_base.html" %}
{% block extra_head %}
{{ super() }}
<link rel="stylesheet" href="{{ static_url('/static/css/audit.css') }}">
{% endblock %}
{% block admin_content %}
<a href="/admin/audit-log" class="audit-back-link"><span class="icon">&#x2190;</span> Back to audit log</a>

<div class="admin-toolbar">
    <h2><span class="audit-badge">{{ event['event_key'] }}</span></h2>
    <span class="audit-result audit-result-{{ event.get('result', 'success') }}">{{ event.get('result', 'success') }}</span>
</div>

<dl class="audit-detail-grid">
    <dt>Time</dt>
    <dd>{{ local_dt(event['created_at']) }} ({{ dt_ago(event['created_at']) if event['created_at'] else event.get('time_ago', '') }})</dd>

    <dt>Category</dt>
    <dd>{{ event.get('category', '') }}</dd>

    <dt>Actor</dt>
    <dd>
        {% if event.get('actor_user') %}
            {% set _user = event['actor_user'] %}{% set _size = 24 %}{% set _size_class = "sm" %}
            <span class="admin-user-cell">{% include "_avatar_link.html" %}{% include "_user_link.html" %}</span>
        {% else %}
            {{ event.get('actor_username') or event.get('actor_kind', 'system') }}
        {% endif %}
    </dd>

    <dt>Role</dt>
    <dd>{{ event.get('actor_role', '') }}</dd>

    <dt>Origin</dt>
    <dd>{{ event.get('origin', '') }}{% if event.get('via_agent') %} <span class="audit-badge audit-badge-devii">via Devii agent</span>{% endif %}</dd>

    {% if event.get('request_method') or event.get('request_path') %}
    <dt>Request</dt>
    <dd>{{ event.get('request_method', '') }} {{ event.get('request_path', '') }}</dd>
    {% endif %}

    {% if event.get('actor_ip') %}
    <dt>IP</dt>
    <dd><span class="audit-actor-ip">{{ event['actor_ip'] }}</span></dd>
    {% endif %}

    {% if event.get('user_agent') %}
    <dt>User agent</dt>
    <dd>{{ event['user_agent'] }}</dd>
    {% endif %}

    {% if event.get('target_type') or event.get('target_uid') %}
    <dt>Target</dt>
    <dd>{{ event.get('target_label') or '' }} <span class="admin-text-muted">{{ event.get('target_type', '') }} {{ event.get('target_uid', '') }}</span></dd>
    {% endif %}

    {% if event.get('old_value') is not none or event.get('new_value') is not none %}
    <dt>Change</dt>
    <dd class="audit-value-change">
        {% if event.get('old_value') is not none %}<span class="audit-value-old">{{ event['old_value'] }}</span> &#x2192; {% endif %}
        {% if event.get('new_value') is not none %}<span class="audit-value-new">{{ event['new_value'] }}</span>{% endif %}
    </dd>
    {% endif %}

    {% if event.get('summary') %}
    <dt>Summary</dt>
    <dd>{{ event['summary'] }}</dd>
    {% endif %}
</dl>

{% if event.get('metadata_pretty') %}
<h3 class="audit-section-title">Metadata</h3>
<div class="audit-meta">
    <pre>{{ event['metadata_pretty'] }}</pre>
</div>
{% endif %}

<h3 class="audit-section-title">Related objects</h3>
{% if links %}
<div class="admin-table-wrap">
    <table class="admin-table">
        <caption class="sr-only">Objects related to this audit event</caption>
        <thead>
            <tr>
                <th scope="col">Relation</th>
                <th scope="col">Type</th>
                <th scope="col">Label</th>
                <th scope="col">UID</th>
            </tr>
        </thead>
        <tbody>
            {% for l in links %}
            <tr>
                <td><span class="audit-badge">{{ l['relation'] }}</span></td>
                <td>{{ l['object_type'] }}</td>
                <td>{{ l.get('object_label') or '' }}</td>
                <td class="admin-text-muted">{{ l['object_uid'] }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% else %}
<p class="admin-text-muted">No related objects linked.</p>
{% endif %}
{% endblock %}
