<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-5-20
 */

$page = max(1, intval($_GET['p'] ?? 1));
$pageSize = 15;
$offset = ($page - 1) * $pageSize;
$memberId = intval($_SESSION['member_id'] ?? 0);

$totalRow = $db->fetch(
    "SELECT COUNT(*) AS cnt FROM {$db->table('balance_log')} WHERE member_id = ?",
    [$memberId]
);
$total = intval($totalRow['cnt'] ?? 0);
$rows = $db->fetchAll(
    "SELECT * FROM {$db->table('balance_log')} WHERE member_id = ? ORDER BY id DESC LIMIT {$offset}, {$pageSize}",
    [$memberId]
);
$totalPages = max(1, (int) ceil($total / $pageSize));
?>
<div class="page-header">
    <h1 class="page-title">
        <i class="icon-dollar-sign"></i>
        财务明细
    </h1>
</div>
<div class="page-content">
    <?php if (empty($rows)): ?>
    <div class="empty-state">
        <div class="empty-state-icon">&#128176;</div>
        <div class="empty-state-text">暂无财务明细记录</div>
    </div>
    <?php else: ?>
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>变动金额</th>
                <th>类型</th>
                <th>说明</th>
                <th>时间</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $row): ?>
            <?php
            $amount = (float) ($row['amount'] ?? 0);
            $note = ($row['note'] ?? '') !== '' ? $row['note'] : ($row['remark'] ?? '');
            $time = intval(($row['add_time'] ?? 0) ?: ($row['create_time'] ?? 0));
            ?>
            <tr>
                <td><?php echo intval($row['id'] ?? 0); ?></td>
                <td style="color: <?php echo $amount >= 0 ? '#10b981' : '#ef4444'; ?>;">
                    <?php echo $amount >= 0 ? '+' : ''; ?>&yen;<?php echo number_format($amount, 2); ?>
                </td>
                <td><?php echo htmlspecialchars((string) ($row['type'] ?? '-')); ?></td>
                <td><?php echo htmlspecialchars((string) $note); ?></td>
                <td><?php echo $time > 0 ? date('Y-m-d H:i', $time) : '-'; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

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