<?php
ob_start();
$user_id = $_SESSION['user_id'];
$user = currentUser();
$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="balance-log-container">
    <!-- 页面头部 -->
    <div class="page-header">
        <h2>
            <img src="https://api.iconify.design/mdi:format-list-bulleted.svg?color=%23667eea" width="26" height="26" style="vertical-align: middle; margin-right: 10px;" alt="">
            余额明细
        </h2>
        <a href="?page=recharge" class="btn btn-primary">
            <img src="https://api.iconify.design/mdi:wallet-plus.svg?color=white" width="18" height="18" style="margin-right: 6px;" alt="">
            充值
        </a>
    </div>
    
    <!-- 统计卡片 -->
    <div class="stats-row">
        <div class="stat-card income">
            <div class="stat-icon">
                <img src="https://api.iconify.design/mdi:arrow-down-circle.svg?color=%2310b981" width="28" height="28" alt="">
            </div>
            <div class="stat-content">
                <span class="stat-label">累计收入</span>
                <span class="stat-value">+¥<?php echo number_format($stats['total_income'], 2); ?></span>
            </div>
        </div>
        <div class="stat-card expense">
            <div class="stat-icon">
                <img src="https://api.iconify.design/mdi:arrow-up-circle.svg?color=%23ef4444" width="28" height="28" alt="">
            </div>
            <div class="stat-content">
                <span class="stat-label">累计支出</span>
                <span class="stat-value">-¥<?php echo number_format($stats['total_expense'], 2); ?></span>
            </div>
        </div>
        <div class="stat-card balance">
            <div class="stat-icon">
                <img src="https://api.iconify.design/mdi:wallet.svg?color=%23667eea" width="28" height="28" alt="">
            </div>
            <div class="stat-content">
                <span class="stat-label">当前余额</span>
                <span class="stat-value">¥<?php echo number_format($user['balance'], 2); ?></span>
            </div>
        </div>
    </div>
    
    <!-- 明细列表 -->
    <div class="table-wrapper">
        <table class="data-table">
            <thead>
                <tr>
                    <th>时间</th>
                    <th>金额</th>
                    <th>类型</th>
                    <th>备注</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' => '退款']
                                ];
                                $t = $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 $t['icon']; ?>.svg?color=%23<?php echo $t['color']; ?>" width="14" height="14" style="margin-right: 4px;" alt="">
                                    <?php echo $t['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=balance_log&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=balance_log&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=balance_log&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=balance_log&p=<?php echo $total_pages; ?>" class="page-link"><?php echo $total_pages; ?></a>
            <?php endif; ?>
            
            <?php if ($page < $total_pages): ?>
                <a href="?page=balance_log&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;
}

.balance-log-container {
    max-width: 100%;
    padding: 4px;
}

/* 页面头部 */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 28px;
    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;
}

/* 按钮 */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 20px;
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    transition: all 0.2s;
    border: 1px solid transparent;
}
.btn-primary {
    background: var(--primary-color);
    color: white;
    box-shadow: 0 2px 4px rgba(102, 126, 234, 0.2);
}
.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

/* 统计卡片 */
.stats-row {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
    margin-bottom: 28px;
}
.stat-card {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 20px 24px;
    border: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 16px;
    transition: all 0.2s;
    box-shadow: var(--shadow-sm);
}
.stat-card:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}
.stat-icon {
    width: 52px;
    height: 52px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
}
.stat-card.income .stat-icon {
    background: rgba(16, 185, 129, 0.1);
}
.stat-card.expense .stat-icon {
    background: rgba(239, 68, 68, 0.1);
}
.stat-card.balance .stat-icon {
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(102, 126, 234, 0.05) 100%);
}
.stat-content {
    display: flex;
    flex-direction: column;
}
.stat-label {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 6px;
}
.stat-value {
    font-size: 22px;
    font-weight: 700;
}
.stat-card.income .stat-value {
    color: #10b981;
}
.stat-card.expense .stat-value {
    color: #ef4444;
}
.stat-card.balance .stat-value {
    color: var(--primary-color);
}

/* 表格 */
.table-wrapper {
    overflow-x: auto;
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}
.data-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 600px;
}
.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: 5px 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) {
    .stats-row {
        grid-template-columns: 1fr;
    }
    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }
    .data-table th,
    .data-table td {
        padding: 12px 14px;
    }
}
</style>