<?php
ob_start();
$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>
                <img src="https://api.iconify.design/mdi:application.svg?color=%23667eea" width="26" height="26" style="vertical-align: middle; margin-right: 10px;" alt="">
                我的应用
            </h2>
            <p class="page-desc">管理您已购买的应用授权</p>
        </div>
        <a href="?page=buy_app" class="btn btn-primary">
            <img src="https://api.iconify.design/mdi:plus.svg?color=white" width="18" height="18" style="margin-right: 6px;" alt="">
            购买新授权
        </a>
    </div>
    
    <?php if (empty($apps)): ?>
        <div class="empty-state">
            <div class="empty-icon">
                <img src="https://api.iconify.design/mdi:package-variant-closed.svg?color=%239ca3af" width="64" height="64" alt="">
            </div>
            <p>您还没有购买任何应用</p>
            <a href="?page=buy_app" class="btn btn-primary">
                <img src="https://api.iconify.design/mdi:cart.svg?color=white" width="18" height="18" style="margin-right: 6px;" alt="">
                立即购买
            </a>
        </div>
    <?php else: ?>
        <div class="apps-grid">
            <?php foreach ($apps as $app): ?>
                <div class="app-card">
                    <div class="app-icon">
                        <img src="https://api.iconify.design/mdi:application.svg?color=%23667eea" width="32" height="32" alt="">
                    </div>
                    <div class="app-header">
                        <h3><?php echo htmlspecialchars($app['name']); ?></h3>
                        <span class="license-count">
                            <img src="https://api.iconify.design/mdi:key.svg?color=%23667eea" width="12" height="12" alt="">
                            <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">
                            <img src="https://api.iconify.design/mdi:calendar-clock.svg?color=%23f59e0b" width="16" height="16" alt="">
                            <?php echo $app['duration_days'] ? $app['duration_days'] . '天' : '永久有效'; ?>
                        </span>
                        <span class="meta-item">
                            <img src="https://api.iconify.design/mdi:cash.svg?color=%2310b981" width="16" height="16" alt="">
                            ¥<?php echo number_format($app['price'], 2); ?>
                        </span>
                    </div>
                    <div class="app-footer">
                        <a href="?page=licenses" class="btn btn-primary btn-sm">
                            <img src="https://api.iconify.design/mdi:key.svg?color=white" width="16" height="16" style="margin-right: 6px;" alt="">
                            管理授权码
                        </a>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>

<style>
/* 基础变量 */
:root {
    --primary-color: #667eea;
    --primary-hover: #5a67d8;
    --danger-color: #ef4444;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --bg-primary: #ffffff;
    --bg-secondary: #f9fafb;
    --text-primary: #1f2937;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --border-color: #e5e7eb;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
}

.apps-container {
    max-width: 100%;
    padding: 4px;
}

/* 页面头部 */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 32px;
    flex-wrap: wrap;
    gap: 20px;
}
.page-title-section h2 {
    display: flex;
    align-items: center;
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 4px;
    color: var(--text-primary);
}
.page-desc {
    font-size: 14px;
    color: var(--text-secondary);
    margin-left: 36px;
}

/* 按钮 */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 20px;
    border-radius: var(--radius-md);
    text-decoration: none;
    font-weight: 500;
    font-size: 14px;
    transition: all 0.2s;
    cursor: pointer;
    border: 1px solid transparent;
}
.btn-primary {
    background: var(--primary-color);
    color: white;
    box-shadow: 0 2px 4px rgba(102, 126, 234, 0.2);
}
.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}
.btn-sm {
    padding: 8px 16px;
    font-size: 13px;
}

/* 应用卡片网格 */
.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;
    position: relative;
}
.app-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-lg);
    border-color: var(--primary-color);
}
.app-icon {
    width: 52px;
    height: 52px;
    border-radius: var(--radius-md);
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(102, 126, 234, 0.05) 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 16px;
}
.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);
    margin: 0;
}
.license-count {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    color: var(--text-secondary);
    background: var(--bg-secondary);
    padding: 4px 10px;
    border-radius: 30px;
    white-space: nowrap;
}
.count-number {
    font-weight: 700;
    color: var(--primary-color);
    font-size: 14px;
    margin-right: 2px;
}
.app-desc {
    color: var(--text-secondary);
    margin-bottom: 16px;
    line-height: 1.6;
    font-size: 14px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    min-height: 44px;
}
.app-meta {
    display: flex;
    gap: 20px;
    margin-bottom: 20px;
    padding: 14px 0;
    border-top: 1px solid var(--border-color);
    border-bottom: 1px solid var(--border-color);
}
.meta-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text-secondary);
}
.app-footer {
    display: flex;
    gap: 12px;
    margin-top: 8px;
}
.app-footer .btn {
    flex: 1;
}

/* 空状态 */
.empty-state {
    text-align: center;
    padding: 80px 20px;
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    border: 1px dashed var(--border-color);
}
.empty-icon {
    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 {
        width: 100%;
    }
    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }
    .page-header .btn-primary {
        align-self: flex-start;
    }
    .page-desc {
        margin-left: 0;
    }
}
</style>