<?php
require_once __DIR__ . '/layout.php';

$db = db();

if (!hasPermission('view_orders')) {
    echo "权限不足";
    exit;
}

$title = '财务明细';
ob_start();

$user_id = isset($_GET['user_id']) ? (int)$_GET['user_id'] : null;
$type = $_GET['type'] ?? '';

$sql = "SELECT t.*, u.username, u.uid FROM transactions t JOIN users u ON t.user_id = u.id";
$params = [];

if ($admin['is_agent']) {
    $sql .= " WHERE u.agent_id = ?";
    $params[] = $admin['id'];
}

if ($user_id) {
    $sql .= (strpos($sql, 'WHERE') === false ? " WHERE" : " AND") . " t.user_id = ?";
    $params[] = $user_id;
}

if ($type) {
    $sql .= (strpos($sql, 'WHERE') === false ? " WHERE" : " AND") . " t.type = ?";
    $params[] = $type;
}

$sql .= " ORDER BY t.created_at DESC";

$stmt = $db->prepare($sql);
$stmt->execute($params);
$transactions = $stmt->fetchAll();

$types = [
    'recharge' => '充值',
    'deposit' => '充值',
    'withdrawal' => '提现',
    'mining_profit' => '矿机收益',
    'binary_win' => '秒合约盈利',
    'binary_loss' => '秒合约亏损',
    'contract_profit' => '合约盈利',
    'contract_loss' => '合约亏损',
    'contract_margin' => '合约保证金',
    'contract_settle' => '合约结算',
    'spot_buy' => '币币买入',
    'spot_sell' => '币币卖出',
    'swap' => '闪兑'
];
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div class="d-flex align-items-center gap-3">
        <a href="<?= $user_id ? 'users.php' : 'index.php' ?>" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left"></i> 返回</a>
        <h4 class="mb-0">账变记录 <?= $user_id ? "(用户ID: $user_id)" : "" ?></h4>
    </div>
    <div class="d-flex gap-2">
        <select class="form-select form-select-sm" onchange="location.href='?type=' + this.value + '<?= $user_id ? "&user_id=$user_id" : "" ?>'">
            <option value="">所有类型</option>
            <?php foreach($types as $k => $v): ?>
                <option value="<?= $k ?>" <?= $type === $k ? 'selected' : '' ?>><?= $v ?></option>
            <?php endforeach; ?>
        </select>
    </div>
</div>

<div class="table-container">
    <table class="table table-hover align-middle">
        <thead>
            <tr class="text-muted small">
                <th>编号</th>
                <th>用户信息</th>
                <th>类型</th>
                <th>币种</th>
                <th>金额</th>
                <th>时间</th>
                <th>状态</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($transactions as $t): ?>
            <tr>
                <td><?= $t['id'] ?></td>
                <td>
                    <div><?= htmlspecialchars($t['username']) ?></div>
                    <code class="small"><?= $t['uid'] ?></code>
                </td>
                <td>
                    <?php
                    $badgeClass = 'bg-secondary';
                    if (strpos($t['type'], 'win') !== false || strpos($t['type'], 'profit') !== false || $t['type'] === 'recharge') $badgeClass = 'bg-success';
                    if (strpos($t['type'], 'loss') !== false || $t['type'] === 'withdrawal') $badgeClass = 'bg-danger';
                    ?>
                    <span class="badge <?= $badgeClass ?>"><?= $types[$t['type']] ?? $t['type'] ?></span>
                </td>
                <td><span class="fw-bold"><?= $t['symbol'] ?></span></td>
                <td>
                    <span class="fw-bold <?= (strpos($t['type'], 'win') !== false || strpos($t['type'], 'profit') !== false || $t['type'] === 'recharge') ? 'text-success' : 'text-danger' ?>">
                        <?= (strpos($t['type'], 'win') !== false || strpos($t['type'], 'profit') !== false || $t['type'] === 'recharge') ? '+' : '-' ?>
                        <?= number_format($t['amount'], 2) ?>
                    </span>
                </td>
                <td><small class="text-muted"><?= $t['created_at'] ?></small></td>
                <td>
                    <span class="badge bg-opacity-10 <?= $t['status'] === 'completed' ? 'bg-success text-success' : 'bg-warning text-warning' ?>">
                        <?= $t['status'] === 'completed' ? '已完成' : '待处理' ?>
                    </span>
                </td>
            </tr>
            <?php endforeach; ?>
            <?php if (empty($transactions)): ?>
            <tr><td colspan="7" class="text-center p-5 text-muted">暂无账变记录</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<?php
$content = ob_get_clean();
renderAdminPage($content, $title);
?>
