<?php
require_once 'config.php';

// 格式化文件大小函数
function formatFileSize($bytes) {
    if ($bytes === false) return '未知';
    if ($bytes >= 1073741824) {
        return number_format($bytes / 1073741824, 2) . ' GB';
    } elseif ($bytes >= 1048576) {
        return number_format($bytes / 1048576, 2) . ' MB';
    } elseif ($bytes >= 1024) {
        return number_format($bytes / 1024, 2) . ' KB';
    } elseif ($bytes > 0) {
        return $bytes . ' B';
    } else {
        return '0 B';
    }
}


// 验证App ID
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header('Location: index.php?error=无效的App ID');
    exit;
}
$appId = $_GET['id'];

// 获取App信息
$app = null;
$getAppSql = "SELECT * FROM apps WHERE id = ?";
$stmt = $conn->prepare($getAppSql);
$stmt->bind_param("i", $appId);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
    header('Location: index.php?error=App不存在');
    exit;
}
$app = $result->fetch_assoc();

// 获取所有版本
$versions = [];
$getVersionsSql = "SELECT * FROM app_versions WHERE app_id = ? ORDER BY created_at DESC";
$stmt = $conn->prepare($getVersionsSql);
$stmt->bind_param("i", $appId);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    $versions[] = $row;
}
?>
<!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($app['name']); ?> - 版本历史</title>
    <!-- Bootstrap CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 自定义CSS -->
    <link rel="stylesheet" href="styles.css">
    <!-- Marked.js 用于Markdown解析 -->
    <script src="js/marked.js"></script>
    <style>
        .version-card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .version-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        .download-btn {
            background-color: #0d6efd;
            border-color: #0d6efd;
        }
        .download-btn:hover {
            background-color: #0b5ed7;
            border-color: #0a58ca;
        }
        
        .page-transition {
            animation: fadeIn 0.5s ease-in-out;
        }
        
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
    </style>
</head>
<body class="page-transition">
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <div class="container">
            <a href="index.php"><img src="/favicon.jpeg" alt="Logo" style="height: 30px; margin-right: 10px; border-radius: var(--border-radius);"></a>
            <a class="navbar-brand" href="index.php"><?php echo APP_STORE_NAME; ?></a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="index.php">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="app.php?id=<?php echo $appId; ?>">返回App详情</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="mb-3 form-floating">
            <input type="text" class="form-control" id="searchVersion" placeholder="搜索版本">
            <label for="searchVersion">搜索版本</label>
        </div>
        <div class="row mb-4">
            <div class="col">
                <h1><?php echo htmlspecialchars($app['name']); ?> - 版本历史</h1>
                <p class="text-muted">查看和下载该应用的所有历史版本</p>
            </div>
        </div>

        <?php if (empty($versions)): ?>
            <div class="alert alert-info">
                暂无版本记录
            </div>
        <?php else: ?>
            <div class="row">
                <?php foreach ($versions as $version): ?>
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card version-card h-100">
                            <div class="card-body">
                                <h5 class="card-title">版本 <?php echo htmlspecialchars($version['version']); ?></h5>
                                <h6 class="card-subtitle mb-2 text-muted">发布日期: <?php echo date('Y-m-d', strtotime($version['created_at'])); ?></h6>
                                <div class="card-text markdown-content"><?php echo htmlspecialchars($version['changelog']); ?></div>
                                
                            </div>
                            <div class="card-footer bg-transparent d-flex justify-content-between align-items-center">
                            <?php
// 简化路径处理以避免open_basedir限制问题
$originalFilePath = trim($version['file_path']);
$fileSize = false;

// 尝试安全地获取文件大小，处理open_basedir限制
try {
    // 首先尝试使用原始路径（相对路径通常在web环境中更安全）
    if (file_exists($originalFilePath)) {
        $fileSize = filesize($originalFilePath);
    } 
    // 尝试相对于当前脚本的路径
    else {
        $relativePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . $originalFilePath;
        if (file_exists($relativePath)) {
            $fileSize = filesize($relativePath);
        }
    }
} catch (Exception $e) {
    // 如果遇到open_basedir限制或其他安全限制，优雅地处理
    $fileSize = false;
}

$sizeText = formatFileSize($fileSize);
?>
<a href="<?php echo htmlspecialchars($version['file_path']); ?>" class="btn btn-primary" download>下载（大小：<?php echo $sizeText; ?>)</a>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>


    
    <!-- Bootstrap JS Bundle with Popper -->
    <script src="/js/bootstrap.bundle.min.js"></script>
    <script>
        // 导航栏滚动效果
        window.addEventListener('scroll', function() {
            const navbar = document.querySelector('.navbar');
            if (window.scrollY > 10) {
                navbar.classList.add('scrolled');
            } else {
                navbar.classList.remove('scrolled');
            }
        });
    </script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            document.body.classList.add('page-transition');
        });
    </script>
    <script>
        // 解析Markdown内容
        document.addEventListener('DOMContentLoaded', function() {
            const contentElements = document.querySelectorAll('.markdown-content');
            contentElements.forEach(function(element) {
                const markdown = element.textContent;
                element.innerHTML = marked.parse(markdown);
            });
        });
    </script>
</body>
</html>