<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Word 预览 - {{ filename }}</title>
    <script src="/static/lib/mammoth.browser.min.js"></script>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
            background-color: #f8f9fa;
            min-height: 100vh;
        }
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 12px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 10px;
            position: sticky;
            top: 0;
            z-index: 100;
        }
        .header a {
            color: white;
            text-decoration: none;
            padding: 8px 16px;
            background: rgba(255,255,255,0.2);
            border-radius: 6px;
            font-size: 14px;
        }
        .header a:hover {
            background: rgba(255,255,255,0.3);
        }
        .filename {
            font-size: 16px;
            font-weight: 500;
        }
        .document-container {
            padding: 20px;
            max-width: 900px;
            margin: 0 auto;
        }
        .document {
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 40px;
            min-height: 500px;
        }
        .loading {
            text-align: center;
            padding: 40px;
            color: #666;
        }
        .error {
            text-align: center;
            padding: 40px;
            color: #cf222e;
            background: #ffebe9;
            border-radius: 8px;
        }
        
        /* Word 文档样式 */
        .document h1 {
            font-size: 2em;
            border-bottom: 2px solid #667eea;
            padding-bottom: 10px;
            margin-bottom: 20px;
            color: #24292f;
        }
        .document h2 {
            font-size: 1.5em;
            margin-top: 25px;
            margin-bottom: 15px;
            color: #333;
        }
        .document h3 {
            font-size: 1.25em;
            margin-top: 20px;
            margin-bottom: 10px;
            color: #444;
        }
        .document p {
            line-height: 1.8;
            margin-bottom: 15px;
            color: #24292f;
        }
        .document ul, .document ol {
            margin-left: 25px;
            margin-bottom: 15px;
        }
        .document li {
            line-height: 1.6;
            margin-bottom: 8px;
        }
        .document blockquote {
            border-left: 4px solid #667eea;
            padding-left: 20px;
            margin: 20px 0;
            color: #555;
            background: #f8f9fa;
            padding: 15px 20px;
        }
        .document code {
            background: #f0f0f0;
            padding: 2px 6px;
            border-radius: 4px;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 0.9em;
        }
        .document pre {
            background: #2d2d2d;
            color: #f8f8f2;
            padding: 20px;
            border-radius: 8px;
            overflow-x: auto;
            margin: 20px 0;
        }
        .document pre code {
            background: none;
            padding: 0;
            color: inherit;
        }
        .document table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        .document th, .document td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        .document th {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        .document tr:nth-child(even) {
            background-color: #f8f9fa;
        }
        .document img {
            max-width: 100%;
            height: auto;
            border-radius: 8px;
            margin: 15px 0;
        }
        .document hr {
            border: none;
            border-top: 2px solid #e0e0e0;
            margin: 25px 0;
        }
        .document a {
            color: #0969da;
            text-decoration: none;
        }
        .document a:hover {
            text-decoration: underline;
        }
        
        @media (max-width: 768px) {
            .header {
                padding: 10px;
            }
            .document-container {
                padding: 10px;
            }
            .document {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <a href="{{ url_for('browser.browse', path=get_relative_path(current_dir)) }}">← 返回目录</a>
        <span class="filename">{{ filename }}</span>
        <a href="{{ url_for('file.download_file', path=file_path) }}">📥 下载</a>
    </div>
    
    <div class="document-container">
        <div id="document-content" class="document">
            <div class="loading">正在加载 Word 文档...</div>
        </div>
    </div>

    <script>
        const filePath = '{{ file_path }}';

        // 加载 Word 文档
        fetch('/serve/' + encodeURIComponent(filePath))
            .then(response => {
                if (!response.ok) throw new Error('文件加载失败');
                return response.arrayBuffer();
            })
            .then(data => {
                mammoth.convertToHtml({ arrayBuffer: data })
                    .then(displayResult)
                    .catch(handleError);
            })
            .catch(handleError);

        function displayResult(result) {
            document.getElementById('document-content').innerHTML = result.value;
        }

        function handleError(err) {
            document.getElementById('document-content').innerHTML = 
                '<div class="error">加载失败: ' + err.message + '</div>';
        }
    </script>
</body>
</html>
