<?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
");

// 统计信息
$stats = $db->fetchOne("
    SELECT 
        COALESCE(SUM(CASE WHEN amount > 0 THEN amount ELSE 0 END), 0) as total_income,
        COALESCE(SUM(CASE WHEN amount < 0 THEN ABS(amount) ELSE 0 END), 0) as total_expense
    FROM balance_logs WHERE user_id = $user_id
");
?>

<div class="user-balance">
    <div class="page-header">
        <h2>
            <img src="https://api.iconify.design/mdi:cash-multiple.svg?color=%23667eea" width="26" height="26" style="vertical-align: middle; margin-right: 10px;" alt="">
            用户余额明细
        </h2>
        <div class="header-actions">
            <a href="?page=user_detail&id=<?php echo $user_id; ?>" class="btn btn-secondary">
                <img src="https://api.iconify.design/mdi:arrow-left.svg?color=currentColor" width="18" height="18" style="vertical-align: middle; margin-right: 6px;" alt="">
                返回用户详情
            </a>
            <a href="?page=users" class="btn btn-secondary">
                <img src="https://api.iconify.design/mdi:account-group.svg?color=currentColor" width="18" height="18" style="vertical-align: middle; margin-right: 6px;" alt="">
                返回用户列表
            </a>
        </div>
    </div>
    
    <!-- 用户信息卡片 -->
    <div class="user-info-card">
        <div class="user-profile">
            <div class="user-avatar">
                <img src="https://api.iconify.design/mdi:account-circle.svg?color=%23667eea" width="48" height="48" alt="">
            </div>
            <div class="user-details">
                <h3><?php echo htmlspecialchars($user['username']); ?></h3>
                <span class="user-email">
                    <img src="https://api.iconify.design/mdi:email.svg?color=%236b7280" width="14" height="14" style="margin-right: 4px;" alt="">
                    <?php echo htmlspecialchars($user['email']); ?>
                </span>
            </div>
        </div>
        <div class="balance-info">
            <div class="balance-label">
                <img src="https://api.iconify.design/mdi:wallet.svg?color=%236b7280" width="18" height="18" style="margin-right: 6px;" alt="">
                当前余额
            </div>
            <div class="balance-value">¥<?php echo number_format($user['balance'], 2); ?></div>
        </div>
    </div>
    
    <!-- 统计卡片 -->
    <div class="stats-row">
        <div class="stat-mini-card income">
            <div class="stat-mini-icon">
                <img src="https://api.iconify.design/mdi:arrow-down-circle.svg?color=%2310b981" width="24" height="24" alt="">
            </div>
            <div class="stat-mini-content">
                <span class="stat-mini-label">累计收入</span>
                <span class="stat-mini-value">+¥<?php echo number_format($stats['total_income'], 2); ?></span>
            </div>
        </div>
        <div class="stat-mini-card expense">
            <div class="stat-mini-icon">
                <img src="https://api.iconify.design/mdi:arrow-up-circle.svg?color=%23ef4444" width="24" height="24" alt="">
            </div>
            <div class="stat-mini-content">
                <span class="stat-mini-label">累计支出</span>
                <span class="stat-mini-value">-¥<?php echo number_format($stats['total_expense'], 2); ?></span>
            </div>
        </div>
    </div>
    
    <!-- 明细列表 -->
    <div class="table-wrapper">
        <table class="data-table">
            <thead>
                <tr>
                    <th style="width: 25%;">时间</th>
                    <th style="width: 20%;">金额</th>
                    <th style="width: 20%;">类型</th>
                    <th style="width: 35%;">备注</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($logs)): ?>
                    <tr>
                        <td colspan="4">
                            <div class="empty-state">
                                <img src="https://api.iconify.design/mdi:cash-remove.svg?color=%239ca3af" width="48" height="48" alt="">
                                <p>暂无余额变动记录</p>
                            </div>
                        </td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($logs as $log): ?>
                        <tr>
                            <td>
                                <div class="datetime-cell">
                                    <img src="https://api.iconify.design/mdi:clock-outline.svg?color=%236b7280" width="14" height="14" style="margin-right: 6px;" alt="">
                                    <?php echo date('Y-m-d H:i:s', strtotime($log['created_at'])); ?>
                                </div>
                            </td>
                            <td>
                                <span class="amount-<?php echo $log['amount'] > 0 ? 'positive' : 'negative'; ?>">
                                    <?php echo $log['amount'] > 0 ? '+' : ''; ?>¥<?php echo number_format($log['amount'], 2); ?>
                                </span>
                            </td>
                            <td>
                                <?php 
                                    $type_map = [
                                        'recharge' => ['icon' => 'plus-circle', 'color' => '10b981', 'label' => '充值'],
                                        'consume' => ['icon' => 'minus-circle', 'color' => 'f59e0b', 'label' => '消费'],
                                        'refund' => ['icon' => 'refresh', 'color' => '667eea', 'label' => '退款']
                                    ];
                                    $type = $type_map[$log['type']] ?? ['icon' => 'circle', 'color' => '6b7280', 'label' => $log['type']];
                                ?>
                                <span class="type-badge type-<?php echo $log['type']; ?>">
                                    <img src="https://api.iconify.design/mdi:<?php echo $type['icon']; ?>.svg?color=%23<?php echo $type['color']; ?>" width="14" height="14" style="margin-right: 4px;" alt="">
                                    <?php echo $type['label']; ?>
                                </span>
                            </td>
                            <td><?php echo htmlspecialchars($log['remark'] ?: '-'); ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <!-- 分页 -->
    <?php if ($total > $perpage): ?>
        <div class="pagination">
            <?php if ($page > 1): ?>
                <a href="?page=user_balance&id=<?php echo $user_id; ?>&p=<?php echo $page - 1; ?>" class="page-link page-prev">
                    <img src="https://api.iconify.design/mdi:chevron-left.svg?color=currentColor" width="16" height="16" alt="">
                </a>
            <?php endif; ?>
            
            <?php
            $total_pages = ceil($total / $perpage);
            $start_page = max(1, $page - 2);
            $end_page = min($total_pages, $page + 2);
            
            if ($start_page > 1): ?>
                <a href="?page=user_balance&id=<?php echo $user_id; ?>&p=1" class="page-link">1</a>
                <?php if ($start_page > 2): ?>
                    <span class="page-ellipsis">...</span>
                <?php endif; ?>
            <?php endif; ?>
            
            <?php for ($i = $start_page; $i <= $end_page; $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; ?>
            
            <?php if ($end_page < $total_pages): ?>
                <?php if ($end_page < $total_pages - 1): ?>
                    <span class="page-ellipsis">...</span>
                <?php endif; ?>
                <a href="?page=user_balance&id=<?php echo $user_id; ?>&p=<?php echo $total_pages; ?>" class="page-link"><?php echo $total_pages; ?></a>
            <?php endif; ?>
            
            <?php if ($page < $total_pages): ?>
                <a href="?page=user_balance&id=<?php echo $user_id; ?>&p=<?php echo $page + 1; ?>" class="page-link page-next">
                    <img src="https://api.iconify.design/mdi:chevron-right.svg?color=currentColor" width="16" height="16" alt="">
                </a>
            <?php endif; ?>
        </div>
    <?php endif; ?>
</div>

<style>
/* 基础变量 */
:root {
    --primary-color: #667eea;
    --primary-hover: #5a67d8;
    --danger-color: #ef4444;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --bg-primary: #ffffff;
    --bg-secondary: #f9fafb;
    --text-primary: #1f2937;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --border-color: #e5e7eb;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
}

.user-balance {
    max-width: 1000px;
    margin: 0 auto;
    padding: 4px;
}

/* 页面头部 */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    flex-wrap: wrap;
    gap: 16px;
}
.page-header h2 {
    display: flex;
    align-items: center;
    font-size: 24px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}
