<?php
include_once 'config.php';
check_auth();

$user_id = $_SESSION['user_id'];
$account = get_account($user_id);

// Fetch assets
$stmt = db()->prepare("SELECT * FROM assets WHERE account_id = ? AND balance > 0");
$stmt->execute([$account['id']]);
$assets = $stmt->fetchAll();

include 'header.php';
?>
<div class="container py-5">
    <div class="row">
        <!-- User Sidebar -->
        <div class="col-md-4">
            <div class="glass-card p-4 bg-dark mb-4">
                <div class="text-center mb-4">
                    <div class="mb-3">
                        <i class="bi bi-person-circle text-warning" style="font-size: 80px;"></i>
                    </div>
                    <h4 class="text-white mb-1"><?php echo htmlspecialchars($_SESSION['username']); ?></h4>
                    <span class="badge bg-warning text-dark px-3 py-2">UID: <?php echo $account['uid']; ?></span>
                </div>
                
                <div class="list-group list-group-flush bg-transparent">
                    <div class="list-group-item bg-transparent text-secondary border-secondary d-flex justify-content-between px-0">
                        <span>信用分</span>
                        <span class="text-white fw-bold"><?php echo $account['credit_score']; ?></span>
                    </div>
                    <div class="list-group-item bg-transparent text-secondary border-secondary d-flex justify-content-between px-0">
                        <span>实名认证</span>
                        <span class="text-<?php echo $account['kyc_status']=='VERIFIED'?'success':'warning'; ?>"><?php echo $account['kyc_status']; ?></span>
                    </div>
                    <div class="list-group-item bg-transparent text-secondary border-secondary d-flex justify-content-between px-0">
                        <span>注册时间</span>
                        <span class="text-white small"><?php echo substr($account['created_at'], 0, 10); ?></span>
                    </div>
                </div>
                
                <a href="logout.php" class="btn btn-outline-danger w-100 mt-4">安全退出</a>
            </div>
        </div>

        <!-- Asset Content -->
        <div class="col-md-8">
            <!-- Balance Card -->
            <div class="glass-card p-4 bg-dark mb-4" style="background: linear-gradient(135deg, #2b2f36 0%, #181a20 100%); border: 1px solid #fcd53533;">
                <div class="row align-items-center">
                    <div class="col-md-7">
                        <div class="text-secondary mb-2 small fw-bold">账户总余额 (估算)</div>
                        <h2 class="text-white fw-bold mb-0">
                            <span class="text-warning">$</span> <?php echo number_format($account['balance'], 2); ?> <span class="fs-5 text-secondary fw-normal">USDT</span>
                        </h2>
                    </div>
                    <div class="col-md-5 text-md-end mt-3 mt-md-0">
                        <button class="btn btn-warning fw-bold px-4 me-2">充值</button>
                        <button class="btn btn-outline-light fw-bold px-4">提现</button>
                    </div>
                </div>
            </div>

            <!-- Asset List -->
            <div class="glass-card p-4 bg-dark mb-4">
                <h5 class="text-white mb-4"><i class="bi bi-wallet2 text-warning me-2"></i> 我的资产</h5>
                <div class="table-responsive">
                    <table class="table table-dark table-hover align-middle">
                        <thead>
                            <tr class="text-secondary small border-bottom border-secondary">
                                <th>币种</th>
                                <th>可用余额</th>
                                <th>冻结金额</th>
                                <th class="text-end">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <img src="https://cryptologos.cc/logos/tether-usdt-logo.png" width="24" class="me-2">
                                        <span class="fw-bold">USDT</span>
                                    </div>
                                </td>
                                <td class="fw-bold"><?php echo number_format($account['balance'], 2); ?></td>
                                <td class="text-secondary">0.00</td>
                                <td class="text-end"><a href="trade.php" class="btn btn-sm btn-link text-warning p-0">交易</a></td>
                            </tr>
                            <?php foreach ($assets as $asset): if($asset['currency'] == 'USDT') continue; ?>
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <span class="fw-bold"><?php echo $asset['currency']; ?></span>
                                    </div>
                                </td>
                                <td class="fw-bold"><?php echo number_format($asset['balance'], 6); ?></td>
                                <td class="text-secondary"><?php echo number_format($asset['frozen'], 6); ?></td>
                                <td class="text-end"><a href="trade.php?symbol=<?php echo $asset['currency']; ?>USDT" class="btn btn-sm btn-link text-warning p-0">交易</a></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Transaction History -->
            <div class="glass-card p-4 bg-dark">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h5 class="text-white mb-0"><i class="bi bi-clock-history text-warning me-2"></i> 最近充提</h5>
                    <a href="#" class="text-warning small text-decoration-none">查看全部</a>
                </div>
                <div class="table-responsive">
                    <table class="table table-dark small">
                        <tbody>
                            <?php
                            $stmt = db()->prepare("SELECT * FROM transactions WHERE account_id = ? ORDER BY timestamp DESC LIMIT 5");
                            $stmt->execute([$account['id']]);
                            $txs = $stmt->fetchAll();
                            if (empty($txs)):
                            ?>
                            <tr><td class="text-center text-secondary py-4">暂无记录</td></tr>
                            <?php else: foreach($txs as $tx): ?>
                            <tr>
                                <td><?php echo $tx['timestamp']; ?></td>
                                <td><span class="badge bg-<?php echo $tx['transaction_type']=='deposit'?'success':'danger'; ?>"><?php echo strtoupper($tx['transaction_type']); ?></span></td>
                                <td><?php echo number_format($tx['amount'], 2); ?> <?php echo $tx['currency']; ?></td>
                                <td class="text-end"><?php echo $tx['status']; ?></td>
                            </tr>
                            <?php endforeach; endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include 'footer.php'; ?>