<?php
$user_id = $_SESSION['user_id'];

// 获取用户已授权的应用
$apps = $db->fetchAll("
    SELECT DISTINCT a.id, a.name, a.description, a.price, a.duration_days,
           (SELECT COUNT(*) FROM licenses WHERE app_id = a.id AND user_id = $user_id AND status = 1) as license_count
    FROM apps a
    JOIN licenses l ON a.id = l.app_id
    WHERE l.user_id = $user_id AND l.status = 1
    ORDER BY a.id DESC
");
?>

<div class="apps-container">
    <div class="page-header">
        <div class="page-title-section">
            <h2>我的应用</h2>
            <p class="page-desc">管理您已购买的应用授权</p>
        </div>
        <a href="?page=buy_app" class="btn btn-primary">
            <span class="btn-icon">+</span> 购买新授权
        </a>
    </div>
    
    <?php if (empty($apps)): ?>
        <div class="empty-state">
            <div class="empty-icon">📦</div>
            <p>您还没有购买任何应用</p>
            <a href="?page=buy_app" class="btn btn-primary">立即购买</a>
        </div>
    <?php else: ?>
        <div class="apps-grid">
            <?php foreach ($apps as $app): ?>
                <div class="app-card">
                    <div class="app-header">
                        <h3><?php echo htmlspecialchars($app['name']); ?></h3>
                        <span class="license-count">
                            <span class="count-number"><?php echo $app['license_count']; ?></span> 个授权码
                        </span>
                    </div>
                    <p class="app-desc"><?php echo htmlspecialchars($app['description'] ?: '暂无描述'); ?></p>
                    <div class="app-meta">
                        <span class="meta-item">📅 <?php echo $app['duration_days'] ? $app['duration_days'] . '天' : '永久'; ?></span>
                        <span class="meta-item">💰 ¥<?php echo number_format($app['price'], 2); ?></span>
                    </div>
                    <div class="app-footer">
                        <a href="?page=licenses" class="btn btn-primary btn-sm">
                            <span class="btn-icon">🔑</span> 管理授权码
                        </a>
                     
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>

<style>
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 32px;
    flex-wrap: wrap;
    gap: 20px;
}
.page-title-section h2 {
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 4px;
    color: var(--text-primary);
}
.page-desc {
    font-size: 14px;
    color: var(--text-secondary);
}
.btn-primary {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 24px;
    background: var(--primary-color);
    color: white;
    border-radius: 10px;
    text-decoration: none;
    font-weight: 500;
    font-size: 14px;
    transition: all 0.2s;
    border: none;
    cursor: pointer;
}
.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(59,130,246,0.3);
}
.btn-outline {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 18px;
    background: transparent;
    color: var(--primary-color);
    border: 1px solid var(--primary-color);
    border-radius: 8px;
    text-decoration: none;
    font-weight: 500;
    font-size: 13px;
    transition: all 0.2s;
}
.btn-outline:hover {
    background: var(--primary-light);
    transform: translateY(-2px);
}
.btn-sm {
    padding: 8px 18px;
    font-size: 13px;
}
.btn-icon {
    font-size: 14px;
}
.apps-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 24px;
}
.app-card {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 24px;
    transition: all 0.3s ease;
}
.app-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-lg);
    border-color: var(--primary-light);
}
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}
.app-header h3 {
    font-size: 20px;
    font-weight: 600;
    color: var(--text-primary);
}
.license-count {
    font-size: 13px;
    color: var(--text-secondary);
    background: var(--bg-secondary);
    padding: 4px 12px;
    border-radius: 30px;
}
.count-number {
    font-weight: 600;
    color: var(--primary-color);
}
.app-desc {
    color: var(--text-secondary);
    margin-bottom: 16px;
    line-height: 1.6;
    font-size: 14px;
}
.app-meta {
    display: flex;
    gap: 20px;
    margin-bottom: 20px;
    padding: 12px 0;
    border-top: 1px solid var(--border-color);
    border-bottom: 1px solid var(--border-color);
}
.meta-item {
    font-size: 13px;
    color: var(--text-muted);
}
.app-footer {
    display: flex;
    gap: 12px;
    margin-top: 8px;
}
.empty-state {
    text-align: center;
    padding: 80px 20px;
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
}
.empty-icon {
    font-size: 64px;
    margin-bottom: 20px;
    opacity: 0.5;
}
.empty-state p {
    margin-bottom: 24px;
    color: var(--text-secondary);
    font-size: 16px;
}
@media (max-width: 768px) {
    .apps-grid {
        grid-template-columns: 1fr;
    }
    .app-footer {
        flex-direction: column;
    }
    .app-footer .btn {
        justify-content: center;
    }
    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }
    .page-header .btn-primary {
        align-self: flex-start;
    }
}
</style>