<?php
requireAdmin();

$user_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($user_id <= 0) {
    redirect('?page=users');
    exit;
}

$user = $db->fetchOne("SELECT id, username, email, balance FROM users WHERE id = $user_id");
if (!$user) {
    redirect('?page=users');
    exit;
}

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

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

$logs = $db->fetchAll("
    SELECT * FROM balance_logs 
    WHERE user_id = $user_id 
    ORDER BY id DESC 
    LIMIT $offset, $perpage
");
?>

<div class="user-balance">
    <div class="page-header">
        <h2>用户余额明细</h2>
        <div>
            <a href="?page=user_detail&id=<?php echo $user_id; ?>" class="btn btn-secondary">← 返回用户详情</a>
            <a href="?page=users" class="btn btn-secondary">返回用户列表</a>
        </div>
    </div>
    
    <!-- 用户信息卡片 -->
    <div class="user-info-card">
        <div class="user-info">
            <span class="label">用户：</span>
            <strong><?php echo htmlspecialchars($user['username']); ?></strong>
            <span style="margin-left: 20px;">(<?php echo htmlspecialchars($user['email']); ?>)</span>
        </div>
        <div class="user-info">
            <span class="label">当前余额：</span>
            <span class="balance">¥<?php echo number_format($user['balance'], 2); ?></span>
        </div>
    </div>
    
    <div class="table-container">
        <table class="data-table">
            <thead>
                <tr>
                    <th>时间</th>
                    <th>金额</th>
                    <th>类型</th>
                    <th>备注</th>
                </thead>
            <tbody>
                <?php if (empty($logs)): ?>
                    <tr><td colspan="4" style="text-align: center;">暂无余额变动记录</td></tr>
                <?php else: ?>
                    <?php foreach ($logs as $log): ?>
                        <tr>
                            <td><?php echo $log['created_at']; ?></td>
                            <td style="color: <?php echo $log['amount'] > 0 ? '#10b981' : '#ef4444'; ?>; font-weight: 500;">
                                <?php echo $log['amount'] > 0 ? '+' : ''; ?>¥<?php echo number_format($log['amount'], 2); ?>
                            </td>
                            <td>
                                <?php 
                                $type_map = ['recharge' => '充值', 'consume' => '消费', 'refund' => '退款'];
                                echo $type_map[$log['type']] ?? $log['type'];
                                ?>
                            </td>
                            <td><?php echo htmlspecialchars($log['remark'] ?: '-'); ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <!-- 分页 -->
    <?php if ($total > $perpage): ?>
        <div class="pagination">
            <?php
            $total_pages = ceil($total / $perpage);
            for ($i = 1; $i <= $total_pages; $i++):
            ?>
                <a href="?page=user_balance&id=<?php echo $user_id; ?>&p=<?php echo $i; ?>" class="page-link <?php echo $i == $page ? 'active' : ''; ?>">
                    <?php echo $i; ?>
                </a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
</div>

<style>
.user-balance {
    max-width: 1000px;
    margin: 0 auto;
}
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 10px;
}
.page-header .btn-secondary {
    margin-left: 10px;
}
.user-info-card {
    background: var(--bg-primary);
    border-radius: 16px;
    padding: 20px;
    margin-bottom: 20px;
    border: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
}
.user-info .label {
    color: var(--text-muted);
    font-size: 14px;
}
.user-info .balance {
    font-size: 20px;
    font-weight: bold;
    color: var(--primary-color);
}
.pagination {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin-top: 20px;
}
.page-link {
    padding: 6px 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    text-decoration: none;
    color: var(--text-primary);
}
.page-link.active {
    background: var(--primary-color);
    color: white;
}
</style>