<?php
session_start();
include 'header.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

require_once 'db/config.php';
$db = db();
$stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();

$kyc_status = $user['kyc_status'] ?? 0;
$kyc_labels = [
    0 => '未认证',
    1 => '审核中',
    2 => '已认证',
    3 => '未通过',
];
$kyc_colors = [0 => '#888', 1 => '#f0b90b', 2 => 'var(--success-color)', 3 => 'var(--danger-color)'];
?>

<style>
    .profile-container {
        padding: 40px 0;
        background: #0b0e11;
        min-height: 100vh;
    }
    .profile-grid {
        display: grid;
        grid-template-columns: 340px 1fr;
        gap: 30px;
    }
    .profile-card {
        background: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 24px;
        overflow: hidden;
    }
    .user-info-card {
        padding: 40px 30px;
        text-align: center;
        background: linear-gradient(180deg, #1e2329 0%, #161a1e 100%);
    }
    .avatar-circle {
        width: 100px;
        height: 100px;
        background: linear-gradient(135deg, #f0b90b, #f8d33a);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 25px;
        font-size: 2.5rem;
        font-weight: 900;
        color: #000;
        box-shadow: 0 15px 35px rgba(240, 185, 11, 0.25);
    }
    .uid-badge {
        display: inline-block;
        padding: 6px 16px;
        background: rgba(255, 255, 255, 0.05);
        border-radius: 20px;
        color: var(--text-muted);
        font-size: 0.85rem;
        margin-top: 10px;
        font-family: 'Roboto Mono', monospace;
    }
    .sidebar-menu { padding: 15px; }
    .sidebar-menu-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 18px 22px;
        border-radius: 18px;
        color: white;
        text-decoration: none;
        transition: all 0.3s ease;
        margin-bottom: 10px;
    }
    .sidebar-menu-item:hover { background: rgba(255, 255, 255, 0.04); transform: translateX(5px); }
    
    .balance-card {
        padding: 50px;
        margin-bottom: 30px;
        background: url('https://www.transparenttextures.com/patterns/carbon-fibre.png'), linear-gradient(135deg, #1e2329 0%, #0b0e11 100%);
        position: relative;
        display: flex;
        flex-direction: column;
        justify-content: center;
    }
    .balance-val {
        font-size: 3.5rem;
        font-weight: 800;
        color: white;
        letter-spacing: -1px;
        margin: 10px 0;
    }
    .balance-actions { display: flex; gap: 20px; margin-top: 40px; }
    .balance-actions .btn {
        flex: 1;
        padding: 18px;
        border-radius: 16px;
        font-weight: 800;
        font-size: 1.1rem;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 12px;
        transition: 0.3s;
        text-decoration: none;
    }
    .btn-deposit { background: var(--primary-color); color: white; }
    .btn-withdraw { background: #2b3139; color: white; border: 1px solid #3b424d; }
    .btn-deposit:hover { background: #0042cc; transform: translateY(-3px); box-shadow: 0 10px 20px rgba(0, 82, 255, 0.2); }
    .btn-withdraw:hover { background: #3b424d; transform: translateY(-3px); }

    .profile-tabs {
        display: flex;
        gap: 50px;
        padding: 0 50px;
        border-bottom: 1px solid var(--border-color);
        background: rgba(255,255,255,0.01);
    }
    .profile-tab-btn {
        padding: 20px 0;
        background: none;
        border: none;
        color: var(--text-muted);
        font-weight: 700;
        font-size: 1.1rem;
        cursor: pointer;
        border-bottom: 4px solid transparent;
        transition: 0.3s;
    }
    .profile-tab-btn.active { color: var(--primary-color); border-bottom-color: var(--primary-color); }
    
    .record-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 20px;
    }
    .record-card {
        background: #161a1e;
        border-radius: 20px;
        padding: 25px;
        border: 1px solid #2b3139;
        transition: 0.3s;
    }
    .record-card:hover { border-color: rgba(0, 82, 255, 0.3); transform: translateY(-5px); }
    
    .status-badge {
        padding: 5px 12px;
        border-radius: 8px;
        font-size: 11px;
        font-weight: 800;
    }

    @media (max-width: 1200px) { 
        .record-grid { grid-template-columns: 1fr; } 
    }
    @media (max-width: 992px) { 
        .profile-grid { grid-template-columns: 1fr; } 
    }
    @media (max-width: 768px) {
        .balance-card { padding: 30px 20px; }
        .balance-val { font-size: 2.5rem; }
        .profile-tabs { gap: 20px; padding: 0 20px; }
        .profile-tab-btn { font-size: 1rem; }
        .tab-content { padding: 25px 15px !important; }
        .record-card { padding: 15px; }
    }
    @media (max-width: 480px) {
        .assets-grid { grid-template-columns: 1fr !important; }
    }
</style>

<div class="profile-container">
    <div class="container">
        <div class="profile-grid">
            <!-- 侧边栏 -->
            <div class="profile-sidebar">
                <div class="profile-card user-info-card">
                    <div class="avatar-circle">
                        <?php echo strtoupper(substr($user['username'], 0, 1)); ?>
                    </div>
                    <h2 style="color: white; margin: 0; font-size: 1.8rem; font-weight: 800;"><?php echo $user['username']; ?></h2>
                    <div class="uid-badge">UID: <?php echo $user['uid'] ?: '618120'; ?></div>
                    
                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin-top: 40px; padding-top: 30px; border-top: 1px solid rgba(255,255,255,0.05);">
                        <div>
                            <div style="color: var(--text-muted); font-size: 13px; margin-bottom: 6px;">信用评分</div>
                            <div style="font-weight: 800; font-size: 1.4rem; color: var(--success-color);"><?php echo $user['credit_score'] ?? 100; ?></div>
                        </div>
                        <div style="border-left: 1px solid rgba(255,255,255,0.05);">
                            <div style="color: var(--text-muted); font-size: 13px; margin-bottom: 6px;">账户等级</div>
                            <div style="font-weight: 800; font-size: 1.4rem; color: #f0b90b;">VIP 0</div>
                        </div>
                    </div>
                </div>

                <div class="profile-card sidebar-menu">
                    <a href="kyc.php" class="sidebar-menu-item">
                        <div style="display: flex; align-items: center; gap: 15px;">
                            <div style="width: 45px; height: 45px; background: rgba(79,172,254,0.1); border-radius: 14px; display: flex; align-items: center; justify-content: center; color: #4facfe;">
                                <i class="fas fa-id-card fa-lg"></i>
                            </div>
                            <span style="font-weight: 700;">身份认证 (KYC)</span>
                        </div>
                        <div style="display: flex; align-items: center; gap: 10px;">
                            <span style="font-size: 12px; font-weight: 800; color: <?php echo $kyc_colors[$kyc_status]; ?>"><?php echo $kyc_labels[$kyc_status]; ?></span>
                            <i class="fas fa-chevron-right" style="font-size: 12px; color: var(--text-muted);"></i>
                        </div>
                    </a>
                    <a href="security.php" class="sidebar-menu-item">
                        <div style="display: flex; align-items: center; gap: 15px;">
                            <div style="width: 45px; height: 45px; background: rgba(14,203,129,0.1); border-radius: 14px; display: flex; align-items: center; justify-content: center; color: var(--success-color);">
                                <i class="fas fa-shield-alt fa-lg"></i>
                            </div>
                            <span style="font-weight: 700;">安全设置</span>
                        </div>
                        <i class="fas fa-chevron-right" style="font-size: 12px; color: var(--text-muted);"></i>
                    </a>
                    <a href="logout.php" class="sidebar-menu-item" style="margin-top: 15px; color: var(--danger-color);">
                        <div style="display: flex; align-items: center; gap: 15px;">
                            <div style="width: 45px; height: 45px; background: rgba(246,70,93,0.1); border-radius: 14px; display: flex; align-items: center; justify-content: center;">
                                <i class="fas fa-sign-out-alt fa-lg"></i>
                            </div>
                            <span style="font-weight: 700;">退出登录</span>
                        </div>
                    </a>
                </div>
            </div>

            <!-- 主内容区 -->
            <div>
                <div class="profile-card balance-card">
                    <div style="color: var(--text-muted); font-size: 16px; font-weight: 600;">账户总净资产 (USDT)</div>
                    <div class="balance-val"><?php echo number_format($user['balance'] ?? 0, 2); ?></div>
                    <div style="color: var(--text-muted); font-size: 1.1rem; display: flex; align-items: center; gap: 12px;">
                        <span style="font-family: 'Roboto Mono', monospace;">≈ $ <?php echo number_format($user['balance'] ?? 0, 2); ?></span>
                        <i class="fas fa-eye" style="cursor: pointer; font-size: 16px; color: var(--primary-color);"></i>
                    </div>
                    
                    <div class="balance-actions">
                        <a href="deposit.php" class="btn btn-deposit">
                            <i class="fas fa-wallet"></i> 充值
                        </a>
                        <a href="withdraw.php" class="btn btn-withdraw">
                            <i class="fas fa-paper-plane"></i> 提现
                        </a>
                    </div>
                </div>

                <div class="profile-card">
                    <div class="profile-tabs">
                        <button class="profile-tab-btn active" onclick="switchProfileTab(this, 'assets-tab')">资产详情</button>
                        <button class="profile-tab-btn" onclick="switchProfileTab(this, 'records-tab')">交易记录</button>
                    </div>

                    <!-- 资产页签 -->
                    <div id="assets-tab" class="tab-content" style="padding: 40px;">
                        <div class="assets-grid" style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
                            <?php
                            $coins = [
                                ['symbol' => 'USDT', 'name' => 'Tether', 'balance' => $user['balance'] ?? 0],
                                ['symbol' => 'BTC', 'name' => 'Bitcoin', 'balance' => 0],
                                ['symbol' => 'ETH', 'name' => 'Ethereum', 'balance' => 0],
                                ['symbol' => 'SOL', 'name' => 'Solana', 'balance' => 0],
                                ['symbol' => 'BNB', 'name' => 'Binance Coin', 'balance' => 0],
                                ['symbol' => 'XRP', 'name' => 'Ripple', 'balance' => 0],
                            ];
                            
                            try {
                                $asset_stmt = $db->prepare("SELECT * FROM user_assets WHERE user_id = ?");
                                $asset_stmt->execute([$_SESSION['user_id']]);
                                $db_assets = $asset_stmt->fetchAll();
                                foreach ($db_assets as $da) {
                                    $found = false;
                                    foreach ($coins as &$c) {
                                        if ($c['symbol'] === $da['symbol']) {
                                            $c['balance'] = $da['amount'];
                                            $found = true;
                                        }
                                    }
                                    if (!$found) {
                                        $coins[] = ['symbol' => $da['symbol'], 'name' => '', 'balance' => $da['amount']];
                                    }
                                }
                            } catch (Exception $e) {}

                            foreach ($coins as $coin):
                            ?>
                            <div style="display: flex; align-items: center; justify-content: space-between; padding: 22px; background: rgba(255,255,255,0.02); border-radius: 20px; border: 1px solid rgba(255,255,255,0.05); transition: 0.3s;">
                                <div style="display: flex; align-items: center; gap: 12px;">
                                    <img src="https://raw.githubusercontent.com/spothq/cryptocurrency-icons/master/128/color/<?php echo strtolower($coin['symbol']); ?>.png" width="36" height="36" onerror="this.src='https://cdn-icons-png.flaticon.com/512/2585/2585274.png'">
                                    <div>
                                        <div style="font-weight: 800; font-size: 1.1rem; color: white;"><?php echo $coin['symbol']; ?></div>
                                        <div style="font-size: 0.75rem; color: var(--text-muted);"><?php echo $coin['name']; ?></div>
                                    </div>
                                </div>
                                <div style="text-align: right;">
                                    <div style="font-weight: 800; font-family: 'Roboto Mono', monospace; font-size: 1.1rem; color: white;">
                                        <?php echo number_format($coin['balance'], $coin['symbol'] === 'USDT' ? 2 : 6); ?>
                                    </div>
                                    <div style="font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase;">余额</div>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </div>
                    </div>

                    <!-- 记录页签 -->
                    <div id="records-tab" class="tab-content" style="display: none; padding: 40px;">
                        <div id="records-list" class="record-grid" style="max-height: 800px; overflow-y: auto; padding-right: 10px;">
                            <div style="grid-column: span 2; text-align: center; padding: 60px;">
                                <div class="spinner-border text-warning" role="status"></div>
                                <p style="margin-top: 15px; color: var(--text-muted);">正在加载交易记录...</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    function switchProfileTab(btn, tabId) {
        document.querySelectorAll('.profile-tab-btn').forEach(b => b.classList.remove('active'));
        btn.classList.add('active');
        document.querySelectorAll('.tab-content').forEach(c => c.style.display = 'none');
        document.getElementById(tabId).style.display = 'block';
        if (tabId === 'records-tab') { loadTransactionRecords(); }
    }

    async function loadTransactionRecords() {
        const container = document.getElementById('records-list');
        try {
            const [spotResp, futuresResp, optionsResp, transResp] = await Promise.all([
                fetch('api/get_orders.php?type=spot&status=history'),
                fetch('api/get_orders.php?type=futures&status=history'),
                fetch('api/get_option_orders.php?status=history'),
                fetch('api/get_transactions.php')
            ]);
            
            const spotRes = await spotResp.json();
            const futuresRes = await futuresResp.json();
            const optionsRes = await optionsResp.json();
            const transRes = await transResp.json();

            let allRecords = [];
            if (spotRes.success) spotRes.data.forEach(r => { r.display_type = 'trade'; r.trade_label = '现货交易'; allRecords.push(r); });
            if (futuresRes.success) futuresRes.data.forEach(r => { r.display_type = 'trade'; r.trade_label = '永续合约'; allRecords.push(r); });
            if (optionsRes.success) optionsRes.data.forEach(r => { r.display_type = 'option'; r.trade_label = '秒合约'; allRecords.push(r); });
            if (transRes.success) transRes.data.forEach(t => { t.display_type = 'transaction'; allRecords.push(t); });

            allRecords.sort((a, b) => new Date(b.created_at || b.open_time) - new Date(a.created_at || a.open_time));

            if (allRecords.length === 0) {
                container.innerHTML = '<div style="grid-column: span 1; text-align: center; padding: 100px; color: var(--text-muted);"><i class="fas fa-file-invoice" style="font-size: 4rem; opacity: 0.2; margin-bottom: 25px;"></i><br>暂无任何交易记录</div>';
                return;
            }

            let html = '';
            allRecords.forEach(r => {
                if (r.display_type === 'trade' || r.display_type === 'option') {
                    const profit = parseFloat(r.profit || 0);
                    const isProfit = profit > 0;
                    const profitColor = isProfit ? 'var(--success-color)' : (profit < 0 ? 'var(--danger-color)' : 'var(--text-muted)');
                    const symbol = r.symbol || r.pair;
                    const time = r.created_at || r.open_time;
                    
                    html += `
                        <div class="record-card">
                            <div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 18px;">
                                <div>
                                    <div style="display: flex; align-items: center; gap: 8px;">
                                        <span style="font-weight: 800; color: white; font-size: 1.1rem;">${symbol}</span>
                                        <span style="background: rgba(0,82,255,0.1); color: var(--primary-color); padding: 3px 8px; border-radius: 6px; font-size: 9px; font-weight: 800;">${r.trade_label}</span>
                                    </div>
                                    <div style="font-size: 11px; color: var(--text-muted); margin-top: 6px;">${time}</div>
                                </div>
                                <div style="text-align: right;">
                                    <div style="font-size: 9px; color: var(--text-muted); text-transform: uppercase; margin-bottom: 2px;">盈利 (USDT)</div>
                                    <div style="font-weight: 800; color: ${profitColor}; font-size: 1.2rem;">${isProfit ? '+' : ''}${profit.toFixed(2)}</div>
                                </div>
                            </div>
                            <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; background: rgba(0,0,0,0.2); padding: 12px; border-radius: 12px; border: 1px solid rgba(255,255,255,0.03);">
                                <div>
                                    <div style="font-size: 9px; color: var(--text-muted); margin-bottom: 4px;">类型</div>
                                    <div style="font-weight: 800; color: ${r.side === 'buy' || r.direction === 'up' ? 'var(--success-color)' : 'var(--danger-color)'}; font-size: 12px;">${(r.side === 'buy' || r.direction === 'up') ? '看涨' : '看跌'}</div>
                                </div>
                                <div>
                                    <div style="font-size: 9px; color: var(--text-muted); margin-bottom: 4px;">金额</div>
                                    <div style="font-weight: 800; color: white; font-size: 12px;">${parseFloat(r.amount || r.invest).toFixed(2)}</div>
                                </div>
                                <div>
                                    <div style="font-size: 9px; color: var(--text-muted); margin-bottom: 4px;">价格</div>
                                    <div style="font-weight: 800; color: white; font-size: 12px;">${parseFloat(r.price || r.open_price).toLocaleString()}</div>
                                </div>
                            </div>
                        </div>
                    `;
                } else {
                    const amount = parseFloat(r.amount);
                    const isPositive = amount > 0;
                    const amountColor = isPositive ? 'var(--success-color)' : 'var(--danger-color)';
                    let typeLabel = r.type === 'deposit' ? '充值' : (r.type === 'withdrawal' ? '提现' : (r.type === 'withdraw_return' ? '提现退回' : r.type));
                    let statusLabel = r.status === 'completed' ? '已成功' : (r.status === 'pending' || r.status === 'matching' ? '处理中' : (r.status === 'rejected' ? '已拒绝' : r.status));
                    let statusColor = r.status === 'completed' ? 'var(--success-color)' : (r.status === 'rejected' ? 'var(--danger-color)' : '#f0b90b');

                    html += `
                        <div class="record-card">
                            <div style="display: flex; justify-content: space-between; align-items: flex-start;">
                                <div>
                                    <div style="display: flex; align-items: center; gap: 10px;">
                                        <span style="font-weight: 800; color: white; font-size: 1.1rem;">${typeLabel}</span>
                                        <span style="font-size: 10px; font-weight: 800; color: ${statusColor}; background: ${statusColor}1A; padding: 4px 8px; border-radius: 6px;">${statusLabel}</span>
                                    </div>
                                    <div style="font-size: 11px; color: var(--text-muted); margin-top: 10px; font-family: 'Roboto Mono', monospace;">${r.created_at}</div>
                                </div>
                                <div style="text-align: right;">
                                    <div style="font-size: 9px; color: var(--text-muted); text-transform: uppercase; margin-bottom: 4px;">变动 (USDT)</div>
                                    <div style="font-weight: 800; color: ${amountColor}; font-size: 1.3rem;">${isPositive ? '+' : ''}${amount.toFixed(2)}</div>
                                </div>
                            </div>
                        </div>
                    `;
                }
            });
            container.innerHTML = html;
        } catch (e) {
            container.innerHTML = '<div style="grid-column: span 1; text-align: center; padding: 60px; color: var(--danger-color); font-weight: 800;"><i class="fas fa-exclamation-triangle fa-2x"></i><br>数据加载失败</div>';
        }
    }
</script>

<?php include 'footer.php'; ?>