<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>视频下载器</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background: #0a0a0a;
            color: #f5f5f5;
            height: 100vh;
            overflow: hidden;
            display: flex;
        }
        /* 左侧面板 */
        .left-panel {
            flex: 1;
            display: flex;
            flex-direction: column;
            border-right: 1px solid #222;
            min-width: 420px;
        }
        .input-section {
            padding: 32px;
            border-bottom: 1px solid #222;
        }
        .input-section h1 {
            font-size: 22px;
            font-weight: 700;
            margin-bottom: 4px;
            color: #fff;
            letter-spacing: -0.02em;
        }
        .input-section .subtitle {
            color: #666;
            font-size: 13px;
            margin-bottom: 24px;
        }
        .input-group {
            margin-bottom: 16px;
        }
        .input-group label {
            display: block;
            font-size: 12px;
            font-weight: 500;
            margin-bottom: 6px;
            color: #888;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }
        .input-group input {
            width: 100%;
            padding: 12px 14px;
            border: 1px solid #333;
            border-radius: 10px;
            background: #0a0a0a;
            color: #f5f5f5;
            font-size: 14px;
            outline: none;
            transition: all 0.2s ease;
        }
        .input-group input::placeholder { color: #444; }
        .input-group input:focus { border-color: #fff; background: #000; }
        button {
            width: 100%;
            padding: 14px;
            border: none;
            border-radius: 10px;
            background: #fff;
            color: #000;
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        button:hover:not(:disabled) { background: #e0e0e0; transform: translateY(-1px); }
        button:disabled { opacity: 0.4; cursor: not-allowed; }
        .status {
            margin-top: 16px;
            padding: 12px 14px;
            border-radius: 10px;
            font-size: 13px;
            line-height: 1.5;
            display: none;
            font-weight: 500;
        }
        .status.show { display: block; }
        .status.success { background: #0a1f0a; color: #4ade80; border: 1px solid #1a3a1a; }
        .status.error { background: #1f0a0a; color: #f87171; border: 1px solid #3a1a1a; }
        .status.info { background: #0a0a1f; color: #60a5fa; border: 1px solid #1a1a3a; }
        .spinner {
            display: inline-block;
            width: 14px;
            height: 14px;
            border: 2px solid rgba(255,255,255,0.15);
            border-top-color: #60a5fa;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
            margin-right: 8px;
            vertical-align: middle;
        }
        @keyframes spin { to { transform: rotate(360deg); } }
        .footer-meta {
            margin-top: 14px;
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 11px;
            color: #444;
        }
        .badge {
            display: inline-block;
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 10px;
            font-weight: 600;
            letter-spacing: 0.02em;
        }
        .badge.ok { background: #0a1f0a; color: #4ade80; border: 1px solid #1a3a1a; }
        .badge.warn { background: #1f1a0a; color: #fbbf24; border: 1px solid #3a301a; }

        /* 日志区域 */
        .log-section {
            flex: 1;
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        .log-header {
            padding: 14px 32px;
            font-size: 12px;
            font-weight: 600;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            border-bottom: 1px solid #1a1a1a;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .log-panel {
            flex: 1;
            padding: 20px 32px;
            background: #000;
            color: #4ade80;
            font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
            font-size: 12px;
            line-height: 1.7;
            overflow-y: auto;
            white-space: pre-wrap;
            word-break: break-all;
        }

        /* 右侧面板 */
        .right-panel {
            flex: 1.2;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 40px;
            position: relative;
            overflow: hidden;
        }
        .right-panel .placeholder {
            text-align: center;
            color: #333;
        }
        .right-panel .placeholder svg {
            width: 64px;
            height: 64px;
            margin-bottom: 16px;
            opacity: 0.3;
        }
        .right-panel .placeholder p {
            font-size: 14px;
        }
        .video-wrap {
            width: 100%;
            max-width: 720px;
            height: 100%;
            display: none;
            flex-direction: column;
            gap: 16px;
            justify-content: center;
            align-items: center;
        }
        .video-wrap.show { display: flex; }
        .video-player {
            max-width: 100%;
            max-height: calc(100% - 70px);
            width: auto;
            height: auto;
            object-fit: contain;
            border-radius: 12px;
            background: #000;
            border: 1px solid #222;
            outline: none;
        }
        .video-info {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 16px;
        }
        .video-title {
            font-size: 15px;
            font-weight: 600;
            color: #fff;
            line-height: 1.4;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            min-width: 0;
            flex: 1;
        }
        .video-actions {
            display: flex;
            align-items: center;
            gap: 12px;
            flex-shrink: 0;
        }
        .countdown {
            font-size: 12px;
            color: #f87171;
            font-variant-numeric: tabular-nums;
        }
        .btn-download {
            padding: 8px 16px;
            border-radius: 8px;
            background: #1a1a1a;
            color: #fff;
            font-size: 13px;
            font-weight: 500;
            text-decoration: none;
            border: 1px solid #333;
            transition: all 0.2s;
            white-space: nowrap;
        }
        .btn-download:hover { background: #2a2a2a; border-color: #444; }
    </style>
</head>
<body>
    <div class="left-panel">
        <div class="input-section">
            <h1>视频下载</h1>
            <p class="subtitle">支持 X 和 Bilibili</p>

            <div class="input-group">
                <label for="url">视频链接</label>
                <input type="url" id="url" placeholder="https://x.com/... 或 https://www.bilibili.com/video/...">
            </div>

            <div class="input-group">
                <label for="outputDir">保存路径</label>
                <input type="text" id="outputDir" value="D:\UserData\Desktop\xiazai">
            </div>

            <button id="downloadBtn" onclick="startDownload()">开始下载</button>

            <div id="status" class="status"></div>

            <div class="footer-meta">
                <span>FFmpeg</span>
                {% if ffmpeg_ok %}
                    <span class="badge ok">已就绪</span>
                {% else %}
                    <span class="badge warn">未检测到</span>
                {% endif %}
            </div>
        </div>

        <div class="log-section" style="display:none;">
            <div class="log-header">
                <span>控制台输出</span>
                <span id="logStatus" style="font-size:11px;color:#444;">等待开始</span>
            </div>
            <pre id="logPanel" class="log-panel"></pre>
        </div>
    </div>

    <div class="right-panel">
        <div id="placeholder" class="placeholder">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
                <rect x="2" y="2" width="20" height="20" rx="4"/>
                <polygon points="9,8 17,12 9,16"/>
            </svg>
            <p>下载完成后在此预览视频</p>
        </div>

        <div id="videoWrap" class="video-wrap">
            <video id="videoPlayer" class="video-player" controls></video>
            <div class="video-info">
                <div id="videoTitle" class="video-title"></div>
                <div class="video-actions">
                    <a id="downloadLink" class="btn-download" href="#" target="_blank">下载文件</a>
                </div>
            </div>
        </div>
    </div>

    <script>
        const urlInput = document.getElementById('url');
        const btn = document.getElementById('downloadBtn');
        const statusEl = document.getElementById('status');
        const logPanel = document.getElementById('logPanel');
        const logStatus = document.getElementById('logStatus');
        const placeholder = document.getElementById('placeholder');
        const videoWrap = document.getElementById('videoWrap');
        const videoPlayer = document.getElementById('videoPlayer');
        const videoTitle = document.getElementById('videoTitle');
        const countdownEl = document.getElementById('countdown');
        const downloadLink = document.getElementById('downloadLink');

        function showStatus(text, type) {
            statusEl.textContent = text;
            statusEl.className = 'status show ' + type;
        }
        function clearStatus() { statusEl.className = 'status'; }

        function showVideo(filepath, title) {
            placeholder.style.display = 'none';
            videoWrap.classList.add('show');
            videoPlayer.src = '/file?path=' + encodeURIComponent(filepath);
            videoTitle.textContent = title || '未命名视频';
            downloadLink.href = '/file?path=' + encodeURIComponent(filepath) + '&download=1';
            downloadLink.setAttribute('download', '');
        }

        async function startDownload() {
            const url = urlInput.value.trim();
            const outputDir = document.getElementById('outputDir').value.trim();
            if (!url) {
                showStatus('请输入视频链接', 'error');
                return;
            }

            btn.disabled = true;
            clearStatus();
            showStatus('', 'info');
            statusEl.innerHTML = '<span class="spinner"></span>正在下载，请稍候...';
            statusEl.className = 'status show info';

            // 隐藏之前的视频
            videoWrap.classList.remove('show');
            placeholder.style.display = 'block';
            videoPlayer.src = '';

            try {
                const res = await fetch('/download', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ url: url, output_dir: outputDir })
                });

                const data = await res.json();

                if (data.success) {
                    showStatus((data.title ? '《' + data.title + '》' : '') + ' 下载完成', 'success');
                    if (data.filepath) {
                        showVideo(data.filepath, data.title);
                    }
                } else {
                    showStatus(data.message, 'error');
                }
            } catch (err) {
                showStatus('请求失败: ' + err.message, 'error');
            } finally {
                btn.disabled = false;
            }
        }

        urlInput.addEventListener('keydown', (e) => {
            if (e.key === 'Enter') startDownload();
        });
    </script>
</body>
</html>
