<?php
/**
 * 我的下载
 */

$page = max(1, intval($_GET['p'] ?? 1));
$page_size = 10;
$offset = ($page - 1) * $page_size;

// 下载记录表
$total = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('download_log')} WHERE member_id = ?", [$_SESSION['member_id']])['cnt'];
$rows = $db->fetchAll("SELECT l.*, d.title, d.file_size FROM {$db->table('download_log')} l LEFT JOIN {$db->table('download')} d ON l.download_id = d.id WHERE l.member_id = ? ORDER BY l.id DESC LIMIT {$offset}, {$page_size}", [$_SESSION['member_id']]);

$total_pages = ceil($total / $page_size);
?>
<div class="page-header">
    <h1 class="page-title">
        <i class="icon-download"></i>
        我的下载
    </h1>
</div>
<div class="page-content">
    <?php if (empty($rows)): ?>
    <div class="empty-state">
        <div class="empty-state-icon">📥</div>
        <div class="empty-state-text">暂无下载记录</div>
    </div>
    <?php else: ?>
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>资源名称</th>
                <th>文件大小</th>
                <th>下载时间</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $row): ?>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo htmlspecialchars($row['title'] ?? '未知'); ?></td>
                <td><?php echo $row['file_size'] ?? '-'; ?></td>
                <td><?php echo date('Y-m-d H:i', $row['add_time']); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <?php if ($total_pages > 1): ?>
    <div class="pagination">
        <?php if ($page > 1): ?><a href="?page=mydownload&p=<?php echo $page - 1; ?>">上一页</a><?php endif; ?>
        <?php for ($i = 1; $i <= $total_pages; $i++): ?>
        <a href="?page=mydownload&p=<?php echo $i; ?>" class="<?php echo $i === $page ? 'current' : ''; ?>"><?php echo $i; ?></a>
        <?php endfor; ?>
        <?php if ($page < $total_pages): ?><a href="?page=mydownload&p=<?php echo $page + 1; ?>">下一页</a><?php endif; ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>
</div>