<?php

try {
    // 从主页面获取音乐列表
    $musicList = require_once __DIR__ . '/data/music.php';
    
    // 获取要播放的音频ID
    $audioId = $_GET['play'] ?? '';
    $audioInfo = null;
    
    // 查找对应的音频信息
    foreach ($musicList as $music) {
        if ($music['id'] === $audioId) {
            $audioInfo = $music;
            break;
        }
    }
    
    // 如果找不到音频，跳转到主页面
    if (!$audioInfo) {
        header('Location: index.php');
        exit;
    }
} catch (Exception $e) {
    // 处理错误，显示友好信息并链接到主页面
    echo "<html><body>";
    echo "<h1>出错了: " . htmlspecialchars($e->getMessage()) . "</h1>";
    echo "<p><a href='index.php'>返回音乐主页</a></p>";
    echo "</body></html>";
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($audioInfo['title']); ?> - 音乐播放</title>
    <link rel="icon" href="./static/icon/new-icon.png" type="image/png">
    <link rel="alternate icon" href="./static/icon/icon.ico" type="image/x-ico">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        @font-face {
            font-family: "MyCustomFont";
            src: url("./static/font/bbc.ttf") format("truetype");
            font-weight: normal;
            font-style: normal;
        }
        :root {
            --main-font: "MyCustomFont", sans-serif; /* 自定义字体变量 */
        }
        /* 基础样式 */
        body {
            font-family: var(--main-font);
            margin: 0;
            padding: 20px;
            background-color: #f5f5f7;
            color: #333;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
            box-sizing: border-box;
        }
        
        .container {
            width: 100%;
            max-width: 600px;
            background-color: white;
            border-radius: 12px;
            padding: 2rem;
            box-shadow: 0 4px 20px rgba(0,0,0,0.1);
            text-align: center;
        }
        
        h1 {
            color: #2c3e50;
            margin-bottom: 0.5rem;
        }
        
        .artist {
            color: #666;
            font-size: 1.1rem;
            margin-bottom: 2rem;
            font-style: italic;
        }
        
        .back-link {
            display: inline-block;
            margin-bottom: 2rem;
            color: #2c3e50;
            text-decoration: none;
            font-size: 0.9rem;
            transition: color 0.3s;
        }
        
        .back-link:hover {
            color: #3498db;
            text-decoration: underline;
        }
        .video-container {
            position: relative;
            width: 100%;
            padding-top: 56.25%; /* 16:9比例 */
            margin: 1rem 0;
            overflow: hidden;
            border-radius: 8px;
        }
        
        .video-container iframe {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border: none;
        }
        
        
        /* 自定义音频播放器样式 */
        .custom-audio-player {
            width: 100%;
            margin: 1.5rem 0;
            box-sizing: border-box;
            background-color: #f8f9fa;
            border-radius: 8px;
            padding: 0.8rem;
            display: flex;
            align-items: center;
            gap: 0.8rem;
            flex-wrap: wrap;
        }
        
        .audio-controls {
            display: flex;
            align-items: center;
            gap: 0.8rem;
        }
        
        .audio-button {
            background: none;
            border: none;
            cursor: pointer;
            color: #2c3e50;
            font-size: 1.2rem;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.2s ease;
        }
        
        .audio-button:hover {
            background-color: rgba(44, 62, 80, 0.1);
        }
        
        .audio-button.active {
            background-color: #2c3e50;
            color: white;
        }
        
        .progress-container {
            flex-grow: 1;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            min-width: 200px;
        }
        
        .audio-progress {
            flex-grow: 1;
            height: 4px;
            background-color: #ddd;
            border-radius: 2px;
            cursor: pointer;
            position: relative;
        }
        
        .progress-fill {
            height: 100%;
            background-color: #2c3e50;
            border-radius: 2px;
            width: 0%;
            transition: width 0.1s ease;
        }
        
        .progress-handle {
            position: absolute;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background-color: #2c3e50;
            top: 50%;
            transform: translateY(-50%);
            left: 0%;
            display: none;
            transition: left 0.1s ease;
            box-shadow: 0 0 5px rgba(0,0,0,0.2);
        }
        
        .audio-progress:hover .progress-handle {
            display: block;
        }
        
        .time-display {
            font-size: 0.8rem;
            color: #666;
            width: 40px;
            text-align: center;
        }
        
        .time-separator {
            font-size: 0.8rem;
            color: #666;
            width: 10px;
            text-align: center;
        }
        
        .volume-control {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            margin-left: 0.5rem;
            min-width: 100px;
        }
        
        .volume-button {
            background: none;
            border: none;
            cursor: pointer;
            color: #2c3e50;
            font-size: 1rem;
            transition: all 0.2s ease;
        }
        
        .volume-slider {
            width: 80px;
            height: 4px;
            -webkit-appearance: none;
            appearance: none;
            background: #ddd;
            border-radius: 2px;
            outline: none;
        }
        
        .volume-slider::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: #2c3e50;
            cursor: pointer;
        }
        
        .download-link {
            display: inline-block;
            margin-top: 1rem;
            color: #2c3e50;
            text-decoration: none;
            padding: 0.5rem 1rem;
            border: 1px solid #2c3e50;
            border-radius: 20px;
            transition: all 0.3s ease;
        }
        
        .download-link:hover {
            background-color: #2c3e50;
            color: white;
        }
        
        @media (max-width: 768px) {
            .container {
                padding: 1rem;
            }
            
            .volume-control {
                margin-top: 0.5rem;
                width: 100%;
                justify-content: center;
                order: 3;
            }
            
            .progress-container {
                order: 2;
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <p>
            <a href="index.php#download-<?php echo $audioInfo['id']; ?>" class="download-link" target="_blank">
                <i class="fas fa-download"></i> 前往主站-查看更多音乐
            </a>
        </p>
        
        <h1><?php echo htmlspecialchars($audioInfo['title']); ?></h1>
        <p class="artist">作者: <?php echo htmlspecialchars($audioInfo['artist']); ?></p>
        
        <div class="custom-audio-player">
            <div class="audio-controls">
                <button class="audio-button play-pause" data-audio="<?php echo $audioInfo['id']; ?>">▶</button>
                <button class="audio-button loop" data-audio="<?php echo $audioInfo['id']; ?>">🔄</button>
            </div>
            <div class="progress-container">
                <div class="audio-progress" data-audio="<?php echo $audioInfo['id']; ?>">
                    <div class="progress-fill" data-audio="<?php echo $audioInfo['id']; ?>"></div>
                    <div class="progress-handle" data-audio="<?php echo $audioInfo['id']; ?>"></div>
                </div>
                <span class="time-display current-time" data-audio="<?php echo $audioInfo['id']; ?>">0:00</span>
                <span class="time-separator">/</span>
                <span class="time-display total-time" data-audio="<?php echo $audioInfo['id']; ?>"><?php echo $audioInfo['duration']; ?></span>
            </div>
            <div class="volume-control" data-audio="<?php echo $audioInfo['id']; ?>">
                <button class="volume-button" data-audio="<?php echo $audioInfo['id']; ?>">🔊</button>
                <input type="range" class="volume-slider" data-audio="<?php echo $audioInfo['id']; ?>" min="0" max="1" step="0.05" value="1">
            </div>
        </div>
        
        <audio id="<?php echo $audioInfo['id']; ?>" class="audio-element" src="<?php echo htmlspecialchars($audioInfo['mp3']); ?>" type="audio/mpeg">
            您的浏览器不支持音频播放
        </audio>
        
        <div class="video-container">
            <iframe 
                src="https://player.bilibili.com/player.html?bvid=<?php echo $music['bvid']; ?>&page=1" 
                allowfullscreen="true"
                loading="lazy"> 
            </iframe>
        </div>
        
        <div class="download-section">
            <p>下载：
                <a href="<?php echo htmlspecialchars($music['mp3']); ?>" class="download-link" download>点我下载</a>
            </p>
        </div>
    </div>

    <script>
        // 音频播放器功能
        (function(d, w) {
            var q = d.querySelector.bind(d);
            var addEvt = function(el, evt, fn) {
                el.addEventListener(evt, fn);
            };
            
            // 初始化播放器
            function initAudioPlayer() {
                var audioId = "<?php echo $audioInfo['id']; ?>";
                var audio = q('#' + audioId);
                var playBtn = q('.play-pause[data-audio="' + audioId + '"]');
                var loopBtn = q('.loop[data-audio="' + audioId + '"]');
                var progressBar = q('.audio-progress[data-audio="' + audioId + '"]');
                var progressFill = q('.progress-fill[data-audio="' + audioId + '"]');
                var progressHandle = q('.progress-handle[data-audio="' + audioId + '"]');
                var currentTime = q('.current-time[data-audio="' + audioId + '"]');
                var totalTime = q('.total-time[data-audio="' + audioId + '"]');
                var volumeBtn = q('.volume-button[data-audio="' + audioId + '"]');
                var volumeSlider = q('.volume-slider[data-audio="' + audioId + '"]');
                
                var originalVolume = 1;
                var isDragging = false;
                
                // 音频加载完成
                addEvt(audio, 'loadedmetadata', function() {
                    totalTime.textContent = formatTime(audio.duration);
                    audio.volume = volumeSlider.value;
                });
                
                // 播放/暂停
                addEvt(playBtn, 'click', function() {
                    if (audio.paused) {
                        audio.play();
                        playBtn.textContent = '⏸';
                    } else {
                        audio.pause();
                        playBtn.textContent = '▶';
                    }
                });
                
                // 循环播放
                addEvt(loopBtn, 'click', function() {
                    audio.loop = !audio.loop;
                    loopBtn.classList.toggle('active', audio.loop);
                });
                
                // 更新进度
                addEvt(audio, 'timeupdate', function() {
                    if (!isDragging) {
                        var progress = (audio.currentTime / audio.duration) * 100;
                        progressFill.style.width = progress + '%';
                        progressHandle.style.left = progress + '%';
                        currentTime.textContent = formatTime(audio.currentTime);
                    }
                });
                
                // 进度条拖动
                var startDrag = function(e) {
                    isDragging = true;
                    updateProgress(e);
                    
                    addEvt(d, 'mousemove', dragProgress);
                    addEvt(d, 'touchmove', dragProgress);
                    addEvt(d, 'mouseup', endDrag);
                    addEvt(d, 'touchend', endDrag);
                };
                
                var dragProgress = function(e) {
                    if (isDragging) updateProgress(e);
                };
                
                var endDrag = function() {
                    isDragging = false;
                    var rect = progressBar.getBoundingClientRect();
                    var pos = Math.min(Math.max(0, progressHandle.offsetLeft / rect.width), 1);
                    audio.currentTime = pos * audio.duration;
                    
                    d.removeEventListener('mousemove', dragProgress);
                    d.removeEventListener('touchmove', dragProgress);
                };
                
                var updateProgress = function(e) {
                    var rect = progressBar.getBoundingClientRect();
                    var clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
                    var pos = (clientX - rect.left) / rect.width;
                    pos = Math.min(Math.max(0, pos), 1);
                    
                    var percent = pos * 100;
                    progressFill.style.width = percent + '%';
                    progressHandle.style.left = percent + '%';
                    currentTime.textContent = formatTime(pos * audio.duration);
                };
                
                addEvt(progressBar, 'mousedown', startDrag);
                addEvt(progressBar, 'touchstart', startDrag);
                
                // 音量控制
                addEvt(volumeSlider, 'input', function() {
                    audio.volume = this.value;
                    updateVolumeIcon();
                    if (audio.muted && this.value > 0) audio.muted = false;
                });
                
                // 静音切换
                addEvt(volumeBtn, 'click', function() {
                    audio.muted = !audio.muted;
                    updateVolumeIcon();
                    
                    if (!audio.muted) {
                        volumeSlider.value = originalVolume;
                        audio.volume = originalVolume;
                    } else {
                        originalVolume = volumeSlider.value;
                        volumeSlider.value = 0;
                    }
                });
                
                // 更新音量图标
                function updateVolumeIcon() {
                    if (audio.muted || volumeSlider.value == 0) {
                        volumeBtn.textContent = '🔇';
                    } else if (volumeSlider.value < 0.5) {
                        volumeBtn.textContent = '🔈';
                    } else {
                        volumeBtn.textContent = '🔊';
                    }
                }
                
                // 播放结束
                addEvt(audio, 'ended', function() {
                    playBtn.textContent = '▶';
                });
            }
            
            // 格式化时间
            function formatTime(seconds) {
                if (isNaN(seconds)) return '0:00';
                var mins = Math.floor(seconds / 60);
                var secs = Math.floor(seconds % 60);
                return mins + ':' + (secs < 10 ? '0' + secs : secs);
            }
            
            // 页面加载完成后初始化
            addEvt(d, 'DOMContentLoaded', function() {
                initAudioPlayer();
            });
        })(document, window);
    </script>
</body>
</html>
    