{% extends "base.html" %}

{% block title %}{{ page_title }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <h1>Application Logs</h1>
            <p>View and monitor application logs.</p>
            
            <!-- Log Filters -->
            <div class="card mb-4">
                <div class="card-header">
                    <h5>Log Filters</h5>
                </div>
                <div class="card-body">
                    <form id="log-filter-form" class="row g-3">
                        <div class="col-md-3">
                            <label for="log-level" class="form-label">Log Level</label>
                            <select class="form-select" id="log-level">
                                <option value="">All Levels</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-3">
                            <label for="log-component" class="form-label">Component</label>
                            <select class="form-select" id="log-component">
                                <option value="">All Components</option>
                                <option value="core">Core</option>
                                <option value="web_dashboard">Web Dashboard</option>
                                <option value="qt_player">Qt Player</option>
                                <option value="api_client">API Client</option>
                                <option value="database">Database</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label for="log-limit" class="form-label">Limit</label>
                            <select class="form-select" id="log-limit">
                                <option value="50">50 entries</option>
                                <option value="100" selected>100 entries</option>
                                <option value="500">500 entries</option>
                                <option value="1000">1000 entries</option>
                            </select>
                        </div>
                        <div class="col-md-3 d-flex align-items-end">
                            <button type="submit" class="btn btn-primary me-2">Filter</button>
                            <button type="button" class="btn btn-secondary" id="refresh-logs">Refresh</button>
                        </div>
                    </form>
                </div>
            </div>
            
            <!-- Log Entries -->
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5>Log Entries</h5>
                    <button class="btn btn-danger btn-sm" id="clear-logs">
                        <i class="fas fa-trash"></i> Clear Logs
                    </button>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped table-sm">
                            <thead>
                                <tr>
                                    <th>Timestamp</th>
                                    <th>Level</th>
                                    <th>Component</th>
                                    <th>Message</th>
                                </tr>
                            </thead>
                            <tbody id="logs-table-body">
                                <!-- Log rows will be populated by JavaScript -->
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // Load logs on page load
    function loadLogs() {
        const level = document.getElementById('log-level').value;
        const component = document.getElementById('log-component').value;
        const limit = document.getElementById('log-limit').value;
        
        let url = '/api/logs?limit=' + limit;
        if (level) url += '&level=' + level;
        if (component) url += '&component=' + component;
        
        fetch(url)
            .then(response => response.json())
            .then(data => {
                const tbody = document.getElementById('logs-table-body');
                tbody.innerHTML = '';
                
                data.forEach(log => {
                    const row = document.createElement('tr');
                    const timestamp = new Date(log.timestamp).toLocaleString();
                    
                    // Add level-specific class for coloring
                    let levelClass = '';
                    switch (log.level) {
                        case 'ERROR':
                        case 'CRITICAL':
                            levelClass = 'table-danger';
                            break;
                        case 'WARNING':
                            levelClass = 'table-warning';
                            break;
                        case 'INFO':
                            levelClass = 'table-info';
                            break;
                        default:
                            levelClass = '';
                    }
                    
                    row.className = levelClass;
                    row.innerHTML = `
                        <td>${timestamp}</td>
                        <td>${log.level}</td>
                        <td>${log.component}</td>
                        <td>${log.message}</td>
                    `;
                    tbody.appendChild(row);
                });
            })
            .catch(error => {
                console.error('Error loading logs:', error);
            });
    }
    
    // Filter logs
    document.getElementById('log-filter-form').addEventListener('submit', function(e) {
        e.preventDefault();
        loadLogs();
    });
    
    // Refresh logs
    document.getElementById('refresh-logs').addEventListener('click', function() {
        loadLogs();
    });
    
    // Clear logs
    document.getElementById('clear-logs').addEventListener('click', function() {
        if (confirm('Are you sure you want to clear all logs?')) {
            // In a real implementation, this would clear the logs
            alert('Clear logs functionality would be implemented here');
        }
    });
    
    // Load logs when page loads
    document.addEventListener('DOMContentLoaded', function() {
        loadLogs();
    });
</script>
{% endblock %}