<?php
// 正常页面显示
$user_id = $_SESSION['user_id'];
$user = currentUser();

$page = isset($_GET['p']) ? intval($_GET['p']) : 1;
$perpage = 12;
$search = isset($_GET['search']) ? trim($_GET['search']) : '';

$where = "status = 1";
if ($search) {
    $search = $db->escape($search);
    $where .= " AND (title LIKE '%$search%' OR description LIKE '%$search%')";
}

$total_result = $db->query("SELECT COUNT(*) as cnt FROM downloads WHERE $where");
$total = $total_result->fetch_assoc()['cnt'];
$offset = ($page - 1) * $perpage;

$downloads = $db->fetchAll("
    SELECT * FROM downloads 
    WHERE $where 
    ORDER BY sort ASC, id DESC 
    LIMIT $offset, $perpage
");
?>

<div class="downloads-container">
    <div class="page-header">
        <h2>资源下载</h2>
        <p>最新版本、安装包、文档资源下载</p>
    </div>
    
    <!-- 搜索栏 -->
    <div class="search-bar">
        <form method="GET">
            <input type="hidden" name="page" value="downloads">
            <input type="text" name="search" placeholder="搜索资源..." value="<?php echo htmlspecialchars($search); ?>">
            <button type="submit" class="btn btn-primary">搜索</button>
            <?php if ($search): ?>
                <a href="?page=downloads" class="btn btn-secondary">清空</a>
            <?php endif; ?>
        </form>
    </div>
    
    <?php if (empty($downloads)): ?>
        <div class="empty-state">
            <div class="empty-icon">📦</div>
            <p>暂无可用资源</p>
        </div>
    <?php else: ?>
        <div class="downloads-grid">
            <?php foreach ($downloads as $item): ?>
                <div class="download-card">
                    <div class="download-icon">
                        <?php
                        $ext = strtolower(pathinfo($item['file_name'], PATHINFO_EXTENSION));
                        switch ($ext) {
                            case 'zip':
                            case 'rar':
                            case '7z':
                                echo '📦';
                                break;
                            case 'exe':
                            case 'msi':
                                echo '⚙️';
                                break;
                            case 'pdf':
                            case 'doc':
                            case 'docx':
                                echo '📄';
                                break;
                            case 'jpg':
                            case 'jpeg':
                            case 'png':
                            case 'gif':
                                echo '🖼️';
                                break;
                            case 'mp4':
                            case 'avi':
                            case 'mov':
                                echo '🎬';
                                break;
                            default:
                                echo '📁';
                        }
                        ?>
                    </div>
                    <div class="download-info">
                        <h3><?php echo htmlspecialchars($item['title']); ?></h3>
                        <p class="description"><?php echo htmlspecialchars(mb_substr($item['description'], 0, 80)); ?></p>
                        <div class="download-meta">
                            <span>📄 <?php echo htmlspecialchars($item['file_name']); ?></span>
                            <span>💾 <?php echo $item['file_size']; ?></span>
                            <?php if ($item['version']): ?>
                                <span>🏷️ v<?php echo htmlspecialchars($item['version']); ?></span>
                            <?php endif; ?>
                            <span>📥 下载 <?php echo $item['download_count']; ?> 次</span>
                        </div>
                        <div class="download-footer">
                            <a href="/api/download.php?id=<?php echo $item['id']; ?>" class="btn btn-primary btn-sm">
                                ⬇️ 立即下载
                            </a>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
    
    <!-- 分页 -->
    <?php if ($total > $perpage): ?>
        <div class="pagination">
            <?php
            $total_pages = ceil($total / $perpage);
            for ($i = 1; $i <= $total_pages; $i++):
            ?>
                <a href="?page=downloads&p=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>" class="page-link <?php echo $i == $page ? 'active' : ''; ?>">
                    <?php echo $i; ?>
                </a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
</div>

<style>
.downloads-container .page-header {
    margin-bottom: 20px;
}
.downloads-container .page-header p {
    color: var(--text-secondary);
    font-size: 14px;
}
.search-bar {
    margin-bottom: 30px;
}
.search-bar form {
    display: flex;
    gap: 10px;
    max-width: 500px;
}
.search-bar input {
    flex: 1;
    padding: 10px 16px;
    border: 1px solid var(--border-color);
    border-radius: 30px;
    background: var(--bg-primary);
}
.downloads-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 20px;
}
.download-card {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 20px;
    display: flex;
    gap: 15px;
    transition: all 0.3s;
}
.download-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-md);
    border-color: var(--primary-light);
}
.download-icon {
    width: 60px;
    height: 60px;
    background: var(--bg-secondary);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    flex-shrink: 0;
}
.download-info {
    flex: 1;
}
.download-info h3 {
    font-size: 18px;
    margin-bottom: 8px;
}
.description {
    color: var(--text-secondary);
    font-size: 13px;
    line-height: 1.5;
    margin-bottom: 12px;
}
.download-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    font-size: 11px;
    color: var(--text-muted);
    margin-bottom: 15px;
}
.download-meta span {
    background: var(--bg-secondary);
    padding: 2px 8px;
    border-radius: 20px;
}
.btn-sm {
    padding: 6px 16px;
    font-size: 13px;
    border-radius: 30px;
}
.empty-state {
    text-align: center;
    padding: 80px 20px;
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
}
.empty-icon {
    font-size: 64px;
    margin-bottom: 20px;
    opacity: 0.5;
}
.pagination {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin-top: 30px;
}
.page-link {
    padding: 8px 14px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    text-decoration: none;
    color: var(--text-primary);
}
.page-link.active {
    background: var(--primary-color);
    color: white;
}
@media (max-width: 768px) {
    .downloads-grid {
        grid-template-columns: 1fr;
    }
    .download-card {
        flex-direction: column;
        text-align: center;
    }
    .download-icon {
        margin: 0 auto;
    }
}
</style>