{% extends "freeform/_layouts/settings" %}
{% hook "freeform.prepareCpTemplate" %}

{% set selectedSubnavItem = "errors" %}

{% set segment3 = craft.app.request.getSegment(3) %}
{% set category = category|default('') %}

{% set perPage = 100 %}
{% set currentPage = craft.app.request.queryParam('page')|default(1) %}

{% set crumbs = [
    { label: craft.freeform.name, url: url("freeform") },
    { label: "Settings"|t("freeform"), url: url("freeform/settings") },
    { label: "Logs"|t("freeform"), url: url("freeform/settings/" ~ segment3) },
] %}

{% set title = segment3|title|replace({'-': " "})|t('freeform') %}

{% block actionButton %}

    <div class="buttons">
        <a href="{{ url(('freeform/logs/clear/'~category)|trim('/')) }}" class="btn submit icon clear-logs">
            {{ "Clear"|t('freeform') }}
        </a>
    </div>

{% endblock %}

{% macro pager(reader, perPage, currentPage, cat) %}
    {% set pages = (reader.count / perPage)|round(0, 'ceil') %}

    {% set isFirst = currentPage <= 1 %}
    {% set isLast = currentPage >= pages %}

    {% if pages > 1 %}
        <div class="paginator" data-page="{{ currentPage }}" data-total="{{ pages }}" data-base-url="{{ "freeform/settings/" ~ cat }}">
            <div class="group">
                <a class="btn small" data-prev data-max {{ isFirst ? 'disabled' }}>
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--!Font Awesome Pro 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2025 Fonticons, Inc.--><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l192 192c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 278.6 86.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-192 192zm384-192l-192 192c-12.5 12.5-12.5 32.8 0 45.3l192 192c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L301.3 256 470.6 86.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0z"/></svg>
                </a>
                <a class="btn small" data-prev {{ isFirst ? 'disabled' }}>
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path d="M9.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l192 192c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L77.3 256 246.6 86.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-192 192z"/></svg>
                </a>
            </div>

            <div class="counter">
                {{ "Page {current} of {total}"|t('freeform', { current: currentPage, total: pages }) }}
            </div>

            <div class="group">
                <a class="btn small reverse" data-next {{ isLast ? 'disabled' }}>
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path d="M9.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l192 192c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L77.3 256 246.6 86.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-192 192z"/></svg>
                </a>
                <a class="btn small reverse" data-next data-max {{ isLast ? 'disabled' }}>
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--!Font Awesome Pro 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2025 Fonticons, Inc.--><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l192 192c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 278.6 86.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-192 192zm384-192l-192 192c-12.5 12.5-12.5 32.8 0 45.3l192 192c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L301.3 256 470.6 86.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0z"/></svg>
                </a>
            </div>
        </div>
    {% endif %}
{% endmacro %}

{% block content %}

    <div class="paginator-wrapper upper">
        {{ _self.pager(logReader, perPage, currentPage, segment3) }}
    </div>

    <div class="container">
        {% if logReader.count %}
            <table class="data fullwidth log-table">
                <thead>
                <tr>
                    <th></th>
                    <th>{{ "Type"|t('freeform') }}</th>
                    <th>{{ "Message"|t('freeform') }}</th>
                </tr>
                </thead>

                <tbody>
                {% for line in logReader.lines(perPage, perPage * (currentPage - 1)) %}
                    <tr>
                        <td class="log-level log-level-{{ line.level|lower }}">
                        </td>
                        <td class="nowrap log-meta">
                            <div data-level class="badge badge-level-{{ line.level|lower }}">
                                {{ line.level|capitalize|t('freeform') }}
                            </div>
                            <div data-channel class="code">
                                {{ line.channel }}
                            </div>

                            <div data-date class="code">
                                {{ line.date.format('Y-m-d H:i:s') }}
                            </div>

                            {% if line.extra.requestId is defined %}
                                <div data-request-id class="code">
                                    #{{ line.extra.requestId }}
                                </div>
                            {% endif %}
                        </td>
                        <td class="log-message">
                            <div class="message">{{ line.message }}</div>

                            {%- if line.context %}
                                <pre>
                                    {{- '' -}}
                                    <code class="language-json">
                                        {{- line.context|json_encode((line.context|length > 1 ? constant('JSON_PRETTY_PRINT') : 0)) -}}
                                    </code>
                                    {{- '' -}}
                                </pre>
                            {% endif -%}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        {% else %}
            <div class="zilch">
                {{ (title == "Error Log" ? "Everything appears to be okay right now! No errors were found." : "No log items were found.")|t("freeform") }}
            </div>
        {% endif %}

    </div>

    <div class="paginator-wrapper lower">
        {{ _self.pager(logReader, perPage, currentPage, segment3) }}
    </div>

{% endblock %}

{% css %}
    #content { padding: 0; }
{% endcss %}
