{% extends "base.html" %}
{% block title %}Логи{% endblock %}
{% block styles %}
    <link rel="stylesheet" href="{{ url_for('static', filename='css/logs.css') }}">
{% endblock %}
{% block content %}
    <div class="container mt-4">
        <h2 class="mb-4">Журнал действий</h2>

        <!-- Форма фильтрации -->
        <form method="get" class="mb-4">
            <div class="row g-3">
                <div class="col-md">
                    <label for="action" class="form-label">Тип действия</label>
                    <input type="text" class="form-control" id="action" name="action"
                           value="{{ filters.action or '' }}">
                </div>
                <div class="col-md">
                    <label for="username" class="form-label">Пользователь</label>
                    <input type="text" class="form-control" id="username" name="username"
                           value="{{ filters.username or '' }}">
                </div>
                <div class="col-md">
                    <label for="timestamp_from" class="form-label">Дата с</label>
                    <input type="date" class="form-control" id="timestamp_from" name="timestamp_from"
                           value="{{ filters.timestamp_from or '' }}">
                </div>
                <div class="col-md">
                    <label for="timestamp_to" class="form-label">Дата по</label>
                    <input type="date" class="form-control" id="timestamp_to" name="timestamp_to"
                           value="{{ filters.timestamp_to or '' }}">
                </div>
                <div class="col-md-auto align-self-end">
                    <button type="submit" class="btn btn-primary">Фильтровать</button>
                </div>
            </div>
        </form>

        <!-- Таблица логов -->
        <div class="table-responsive">
            <table class="table table-bordered table-striped">
                <thead class="table-light">
                <tr>
                    <th>
                        {% set new_order = 'asc' if filters.order != 'asc' else 'desc' %}
                        <a href="{{ url_for('dashboard.logs_page', page=1, order=new_order,
                            action=filters.action, username=filters.username,
                            timestamp_from=filters.timestamp_from, timestamp_to=filters.timestamp_to) }}">
                            Дата и время
                            {% if filters.order == 'asc' %}
                                ▲
                            {% elif filters.order == 'desc' %}
                                ▼
                            {% endif %}
                        </a>
                    </th>
                    <th>Пользователь</th>
                    <th>Тип действия</th>
                    <th>Описание</th>
                    <th>IP-адрес</th>
                </tr>
                </thead>
                <tbody>
                {% for log in logs.items %}
                    <tr>
                        <td>{{ log.timestamp.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                        <td>{{ log.username }}</td>
                        <td>{{ log.action }}</td>
                        <td>{{ log.details }}</td>
                        <td>{{ log.ipaddress }}</td>
                    </tr>
                {% else %}
                    <tr>
                        <td colspan="5" class="text-center">Нет записей</td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- Пагинация -->
        <nav>
            <ul class="pagination justify-content-center">
                {% if logs.has_prev %}
                    <li class="page-item">
                        <a class="page-link"
                           href="{{ url_for('dashboard.logs_page', page=logs.prev_num, order=filters.order,
                                            action=filters.action, username=filters.username,
                                            timestamp_from=filters.timestamp_from, timestamp_to=filters.timestamp_to) }}">
                            Назад
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled"><span class="page-link">Назад</span></li>
                {% endif %}

                {% for page_num in logs.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=2) %}
                    {% if page_num %}
                        {% if page_num == logs.page %}
                            <li class="page-item active"><span class="page-link">{{ page_num }}</span></li>
                        {% else %}
                            <li class="page-item">
                                <a class="page-link"
                                   href="{{ url_for('dashboard.logs_page', page=page_num, order=filters.order,
                                                    action=filters.action, username=filters.username,
                                                    timestamp_from=filters.timestamp_from, timestamp_to=filters.timestamp_to) }}">
                                    {{ page_num }}
                                </a>
                            </li>
                        {% endif %}
                    {% else %}
                        <li class="page-item disabled"><span class="page-link">…</span></li>
                    {% endif %}
                {% endfor %}

                {% if logs.has_next %}
                    <li class="page-item">
                        <a class="page-link"
                           href="{{ url_for('dashboard.logs_page', page=logs.next_num, order=filters.order,
                                            action=filters.action, username=filters.username,
                                            timestamp_from=filters.timestamp_from, timestamp_to=filters.timestamp_to) }}">
                            Вперёд
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled"><span class="page-link">Вперёд</span></li>
                {% endif %}
            </ul>
        </nav>
    </div>
{% endblock %}