.header-actions {
    display: flex;
    gap: 10px;
}

/* 按钮 */
.btn {
    display: inline-flex;
    align-items: center;
    padding: 10px 20px;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    transition: all 0.2s;
    border: 1px solid transparent;
}
.btn-secondary {
    background: var(--bg-primary);
    color: var(--text-primary);
    border-color: var(--border-color);
}
.btn-secondary:hover {
    background: var(--bg-secondary);
    border-color: var(--text-muted);
}

/* 用户信息卡片 */
.user-info-card {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 24px 28px;
    margin-bottom: 24px;
    border: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 20px;
    box-shadow: var(--shadow-sm);
}
.user-profile {
    display: flex;
    align-items: center;
    gap: 16px;
}
.user-avatar img {
    display: block;
}
.user-details h3 {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 0 4px;
}
.user-email {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: var(--text-muted);
}
.balance-info {
    text-align: right;
}
.balance-label {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    font-size: 14px;
    color: var(--text-muted);
    margin-bottom: 6px;
}
.balance-value {
    font-size: 32px;
    font-weight: 700;
    color: var(--primary-color);
    line-height: 1.2;
}

/* 统计卡片行 */
.stats-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 24px;
}
.stat-mini-card {
    background: var(--bg-primary);
    border-radius: var(--radius-md);
    padding: 16px 20px;
    border: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 14px;
    transition: all 0.2s;
    box-shadow: var(--shadow-sm);
}
.stat-mini-card:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}
.stat-mini-icon {
    width: 44px;
    height: 44px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
}
.stat-mini-card.income .stat-mini-icon {
    background: rgba(16, 185, 129, 0.1);
}
.stat-mini-card.expense .stat-mini-icon {
    background: rgba(239, 68, 68, 0.1);
}
.stat-mini-content {
    display: flex;
    flex-direction: column;
}
.stat-mini-label {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 4px;
}
.stat-mini-value {
    font-size: 20px;
    font-weight: 700;
}
.stat-mini-card.income .stat-mini-value {
    color: #10b981;
}
.stat-mini-card.expense .stat-mini-value {
    color: #ef4444;
}

