{% extends 'layouts/admin.twig' %}

{% block content %}

    <ul class="breadcrumbs el">
        <li><a href="/admin/">Admin</a> /</li>
        <li class="end">{{ title }}</li>
    </ul>

    <h1>
        {{ title }}
    </h1>

    <p>
        Record of changes made through the CMS content manager. Use this to evidence
        who changed a page and when.
    </p>

    <form method="get" action="{{ route('admin-cms-audit-log') }}" class="el">
        <label>
            Action
            <select name="action">
                <option value="">All actions</option>
                {% for key, label in actions %}
                    <option value="{{ key }}" {{ filters.action == key ? 'selected' : '' }}>{{ label }}</option>
                {% endfor %}
            </select>
        </label>
        <label>
            User
            <select name="admin_user_id">
                <option value="">All users</option>
                {% for user in admin_users %}
                    <option value="{{ user.admin_user_id }}" {{ filters.admin_user_id == user.admin_user_id ? 'selected' : '' }}>
                        {{ user.admin_user_name ?: 'User #' ~ user.admin_user_id }}
                    </option>
                {% endfor %}
            </select>
        </label>
        <label>
            Page ID
            <input type="number" name="page_id" value="{{ filters.page_id }}" style="width: 90px;" />
        </label>
        <label>
            From
            <input type="date" name="date_from" value="{{ filters.date_from }}" />
        </label>
        <label>
            To
            <input type="date" name="date_to" value="{{ filters.date_to }}" />
        </label>
        <button type="submit" class="btn">Filter</button>
        <a href="{{ route('admin-cms-audit-log') }}">Reset</a>
    </form>

    {% if entries.count() > 0 %}
        <table class="listingTable">
            <thead>
                <tr>
                    <th>Date / Time</th>
                    <th>Action</th>
                    <th>Page</th>
                    <th>User</th>
                    <th>IP Address</th>
                    <th>Details</th>
                </tr>
            </thead>
            <tbody>
                {% for entry in entries %}
                    <tr>
                        <td>{{ entry.created_at|date('d/m/Y H:i:s') }}</td>
                        <td>{{ entry.action_label }}</td>
                        <td>
                            {% if entry.page_title %}
                                {{ entry.page_title }}
                            {% endif %}
                            {% if entry.page_id %}
                                <small>(ID {{ entry.page_id }})</small>
                            {% endif %}
                        </td>
                        <td>
                            {{ entry.admin_user_name }}
                            {% if entry.admin_user_id %}
                                <small>(ID {{ entry.admin_user_id }})</small>
                            {% endif %}
                        </td>
                        <td>{{ entry.ip_address }}</td>
                        <td>
                            {% set details = entry.details_array %}
                            {% if details.changes is defined and details.changes is not empty %}
                                <details>
                                    <summary>{{ details.changes|length }} field(s) changed</summary>
                                    <ul>
                                        {% for field, change in details.changes %}
                                            <li>
                                                <strong>{{ field }}</strong>:
                                                <del>{{ change.old|slice(0, 200) }}</del>
                                                &rarr;
                                                <ins>{{ change.new|slice(0, 200) }}</ins>
                                            </li>
                                        {% endfor %}
                                    </ul>
                                </details>
                            {% elseif details.old_value is defined or details.new_value is defined %}
                                <details>
                                    <summary>{{ details.field_name ?? 'Content field' }}</summary>
                                    <div>
                                        <del>{{ details.old_value|slice(0, 500) }}</del>
                                        &rarr;
                                        <ins>{{ details.new_value|slice(0, 500) }}</ins>
                                    </div>
                                </details>
                            {% elseif details.comment is defined and details.comment %}
                                Comment: {{ details.comment }}
                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>

        <div class="pagination el">
            {% if entries.previousPageUrl() %}
                <a href="{{ entries.previousPageUrl() }}">&laquo; Previous</a>
            {% endif %}
            <span>Page {{ entries.currentPage() }} of {{ entries.lastPage() }} ({{ entries.total() }} entries)</span>
            {% if entries.nextPageUrl() %}
                <a href="{{ entries.nextPageUrl() }}">Next &raquo;</a>
            {% endif %}
        </div>
    {% else %}
        <p>No audit entries found.</p>
    {% endif %}

{% endblock %}
