{% extends "admin/admin_base.html" %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <h2 class="mb-4"><i class="bi bi-journal-text"></i> Логи и аудит</h2>
            
            <!-- Вкладки -->
            <ul class="nav nav-tabs mb-3" id="logsTabs" role="tablist">
                <li class="nav-item" role="presentation">
                    <button class="nav-link active" onclick="switchTab('system')" type="button">Системные логи</button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" onclick="switchTab('audit')" type="button">Аудит действий</button>
                </li>
            </ul>
            
            <!-- Фильтры -->
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-funnel"></i> Фильтры и поиск</h5>
                </div>
                <div class="card-body">
                    <div class="row g-3">
                        <div class="col-md-2">
                            <label class="form-label">Уровень</label>
                            <select class="form-select form-select-sm" id="logLevel">
                                <option value="">Все уровни</option>
                                <option value="DEBUG">DEBUG</option>
                                <option value="INFO">INFO</option>
                                <option value="WARNING">WARNING</option>
                                <option value="ERROR">ERROR</option>
                                <option value="CRITICAL">CRITICAL</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">Логгер</label>
                            <input type="text" class="form-control form-control-sm" id="logLogger" placeholder="Имя логгера">
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Поиск</label>
                            <input type="text" class="form-control form-control-sm" id="logSearch" placeholder="Поиск по тексту...">
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">С</label>
                            <input type="datetime-local" class="form-control form-control-sm" id="logStartTime">
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">По</label>
                            <input type="datetime-local" class="form-control form-control-sm" id="logEndTime">
                        </div>
                        <div class="col-md-1 d-flex align-items-end">
                            <button class="btn btn-primary btn-sm w-100" id="applyFilters">
                                <i class="bi bi-search"></i> Применить
                            </button>
                        </div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-12">
                            <button class="btn btn-secondary btn-sm" id="clearFilters">
                                <i class="bi bi-x-circle"></i> Очистить фильтры
                            </button>
                            <div class="btn-group ms-2" role="group">
                                <button type="button" class="btn btn-success btn-sm dropdown-toggle" data-bs-toggle="dropdown">
                                    <i class="bi bi-download"></i> Экспорт
                                </button>
                                <ul class="dropdown-menu">
                                    <li><a class="dropdown-item" href="#" onclick="exportLogs('json'); return false;">JSON</a></li>
                                    <li><a class="dropdown-item" href="#" onclick="exportLogs('csv'); return false;">CSV</a></li>
                                    <li><a class="dropdown-item" href="#" onclick="exportLogs('txt'); return false;">TXT</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Логи -->
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Логи</h5>
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" id="autoRefresh" checked>
                        <label class="form-check-label" for="autoRefresh">Автообновление (5 сек)</label>
                    </div>
                </div>
                <div class="card-body">
                    <div id="logsContainer" style="max-height: 600px; overflow-y: auto;">
                        <div class="text-center">
                            <div class="spinner-border text-primary" role="status">
                                <span class="visually-hidden">Загрузка...</span>
                            </div>
                        </div>
                    </div>
                    <div id="logsPagination" class="mt-3"></div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Модальное окно для исключений -->
<div class="modal fade" id="exceptionModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Детали исключения</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <pre id="exceptionContent" style="max-height: 500px; overflow-y: auto;"></pre>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block admin_scripts %}
<script src="{{ url_for('static', filename='js/logs.js') }}"></script>
{% endblock %}
