{% extends 'base.html' %}

{% block title %}系統日誌 - WOOO TECH{% endblock %}

{% block extra_css %}
<style>
    /* Page Header */
    .page-header {
        background: white;
        padding: 25px 30px;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.06);
        margin-bottom: 30px;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .page-header h4 {
        margin: 0;
        color: #1f2937;
        font-weight: 600;
    }

    .status-indicators {
        display: flex;
        gap: 20px;
        align-items: center;
    }

    .status-item {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 13px;
        color: #6b7280;
    }

    .status-dot {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        background: #10b981;
        animation: pulse 2s ease-in-out infinite;
    }

    @keyframes pulse {
        0%, 100% { opacity: 1; }
        50% { opacity: 0.5; }
    }

    /* Control Panel */
    .control-panel {
        background: white;
        padding: 25px;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.06);
        margin-bottom: 25px;
    }

    .control-section {
        margin-bottom: 20px;
    }

    .control-section:last-child {
        margin-bottom: 0;
    }

    .control-section-title {
        font-size: 13px;
        font-weight: 600;
        color: #6b7280;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin-bottom: 12px;
    }

    .control-buttons {
        display: flex;
        gap: 10px;
        flex-wrap: wrap;
    }

    .btn-control {
        padding: 10px 18px;
        border: none;
        border-radius: 8px;
        font-size: 14px;
        font-weight: 500;
        cursor: pointer;
        transition: all 0.3s;
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }

    .btn-refresh {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        box-shadow: 0 2px 6px rgba(102, 126, 234, 0.3);
    }

    .btn-refresh:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
    }

    .btn-refresh.spinning i {
        animation: spin 1s linear infinite;
    }

    @keyframes spin {
        from { transform: rotate(0deg); }
        to { transform: rotate(360deg); }
    }

    .btn-pause {
        background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
        color: white;
        box-shadow: 0 2px 6px rgba(245, 158, 11, 0.3);
    }

    .btn-pause:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(245, 158, 11, 0.4);
    }

    .btn-resume {
        background: linear-gradient(135deg, #10b981 0%, #059669 100%);
        color: white;
        box-shadow: 0 2px 6px rgba(16, 185, 129, 0.3);
    }

    .btn-resume:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);
    }

    .btn-clear {
        background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
        color: white;
        box-shadow: 0 2px 6px rgba(239, 68, 68, 0.3);
    }

    .btn-clear:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(239, 68, 68, 0.4);
    }

    .btn-download {
        background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);
        color: white;
        box-shadow: 0 2px 6px rgba(99, 102, 241, 0.3);
    }

    .btn-download:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
    }

    /* Level Filter */
    .filter-buttons {
        display: flex;
        gap: 8px;
        flex-wrap: wrap;
    }

    .btn-filter {
        padding: 8px 16px;
        border: 2px solid #e5e7eb;
        border-radius: 8px;
        font-size: 13px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s;
        background: white;
        color: #6b7280;
    }

    .btn-filter:hover {
        border-color: #6366f1;
        color: #6366f1;
    }

    .btn-filter.active {
        background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);
        color: white;
        border-color: #6366f1;
    }

    .btn-filter.error.active {
        background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
        border-color: #ef4444;
    }

    .btn-filter.warning.active {
        background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
        border-color: #f59e0b;
    }

    .btn-filter.info.active {
        background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
        border-color: #3b82f6;
    }

    /* Search Box */
    .search-box {
        position: relative;
        flex: 1;
        min-width: 250px;
    }

    .search-box input {
        width: 100%;
        padding: 10px 40px 10px 40px;
        border: 2px solid #e5e7eb;
        border-radius: 8px;
        font-size: 14px;
        transition: all 0.3s;
    }

    .search-box input:focus {
        outline: none;
        border-color: #6366f1;
        box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
    }

    .search-box .search-icon {
        position: absolute;
        left: 12px;
        top: 50%;
        transform: translateY(-50%);
        color: #9ca3af;
        font-size: 14px;
    }

    .search-box .clear-icon {
        position: absolute;
        right: 12px;
        top: 50%;
        transform: translateY(-50%);
        color: #9ca3af;
        font-size: 14px;
        cursor: pointer;
        display: none;
    }

    .search-box.has-text .clear-icon {
        display: block;
    }

    /* Font Size Controls */
    .font-size-controls {
        display: flex;
        gap: 6px;
    }

    .btn-font-size {
        padding: 8px 14px;
        border: 2px solid #e5e7eb;
        border-radius: 8px;
        font-size: 13px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s;
        background: white;
        color: #6b7280;
    }

    .btn-font-size:hover {
        border-color: #6366f1;
        color: #6366f1;
    }

    .btn-font-size.active {
        background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);
        color: white;
        border-color: #6366f1;
    }

    /* Toggle Switches */
    .toggle-group {
        display: flex;
        gap: 20px;
        flex-wrap: wrap;
    }

    .toggle-item {
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .toggle-switch {
        position: relative;
        display: inline-block;
        width: 48px;
        height: 26px;
    }

    .toggle-switch input {
        opacity: 0;
        width: 0;
        height: 0;
    }

    .slider {
        position: absolute;
        cursor: pointer;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: linear-gradient(135deg, #d1d5db 0%, #9ca3af 100%);
        transition: .4s;
        border-radius: 26px;
    }

    .slider:before {
        position: absolute;
        content: "";
        height: 20px;
        width: 20px;
        left: 3px;
        bottom: 3px;
        background: white;
        transition: .4s;
        border-radius: 50%;
        box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    }

    input:checked + .slider {
        background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    }

    input:checked + .slider:before {
        transform: translateX(22px);
    }

    .toggle-label {
        font-size: 14px;
        color: #4b5563;
        font-weight: 500;
    }

    /* Statistics Cards */
    .stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 15px;
        margin-bottom: 25px;
    }

    .stat-card {
        background: white;
        padding: 20px;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.06);
        display: flex;
        align-items: center;
        gap: 15px;
    }

    .stat-icon {
        width: 50px;
        height: 50px;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 22px;
    }

    .stat-card.total .stat-icon {
        background: linear-gradient(135deg, #e0e7ff 0%, #c7d2fe 100%);
        color: #6366f1;
    }

    .stat-card.error .stat-icon {
        background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
        color: #ef4444;
    }

    .stat-card.warning .stat-icon {
        background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
        color: #f59e0b;
    }

    .stat-card.info .stat-icon {
        background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%);
        color: #3b82f6;
    }

    .stat-content {
        flex: 1;
    }

    .stat-value {
        font-size: 28px;
        font-weight: 700;
        color: #1f2937;
        line-height: 1;
    }

    .stat-label {
        font-size: 12px;
        color: #6b7280;
        font-weight: 500;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin-top: 5px;
    }

    /* Log Container */
    .log-container-wrapper {
        background: white;
        padding: 0;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.06);
        overflow: hidden;
    }

    #log-container {
        background-color: #1e1e1e;
        color: #d4d4d4;
        font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
        padding: 20px;
        height: 65vh;
        overflow-y: auto;
        overflow-x: auto;
        white-space: pre-wrap;
        word-wrap: break-word;
        font-size: 13px;
        line-height: 1.6;
        position: relative;
    }

    #log-container.font-small { font-size: 11px; }
    #log-container.font-medium { font-size: 13px; }
    #log-container.font-large { font-size: 15px; }

    /* Custom Scrollbar */
    #log-container::-webkit-scrollbar {
        width: 12px;
        height: 12px;
    }

    #log-container::-webkit-scrollbar-track {
        background: #2d2d2d;
        border-radius: 6px;
    }

    #log-container::-webkit-scrollbar-thumb {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 6px;
    }

    /* Log Line Highlighting */
    .log-line {
        padding: 2px 0;
        border-radius: 3px;
    }

    .log-line.error {
        background: rgba(239, 68, 68, 0.15);
        border-left: 3px solid #ef4444;
        padding-left: 8px;
        margin: 2px 0;
    }

    .log-line.warning {
        background: rgba(245, 158, 11, 0.15);
        border-left: 3px solid #f59e0b;
        padding-left: 8px;
        margin: 2px 0;
    }

    .log-line.info {
        border-left: 3px solid #3b82f6;
        padding-left: 8px;
        margin: 2px 0;
    }

    .log-timestamp { color: #10b981; font-weight: 600; }
    .log-error { color: #f87171; font-weight: 600; }
    .log-warning { color: #fbbf24; font-weight: 600; }
    .log-info { color: #60a5fa; font-weight: 600; }
    .highlight { background: rgba(251, 191, 36, 0.4); padding: 2px 4px; border-radius: 2px; }

    .log-empty {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 100%;
        color: #6b7280;
    }

    .log-empty i { font-size: 64px; color: #9ca3af; margin-bottom: 20px; }
    .log-empty p { font-size: 16px; margin: 0; }

    /* Responsive */
    @media (max-width: 768px) {
        .page-header {
            flex-direction: column;
            gap: 15px;
        }
        .control-buttons { flex-direction: column; }
        .btn-control { width: 100%; }
        .stats-grid { grid-template-columns: 1fr; }
        #log-container { height: 50vh; }
    }
</style>
{% endblock %}

{% block content %}
<div class="container">
    <!-- Page Header -->
    <div class="page-header">
        <h4><i class="fas fa-file-alt me-2"></i>系統即時日誌</h4>
        <div class="status-indicators">
            <div class="status-item">
                <div class="status-dot"></div>
                <span id="connection-status">連接正常</span>
            </div>
            <div class="status-item">
                <i class="fas fa-clock"></i>
                <span id="last-update">最後更新: --</span>
            </div>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="stats-grid">
        <div class="stat-card total">
            <div class="stat-icon"><i class="fas fa-list"></i></div>
            <div class="stat-content">
                <div class="stat-value" id="total-lines">0</div>
                <div class="stat-label">總行數</div>
            </div>
        </div>
        <div class="stat-card error">
            <div class="stat-icon"><i class="fas fa-times-circle"></i></div>
            <div class="stat-content">
                <div class="stat-value" id="error-count">0</div>
                <div class="stat-label">ERROR</div>
            </div>
        </div>
        <div class="stat-card warning">
            <div class="stat-icon"><i class="fas fa-exclamation-triangle"></i></div>
            <div class="stat-content">
                <div class="stat-value" id="warning-count">0</div>
                <div class="stat-label">WARNING</div>
            </div>
        </div>
        <div class="stat-card info">
            <div class="stat-icon"><i class="fas fa-info-circle"></i></div>
            <div class="stat-content">
                <div class="stat-value" id="info-count">0</div>
                <div class="stat-label">INFO</div>
            </div>
        </div>
    </div>

    <!-- Control Panel -->
    <div class="control-panel">
        <div class="control-section">
            <div class="control-section-title">主要操作</div>
            <div class="control-buttons">
                <button class="btn-control btn-refresh" onclick="refreshLogs()">
                    <i class="fas fa-sync-alt"></i><span>立即刷新</span>
                </button>
                <button class="btn-control btn-pause" id="pause-btn" onclick="toggleAutoRefresh()">
                    <i class="fas fa-pause"></i><span>暫停自動刷新</span>
                </button>
                <button class="btn-control btn-clear" onclick="clearLogs()">
                    <i class="fas fa-eraser"></i><span>清除日誌</span>
                </button>
                <button class="btn-control btn-download" onclick="downloadLogs()">
                    <i class="fas fa-download"></i><span>下載日誌</span>
                </button>
            </div>
        </div>

        <div class="control-section">
            <div class="control-section-title">過濾與搜尋</div>
            <div style="display: flex; gap: 15px; flex-wrap: wrap; align-items: center;">
                <div class="filter-buttons">
                    <button class="btn-filter active" data-level="all" onclick="filterByLevel('all')">
                        <i class="fas fa-list me-1"></i>ALL
                    </button>
                    <button class="btn-filter error" data-level="error" onclick="filterByLevel('error')">
                        <i class="fas fa-times-circle me-1"></i>ERROR
                    </button>
                    <button class="btn-filter warning" data-level="warning" onclick="filterByLevel('warning')">
                        <i class="fas fa-exclamation-triangle me-1"></i>WARNING
                    </button>
                    <button class="btn-filter info" data-level="info" onclick="filterByLevel('info')">
                        <i class="fas fa-info-circle me-1"></i>INFO
                    </button>
                </div>
                <div class="search-box" id="search-box">
                    <i class="fas fa-search search-icon"></i>
                    <input type="text" id="search-input" placeholder="搜尋關鍵字..." oninput="searchLogs()">
                    <i class="fas fa-times clear-icon" onclick="clearSearch()"></i>
                </div>
            </div>
        </div>

        <div class="control-section" style="margin-bottom: 0;">
            <div class="control-section-title">顯示選項</div>
            <div style="display: flex; gap: 30px; flex-wrap: wrap; align-items: center;">
                <div style="display: flex; gap: 8px; align-items: center;">
                    <span style="font-size: 13px; color: #6b7280; font-weight: 500;">字體大小:</span>
                    <div class="font-size-controls">
                        <button class="btn-font-size" data-size="small" onclick="changeFontSize('small')">小</button>
                        <button class="btn-font-size active" data-size="medium" onclick="changeFontSize('medium')">中</button>
                        <button class="btn-font-size" data-size="large" onclick="changeFontSize('large')">大</button>
                    </div>
                </div>
                <div class="toggle-group">
                    <div class="toggle-item">
                        <label class="toggle-switch">
                            <input type="checkbox" id="auto-scroll-toggle" checked onchange="toggleAutoScroll()">
                            <span class="slider"></span>
                        </label>
                        <span class="toggle-label">自動滾動</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Log Container -->
    <div class="log-container-wrapper">
        <div id="log-container" class="font-medium">
            <div class="log-empty">
                <i class="fas fa-file-alt"></i>
                <p>正在載入日誌...</p>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    // Global state
    let autoRefreshEnabled = true;
    let autoScrollEnabled = true;
    let currentFilter = 'all';
    let searchKeyword = '';
    let refreshInterval = null;
    let rawLogs = '';

    // Initialize
    document.addEventListener('DOMContentLoaded', function() {
        showLoading('正在載入系統日誌...', '請稍候，系統正在讀取日誌資料');
        fetchLogs();
        startAutoRefresh();
    });

    function updateLastUpdateTime() {
        const now = new Date();
        const timeStr = now.toLocaleTimeString('zh-TW');
        document.getElementById('last-update').textContent = `最後更新: ${timeStr}`;
    }

    function fetchLogs() {
        fetch('/api/logs')
            .then(response => response.json())
            .then(data => {
                rawLogs = data.logs || '';
                updateStats(rawLogs);
                displayLogs();
                updateLastUpdateTime();
                document.getElementById('connection-status').textContent = '連接正常';
                hideLoading(); // 隱藏載入動畫
            })
            .catch(error => {
                console.error('Error fetching logs:', error);
                document.getElementById('connection-status').textContent = '連接失敗';
                showToast('載入日誌失敗', 'error');
                hideLoading(); // 即使失敗也要隱藏載入動畫
            });
    }

    function displayLogs() {
        const container = document.getElementById('log-container');

        if (!rawLogs || rawLogs.trim() === '') {
            container.innerHTML = '<div class="log-empty"><i class="fas fa-file-alt"></i><p>暫無日誌資料</p></div>';
            return;
        }

        const lines = rawLogs.split('\n');
        let filteredLines = lines;

        if (currentFilter !== 'all') {
            filteredLines = filteredLines.filter(line => {
                const upperLine = line.toUpperCase();
                if (currentFilter === 'error') return upperLine.includes('ERROR');
                if (currentFilter === 'warning') return upperLine.includes('WARNING');
                if (currentFilter === 'info') return upperLine.includes('INFO');
                return true;
            });
        }

        if (searchKeyword) {
            filteredLines = filteredLines.filter(line =>
                line.toLowerCase().includes(searchKeyword.toLowerCase())
            );
        }

        const formattedLines = filteredLines.map(line => formatLogLine(line)).join('\n');
        container.innerHTML = formattedLines || '<div class="log-empty"><i class="fas fa-search"></i><p>沒有符合的日誌</p></div>';

        if (autoScrollEnabled) {
            container.scrollTop = container.scrollHeight;
        }
    }

    function formatLogLine(line) {
        if (!line.trim()) return '';

        let className = '';
        let formatted = line;

        const upperLine = line.toUpperCase();
        if (upperLine.includes('ERROR')) {
            className = 'error';
            formatted = line.replace(/ERROR/gi, '<span class="log-error">ERROR</span>');
        } else if (upperLine.includes('WARNING')) {
            className = 'warning';
            formatted = line.replace(/WARNING/gi, '<span class="log-warning">WARNING</span>');
        } else if (upperLine.includes('INFO')) {
            className = 'info';
            formatted = line.replace(/INFO/gi, '<span class="log-info">INFO</span>');
        }

        formatted = formatted.replace(/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/g, '<span class="log-timestamp">$1</span>');

        if (searchKeyword) {
            const regex = new RegExp(`(${searchKeyword})`, 'gi');
            formatted = formatted.replace(regex, '<span class="highlight">$1</span>');
        }

        return `<div class="log-line ${className}">${formatted}</div>`;
    }

    function updateStats(logs) {
        const lines = logs.split('\n').filter(line => line.trim());
        document.getElementById('total-lines').textContent = lines.length;
        document.getElementById('error-count').textContent = lines.filter(line => line.toUpperCase().includes('ERROR')).length;
        document.getElementById('warning-count').textContent = lines.filter(line => line.toUpperCase().includes('WARNING')).length;
        document.getElementById('info-count').textContent = lines.filter(line => line.toUpperCase().includes('INFO')).length;
    }

    function refreshLogs() {
        const btn = event.target.closest('.btn-refresh');
        btn.classList.add('spinning');
        btn.disabled = true;
        fetchLogs();
        setTimeout(() => {
            btn.classList.remove('spinning');
            btn.disabled = false;
            showToast('日誌已刷新', 'success');
        }, 600);
    }

    function toggleAutoRefresh() {
        autoRefreshEnabled = !autoRefreshEnabled;
        const btn = document.getElementById('pause-btn');

        if (autoRefreshEnabled) {
            btn.className = 'btn-control btn-pause';
            btn.innerHTML = '<i class="fas fa-pause"></i><span>暫停自動刷新</span>';
            startAutoRefresh();
            showToast('已啟用自動刷新', 'success');
        } else {
            btn.className = 'btn-control btn-resume';
            btn.innerHTML = '<i class="fas fa-play"></i><span>繼續自動刷新</span>';
            stopAutoRefresh();
            showToast('已暫停自動刷新', 'info');
        }
    }

    function startAutoRefresh() {
        if (refreshInterval) clearInterval(refreshInterval);
        refreshInterval = setInterval(fetchLogs, 5000);
    }

    function stopAutoRefresh() {
        if (refreshInterval) {
            clearInterval(refreshInterval);
            refreshInterval = null;
        }
    }

    function clearLogs() {
        if (confirm('確定要清除日誌顯示嗎？（不會刪除實際日誌檔案）')) {
            rawLogs = '';
            updateStats('');
            displayLogs();
            showToast('已清除日誌顯示', 'success');
        }
    }

    function downloadLogs() {
        if (!rawLogs) {
            showToast('沒有可下載的日誌', 'error');
            return;
        }

        const blob = new Blob([rawLogs], { type: 'text/plain' });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        const now = new Date();
        const timestamp = now.toISOString().replace(/[:.]/g, '-').slice(0, -5);
        a.download = `momo_system_logs_${timestamp}.txt`;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
        showToast('日誌已下載', 'success');
    }

    function filterByLevel(level) {
        currentFilter = level;
        document.querySelectorAll('.btn-filter').forEach(btn => {
            btn.classList.remove('active');
            if (btn.dataset.level === level) btn.classList.add('active');
        });
        displayLogs();
    }

    function searchLogs() {
        const input = document.getElementById('search-input');
        const searchBox = document.getElementById('search-box');
        searchKeyword = input.value.trim();
        searchBox.classList.toggle('has-text', !!searchKeyword);
        displayLogs();
    }

    function clearSearch() {
        const input = document.getElementById('search-input');
        input.value = '';
        searchKeyword = '';
        document.getElementById('search-box').classList.remove('has-text');
        displayLogs();
        input.focus();
    }

    function changeFontSize(size) {
        const container = document.getElementById('log-container');
        container.classList.remove('font-small', 'font-medium', 'font-large');
        container.classList.add(`font-${size}`);
        document.querySelectorAll('.btn-font-size').forEach(btn => {
            btn.classList.remove('active');
            if (btn.dataset.size === size) btn.classList.add('active');
        });
    }

    function toggleAutoScroll() {
        autoScrollEnabled = document.getElementById('auto-scroll-toggle').checked;
        if (autoScrollEnabled) {
            document.getElementById('log-container').scrollTop = document.getElementById('log-container').scrollHeight;
        }
    }
</script>
{% endblock %}
