<?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-lg-4">
            <div class="glass-card p-4 mb-4 text-center">
                <div class="position-relative d-inline-block mb-3">
                    <i class="bi bi-person-circle text-warning" style="font-size: 80px;"></i>
                    <?php if($account['kyc_status'] == 'VERIFIED'): ?>
                    <span class="position-absolute bottom-0 end-0 bg-success rounded-circle p-1" style="border: 4px solid var(--bg-card);">
                        <i class="bi bi-check-lg text-white" style="font-size: 14px;"></i>
                    </span>
                    <?php endif; ?>
                </div>
                <h4 class="text-white mb-1"><?php echo htmlspecialchars($_SESSION['username']); ?></h4>
                <div class="d-flex justify-content-center gap-2 mb-4">
                    <span class="badge bg-dark border border-secondary text-secondary px-3 py-2">UID: <?php echo $account['uid']; ?></span>
                    <span class="badge bg-<?php echo $account['kyc_status']=='VERIFIED'?'success':'warning'; ?> text-<?php echo $account['kyc_status']=='VERIFIED'?'white':'dark'; ?> px-3 py-2">
                        <?php 
                        $kyc_labels = ['UNVERIFIED'=>'未认证', 'PENDING'=>'审核中', 'VERIFIED'=>'已认证', 'REJECTED'=>'已驳回'];
                        echo $kyc_labels[$account['kyc_status']] ?? '未认证';
                        ?>
                    </span>
                </div>

                <div class="list-group list-group-flush bg-transparent text-start">
                    <a href="verify.php" class="list-group-item bg-transparent text-secondary border-secondary d-flex justify-content-between px-0 py-3">
                        <span><i class="bi bi-shield-check me-2"></i> 实名认证</span>
                        <span class="<?php echo $account['kyc_status']=='VERIFIED'?'text-success':'text-warning'; ?> small">
                            <?php echo $account['kyc_status'] == 'UNVERIFIED' ? '去认证 <i class="bi bi-chevron-right"></i>' : $kyc_labels[$account['kyc_status']]; ?>
                        </span>
                    </a>
                    <div class="list-group-item bg-transparent text-secondary border-secondary d-flex justify-content-between px-0 py-3">
                        <span><i class="bi bi-star me-2"></i> 信用评分</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 py-3">
                        <span><i class="bi bi-calendar3 me-2"></i> 注册时间</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 border-0" style="background: rgba(220, 53, 69, 0.1);">安全退出</a>
            </div>

            <div class="glass-card p-4 mb-4">
                <h6 class="text-white mb-3">安全中心</h6>
                <div class="d-flex align-items-center mb-3">
                    <div class="bg-success rounded-circle p-2 me-3" style="width: 32px; height: 32px; display: flex; align-items: center; justify-content: center;">
                        <i class="bi bi-envelope text-white small"></i>
                    </div>
                    <div>
                        <div class="text-white small">绑定邮箱</div>
                        <div class="text-secondary smaller"><?php echo substr($_SESSION['username'], 0, 3); ?>***@gmail.com</div>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <div class="bg-warning rounded-circle p-2 me-3" style="width: 32px; height: 32px; display: flex; align-items: center; justify-content: center;">
                        <i class="bi bi-phone text-dark small"></i>
                    </div>
                    <div>
                        <div class="text-white small">绑定手机</div>
                        <div class="text-warning smaller" style="cursor: pointer;">未绑定，去绑定 ></div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Asset Content -->
        <div class="col-lg-8">
            <!-- Balance Card -->
            <div class="glass-card p-4 mb-4" style="background: linear-gradient(135deg, #2b2f36 0%, #181a20 100%); border: 1px solid #fcd53533; box-shadow: 0 10px 30px rgba(0,0,0,0.5);">
                <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-1">
                            <span class="text-warning">$</span> <?php echo number_format($account['balance'] + $account['frozen_balance'], 2); ?> <span class="fs-6 text-secondary fw-normal">USDT</span>
                        </h2>
                        <div class="text-secondary smaller">≈ <?php echo number_format(($account['balance'] + $account['frozen_balance']) * 7.25, 2); ?> CNY</div>
                    </div>
                    <div class="col-md-5 text-md-end mt-3 mt-md-0">
                        <a href="deposit.php" class="btn btn-warning fw-bold px-4 me-2">充值</a>
                        <a href="withdraw.php" class="btn btn-outline-light fw-bold px-4">提现</a>
                    </div>
                </div>
            </div>

            <!-- Asset Tabs -->
            <div class="glass-card p-4 mb-4">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h5 class="text-white mb-0"><i class="bi bi-wallet2 text-warning me-2"></i> 资产概览</h5>
                    <div class="form-check form-switch small">
                        <input class="form-check-input" type="checkbox" id="hideZero">
                        <label class="form-check-label text-secondary" for="hideZero">隐藏 0 余额</label>
                    </div>
                </div>
                
                <div class="table-responsive">
                    <table class="table table-dark table-hover align-middle">
                        <thead>
                            <tr class="text-secondary small border-bottom border-secondary">
                                <th class="ps-0">币种</th>
                                <th>总额</th>
                                <th>可用</th>
                                <th>冻结</th>
                                <th class="text-end pe-0">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td class="ps-0">
                                    <div class="d-flex align-items-center">
                                        <div class="bg-success rounded-circle me-2 d-flex align-items-center justify-content-center" style="width:28px; height:28px;">
                                            <i class="bi bi-currency-dollar text-white"></i>
                                        </div>
                                        <span class="fw-bold">USDT</span>
                                    </div>
                                </td>
                                <td class="text-white fw-bold"><?php echo number_format($account['balance'] + $account['frozen_balance'], 2); ?></td>
                                <td class="text-white"><?php echo number_format($account['balance'], 2); ?></td>
                                <td class="text-secondary"><?php echo number_format($account['frozen_balance'], 2); ?></td>
                                <td class="text-end pe-0">
                                    <a href="trade.php" class="btn btn-sm btn-link text-warning text-decoration-none">交易</a>
                                    <a href="withdraw.php" class="btn btn-sm btn-link text-secondary text-decoration-none ms-2">提现</a>
                                </td>
                            </tr>
                            <?php foreach ($assets as $asset): if($asset['currency'] == 'USDT') continue; ?>
                            <tr>
                                <td class="ps-0">
                                    <div class="d-flex align-items-center">
                                        <div class="bg-secondary rounded-circle me-2 d-flex align-items-center justify-content-center fw-bold" style="width:28px; height:28px; font-size: 10px;">
                                            <?php echo substr($asset['currency'], 0, 1); ?>
                                        </div>
                                        <span class="fw-bold"><?php echo $asset['currency']; ?></span>
                                    </div>
                                </td>
                                <td class="text-white fw-bold"><?php echo number_format($asset['balance'] + $asset['frozen'], 6); ?></td>
                                <td class="text-white"><?php echo number_format($asset['balance'], 6); ?></td>
                                <td class="text-secondary"><?php echo number_format($asset['frozen'], 6); ?></td>
                                <td class="text-end pe-0">
                                    <a href="trade.php?symbol=<?php echo $asset['currency']; ?>USDT" class="btn btn-sm btn-link text-warning text-decoration-none">交易</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Transaction History -->
            <div class="glass-card p-4">
                <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 align-middle">
                        <thead>
                            <tr class="text-secondary border-bottom border-secondary">
                                <th>时间</th>
                                <th>类型</th>
                                <th>金额</th>
                                <th>方式</th>
                                <th class="text-end">状态</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $stmt = db()->prepare("SELECT * FROM transactions WHERE account_id = ? ORDER BY timestamp DESC LIMIT 10");
                            $stmt->execute([$account['id']]);
                            $txs = $stmt->fetchAll();
                            if (empty($txs)):
                            ?>
                            <tr><td colspan="5" class="text-center text-secondary py-5">暂无流水记录</td></tr>
                            <?php else: foreach($txs as $tx): ?>
                            <tr>
                                <td class="text-secondary"><?php echo substr($tx['timestamp'], 2, 14); ?></td>
                                <td>
                                    <span class="badge bg-<?php echo $tx['transaction_type']=='deposit'?'success-subtle text-success':'danger-subtle text-danger'; ?> border border-<?php echo $tx['transaction_type']=='deposit'?'success':'danger'; ?> px-2 py-1">
                                        <?php echo $tx['transaction_type']=='deposit'?'充值':'提现'; ?>
                                    </span>
                                </td>
                                <td class="fw-bold"><?php echo number_format($tx['amount'], 2); ?> <span class="smaller fw-normal">USDT</span></td>
                                <td class="text-secondary"><?php echo $tx['pay_method'] ?? 'USDT'; ?></td>
                                <td class="text-end">
                                    <?php if($tx['status'] == 'completed'): ?>
                                        <span class="text-success"><i class="bi bi-check-circle me-1"></i>已成功</span>
                                    <?php elseif($tx['status'] == 'failed'): ?>
                                        <span class="text-danger"><i class="bi bi-x-circle me-1"></i>已驳回</span>
                                    <?php else: ?>
                                        <span class="text-warning"><i class="bi bi-hourglass-split me-1"></i>审核中</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<style>
    .bg-success-subtle { background-color: rgba(14, 203, 129, 0.1) !important; }
    .bg-danger-subtle { background-color: rgba(246, 70, 93, 0.1) !important; }
    .smaller { font-size: 0.75rem; }
</style>
<?php include 'footer.php'; ?>