/* 表格 */
.table-wrapper {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
}
.data-table {
    width: 100%;
    border-collapse: collapse;
}
.data-table th {
    padding: 16px 20px;
    text-align: left;
    font-weight: 600;
    font-size: 13px;
    color: var(--text-secondary);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    text-transform: uppercase;
    letter-spacing: 0.3px;
}
.data-table td {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
    font-size: 14px;
    vertical-align: middle;
}
.data-table tbody tr {
    transition: background 0.2s;
}
.data-table tbody tr:hover {
    background: var(--bg-secondary);
}
.data-table tbody tr:last-child td {
    border-bottom: none;
}
.datetime-cell {
    display: flex;
    align-items: center;
    color: var(--text-secondary);
}
.amount-positive {
    color: #10b981;
    font-weight: 600;
    font-size: 15px;
}
.amount-negative {
    color: #ef4444;
    font-weight: 600;
    font-size: 15px;
}
.type-badge {
    display: inline-flex;
    align-items: center;
    padding: 4px 12px;
    border-radius: 30px;
    font-size: 12px;
    font-weight: 500;
}
.type-recharge {
    background: #d1fae5;
    color: #065f46;
}
.type-consume {
    background: #fef3c7;
    color: #92400e;
}
.type-refund {
    background: #e0e7ff;
    color: #4338ca;
}

/* 空状态 */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 20px;
    text-align: center;
}
.empty-state img {
    opacity: 0.4;
    margin-bottom: 12px;
}
.empty-state p {
    color: var(--text-muted);
    font-size: 15px;
    margin: 0;
}

/* 分页 */
.pagination {
    display: flex;
    gap: 6px;
    justify-content: center;
    margin-top: 28px;
}
.page-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 38px;
    height: 38px;
    padding: 0 8px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    text-decoration: none;
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 500;
    background: var(--bg-primary);
    transition: all 0.2s;
}
.page-link:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
    background: rgba(102, 126, 234, 0.05);
}
.page-link.active {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}
.page-ellipsis {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 38px;
    height: 38px;
    color: var(--text-muted);
}

/* 响应式 */
@media (max-width: 768px) {
    .user-info-card {
        flex-direction: column;
        align-items: flex-start;
    }
    .balance-info {
        text-align: left;
        width: 100%;
    }
    .balance-label {
        justify-content: flex-start;
    }
    .stats-row {
        grid-template-columns: 1fr;
    }
    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }
    .header-actions {
        width: 100%;
    }
    .header-actions .btn {
        flex: 1;
        justify-content: center;
    }
    .data-table th,
    .data-table td {
        padding: 12px 14px;
    }
}
</style>