<?php
$user_id = $_SESSION['user_id'];
$user = currentUser();

$apps_count = $db->fetchOne("SELECT COUNT(DISTINCT app_id) as cnt FROM licenses WHERE user_id = $user_id AND status = 1")['cnt'] ?? 0;
$licenses_count = $db->fetchOne("SELECT COUNT(*) as cnt FROM licenses WHERE user_id = $user_id AND status = 1")['cnt'] ?? 0;
$open_tickets = $db->fetchOne("SELECT COUNT(*) as cnt FROM tickets WHERE user_id = $user_id AND status IN ('open','processing')")['cnt'] ?? 0;
$current_group = getUserCurrentGroup($user_id);
?>

<!-- 用户卡片 -->
<div class="card" style="display: flex; align-items: center; gap: 12px;">
    <img src="https://api.iconify.design/mdi:account-circle.svg?color=%23667eea" width="50" height="50" alt="">
    <div style="flex:1;">
        <div style="font-weight: 600; font-size: 18px;"><?php echo htmlspecialchars($user['username']); ?></div>
    </div>
</div>

<!-- 余额卡片 -->
<div class="card" style="background: linear-gradient(135deg, #667eea, #764ba2); color: white;">
    <div style="display: flex; justify-content: space-between; align-items: center;">
        <div>
            <div style="font-size: 13px; opacity: 0.9;">账户余额</div>
            <div style="font-size: 32px; font-weight: 700;">¥<?php echo number_format($user['balance'], 2); ?></div>
        </div>
        <a href="?page=recharge" style="background: white; color: #667eea; padding: 10px 20px; border-radius: 30px; text-decoration: none; font-weight: 500;">
            <img src="https://api.iconify.design/mdi:plus.svg?color=%23667eea" width="16" height="16" alt=""> 充值
        </a>
    </div>
    <?php if ($current_group['discount'] < 100): ?>
    <div style="margin-top: 8px; font-size: 12px; opacity: 0.9;">
        <img src="https://api.iconify.design/mdi:percent.svg?color=white" width="14" height="14" alt="">
        <?php echo $current_group['discount']; ?>折优惠
    </div>
    <?php endif; ?>
</div>

<!-- 统计 -->
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;">
    <div class="card" style="text-align: center; padding: 12px;">
        <div style="font-size: 22px; font-weight: 700; color: #667eea;"><?php echo $apps_count; ?></div>
        <div style="font-size: 12px; color: #999;">应用</div>
    </div>
    <div class="card" style="text-align: center; padding: 12px;">
        <div style="font-size: 22px; font-weight: 700; color: #f59e0b;"><?php echo $licenses_count; ?></div>
        <div style="font-size: 12px; color: #999;">授权码</div>
    </div>
    <div class="card" style="text-align: center; padding: 12px;">
        <div style="font-size: 22px; font-weight: 700; color: #ef4444;"><?php echo $open_tickets; ?></div>
        <div style="font-size: 12px; color: #999;">工单</div>
    </div>
</div>

<!-- 快捷入口 -->
<div class="card">
    <h3 style="margin-bottom: 16px; font-size: 15px;">快捷入口</h3>
    <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;">
        <a href="?page=buy_app" style="text-decoration: none; text-align: center;">
            <img src="https://api.iconify.design/mdi:cart.svg?color=%23667eea" width="28" height="28" alt="">
            <div style="font-size: 12px; color: #666;">购买</div>
        </a>
        <a href="?page=licenses" style="text-decoration: none; text-align: center;">
            <img src="https://api.iconify.design/mdi:key.svg?color=%23f59e0b" width="28" height="28" alt="">
            <div style="font-size: 12px; color: #666;">授权</div>
        </a>
        <a href="?page=tickets" style="text-decoration: none; text-align: center;">
            <img src="https://api.iconify.design/mdi:ticket.svg?color=%23ef4444" width="28" height="28" alt="">
            <div style="font-size: 12px; color: #666;">工单</div>
        </a>
        <a href="?page=version" style="text-decoration: none; text-align: center;">
            <img src="https://api.iconify.design/mdi:information.svg?color=%238b5cf6" width="28" height="28" alt="">
            <div style="font-size: 12px; color: #666;">版本</div>
        </a>
    </div>
</div>