<?php
ob_start();
require_once '../../config.php';

$apps = $db->fetchAll("SELECT * FROM apps WHERE status = 1 ORDER BY id DESC");

$site_name = getSiteConfig('site_name') ?? '麟泓授权系统';

// 获取购买页面公告
$announcements = $db->fetchAll("
    SELECT * FROM announcements 
    WHERE type = 'user_buy' AND status = 1 
    AND (start_time IS NULL OR start_time <= NOW()) 
    AND (end_time IS NULL OR end_time >= NOW())
    ORDER BY priority DESC, id DESC LIMIT 3
");
?>
<?php include 'header.php'; ?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购买授权 - <?php echo htmlspecialchars($site_name); ?></title>
    <link rel="stylesheet" href="/assets/css/style.css">
    <link rel="stylesheet" href="/assets/css/dark.css" id="dark-theme" disabled>
    <script src="/assets/js/main.js" defer></script>
    <style>
        .buy-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 40px 20px;
        }
        .buy-header {
            text-align: center;
            margin-bottom: 40px;
        }
        .buy-header h2 {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            font-size: 32px;
            margin-bottom: 10px;
            color: var(--text-primary);
        }
        .buy-header p {
            color: var(--text-secondary);
            font-size: 15px;
        }
        
        /* 公告区域 */
        .announcement-section {
            max-width: 900px;
            margin: 0 auto 32px;
        }
        .announcement-header {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 16px;
            font-weight: 600;
            color: var(--text-primary);
            margin-bottom: 12px;
        }
        .announce-list {
            background: var(--bg-primary);
            border-radius: 16px;
            border: 1px solid var(--border-color);
            overflow: hidden;
        }
        .announce-item {
            display: flex;
            align-items: center;
            gap: 14px;
            padding: 14px 18px;
            cursor: pointer;
            transition: background 0.2s;
            border-bottom: 1px solid var(--border-color);
        }
        .announce-item:last-child {
            border-bottom: none;
        }
        .announce-item:hover {
            background: var(--bg-secondary);
        }
        .announce-icon {
            width: 36px;
            height: 36px;
            border-radius: 10px;
            background: #fef3c7;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
        }
        .announce-content {
            flex: 1;
            min-width: 0;
        }
        .announce-title {
            font-size: 14px;
            font-weight: 500;
            color: var(--text-primary);
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .announce-time {
            font-size: 12px;
            color: var(--text-muted);
            display: flex;
            align-items: center;
            gap: 4px;
            margin-top: 2px;
        }
        
        /* 应用卡片网格 */
        .apps-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
            gap: 28px;
        }
        .app-card {
            background: var(--bg-primary);
            border: 1px solid var(--border-color);
            border-radius: 20px;
            padding: 28px 24px;
            transition: all 0.3s ease;
            display: flex;
            flex-direction: column;
        }
        .app-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 35px -10px rgba(0, 0, 0, 0.15);
            border-color: var(--primary-color);
        }
        .app-icon {
            width: 56px;
            height: 56px;
            border-radius: 16px;
            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: 20px;
        }
        .app-card-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 14px;
        }
        .app-card-header h3 {
            font-size: 22px;
            font-weight: 600;
            color: var(--text-primary);
            margin: 0;
        }
        .price {
            font-size: 26px;
            font-weight: 700;
            color: var(--primary-color);
        }
        .app-desc {
            color: var(--text-secondary);
            margin-bottom: 20px;
            line-height: 1.7;
            font-size: 14px;
            flex: 1;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }
        .app-meta {
            margin-bottom: 24px;
            padding: 16px 0;
            border-top: 1px solid var(--border-color);
            border-bottom: 1px solid var(--border-color);
        }
        .meta-item {
            display: flex;
            align-items: center;
            gap: 10px;
            color: var(--text-secondary);
            font-size: 14px;
        }
        .app-card-footer {
            margin-top: auto;
        }
        .btn-block {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            padding: 14px 20px;
            background: var(--primary-color);
            color: white;
            border: none;
            border-radius: 14px;
            font-size: 16px;
            font-weight: 600;
            text-decoration: none;
            cursor: pointer;
            transition: all 0.3s;
        }
        .btn-block:hover {
            background: var(--primary-hover);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
        }
        
        /* 空状态 */
        .empty-state {
            text-align: center;
            padding: 80px 20px;
            background: var(--bg-primary);
            border-radius: 20px;
            border: 1px dashed var(--border-color);
        }
        .empty-state img {
            opacity: 0.4;
            margin-bottom: 16px;
        }
        .empty-state p {
            color: var(--text-muted);
            font-size: 16px;
        }
        
        /* 公告弹窗 */
        .announcement-modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            backdrop-filter: blur(4px);
            display: none;
            align-items: center;
            justify-content: center;
            z-index: 10001;
        }
        .announcement-modal-content {
            background: var(--bg-primary);
            border-radius: 20px;
            width: 90%;
            max-width: 500px;
            max-height: 80vh;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0,0,0,0.2);
        }
        .modal-header {
            padding: 20px 24px;
            border-bottom: 1px solid var(--border-color);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .modal-header h3 {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 18px;
            font-weight: 600;
            margin: 0;
        }
        .modal-close {
            background: none;
            border: none;
            cursor: pointer;
            padding: 4px;
            border-radius: 8px;
            display: flex;
        }
        .modal-body {
            padding: 24px;
            max-height: 50vh;
            overflow-y: auto;
            line-height: 1.7;
        }
        .announcement-modal-footer {
            padding: 16px 24px;
            border-top: 1px solid var(--border-color);
            display: flex;
            gap: 12px;
            justify-content: flex-end;
        }
        .btn {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 10px 20px;
            border-radius: 10px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
            border: 1px solid transparent;
        }
        .btn-primary {
            background: var(--primary-color);
            color: white;
        }
        .btn-secondary {
            background: var(--bg-primary);
            color: var(--text-primary);
            border-color: var(--border-color);
        }
        
        @media (max-width: 768px) {
            .buy-header h2 {
                font-size: 26px;
            }
            .apps-grid {
                grid-template-columns: 1fr;
            }
            .app-card {
                padding: 22px 20px;
            }
            .app-card-header h3 {
                font-size: 18px;
            }
            .price {
                font-size: 22px;
            }
        }
    </style>
</head>
<body>
    <div class="front-container">
        <main class="front-main">
            <div class="buy-container">
                <div class="buy-header">
                    <h2>
                        <img src="https://api.iconify.design/mdi:cart.svg?color=%23667eea" width="32" height="32" alt="">
                        购买授权
                    </h2>
                    <p>选择您需要的应用授权，享受正版服务</p>
                </div>
                
                <!-- 公告区域 -->
                <?php if (!empty($announcements)): ?>
                <div class="announcement-section">
                    <div class="announcement-header">
                        <img src="https://api.iconify.design/mdi:bullhorn.svg?color=%23f59e0b" width="18" height="18" alt="">
                        公告须知
                    </div>
                    <div class="announce-list">
                        <?php foreach ($announcements as $announce): ?>
                        <div class="announce-item" onclick="showAnnouncement(<?php echo htmlspecialchars(json_encode($announce)); ?>)">
                            <div class="announce-icon">
                                <img src="https://api.iconify.design/mdi:bullhorn.svg?color=%23f59e0b" width="18" height="18" alt="">
                            </div>
                            <div class="announce-content">
                                <div class="announce-title"><?php echo htmlspecialchars($announce['title']); ?></div>
                                <div class="announce-time">
                                    <img src="https://api.iconify.design/mdi:clock-outline.svg?color=%236b7280" width="12" height="12" alt="">
                                    <?php echo date('m-d H:i', strtotime($announce['created_at'])); ?>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
                
                <?php if (empty($apps)): ?>
                    <div class="empty-state">
                        <img src="https://api.iconify.design/mdi:package-variant-closed.svg?color=%239ca3af" width="64" height="64" alt="">
                        <p>暂无可购买的应用</p>
                    </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-card-header">
                                    <h3><?php echo htmlspecialchars($app['name']); ?></h3>
                                    <div class="price">¥<?php echo number_format($app['price'], 2); ?></div>
                                </div>
                                <p class="app-desc"><?php echo htmlspecialchars($app['description'] ?: '暂无描述'); ?></p>
                                <div class="app-meta">
                                    <div class="meta-item">
                                        <img src="https://api.iconify.design/mdi:calendar-clock.svg?color=%23f59e0b" width="18" height="18" alt="">
                                        授权期限：<?php echo $app['duration_days'] ? $app['duration_days'] . '天' : '永久'; ?>
                                    </div>
                                </div>
                                <div class="app-card-footer">
                                    <?php if (isLoggedIn()): ?>
                                        <a href="/admin.php?page=buy_app&app_id=<?php echo $app['id']; ?>" class="btn-block">
                                            <img src="https://api.iconify.design/mdi:cart.svg?color=white" width="20" height="20" alt="">
                                            立即购买
                                        </a>
                                    <?php else: ?>
                                        <a href="/modules/home/login.php?redirect=buy&app_id=<?php echo $app['id']; ?>" class="btn-block">
                                            <img src="https://api.iconify.design/mdi:login.svg?color=white" width="20" height="20" alt="">
                                            登录后购买
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </main>
        
        <?php include 'footer.php'; ?>
    </div>
    
    <!-- 公告详情弹窗 -->
    <div id="announcementModal" class="announcement-modal">
        <div class="announcement-modal-content">
            <div class="modal-header">
                <h3 id="modalTitle">
                    <img src="https://api.iconify.design/mdi:bullhorn.svg?color=%23667eea" width="20" height="20" alt="">
                    公告详情
                </h3>
                <button class="modal-close" onclick="closeModal()">
                    <img src="https://api.iconify.design/mdi:close.svg?color=%239ca3af" width="20" height="20" alt="">
                </button>
            </div>
            <div class="modal-body">
                <div id="modalContent"></div>
            </div>
            <div class="announcement-modal-footer">
                <button class="btn btn-secondary" id="dontShowAgainBtn" onclick="closeModal()">
                    <img src="https://api.iconify.design/mdi:bell-off.svg?color=currentColor" width="16" height="16" alt="">
                    不再提示
                </button>
                <button class="btn btn-primary" onclick="closeModal()">关闭</button>
            </div>
        </div>
    </div>
    
    <script>
        // 显示公告详情
        function showAnnouncement(announce) {
            const modal = document.getElementById('announcementModal');
            document.getElementById('modalTitle').innerHTML = `
                <img src="https://api.iconify.design/mdi:bullhorn.svg?color=%23667eea" width="20" height="20" alt="">
                ${announce.title}
            `;
            document.getElementById('modalContent').innerHTML = announce.content;
            modal.style.display = 'flex';
            
            // 记录已读
            fetch('/api/mark_announcement_read.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: `id=${announce.id}`
            });
        }
        
        function closeModal() {
            document.getElementById('announcementModal').style.display = 'none';
        }
        
        // 点击背景关闭
        document.getElementById('announcementModal')?.addEventListener('click', function(e) {
            if (e.target === this) {
                closeModal();
            }
        });
    </script>
</body>
</html>