<?php
ob_start();
$user_id = $_SESSION['user_id'];
$user = currentUser();
require_once __DIR__ . '/../../includes/LicenseClient.php';

// 获取系统版本信息
$php_version = PHP_VERSION;
$mysql_version = $db->fetchOne("SELECT VERSION() as ver")['ver'] ?? '未知';
$server_software = $_SERVER['SERVER_SOFTWARE'] ?? '未知';

// 从主站获取数据
try {
    $client = new LicenseClient();
    $current_version = $client->getCurrentVersion();
    $update_info = $client->checkUpdate();
    $has_update = $update_info['has_update'] ?? false;
    $latest_version = $update_info['version'] ?? $current_version;
    
    // 获取主站更新日志
    $remote_changelog = $client->getChangelog();
    $changelog = $client->getChangelog();
} catch (Exception $e) {
    $license_error = $e->getMessage();
    $current_version = defined('SYS_VERSION') ? SYS_VERSION : '1.0.0';
    $latest_version = $current_version;
    $has_update = false;
    $changelog = [];
}
?>

<div class="version-container">
    <!-- 页面头部 -->
    <div class="version-header">
        <h2>
            <img src="https://api.iconify.design/mdi:information.svg?color=%23667eea" width="28" height="28" style="vertical-align: middle; margin-right: 10px;" alt="">
            系统版本信息
        </h2>
        <p>当前程序版本及运行环境信息</p>
    </div>

    <!-- 系统卡片 -->
    <div class="version-card">
        <div class="version-icon">
            <img src="https://api.iconify.design/mdi:rocket-launch.svg?color=%23667eea" width="64" height="64" alt="">
        </div>
        <h3>麟泓授权系统</h3>
        <div class="version-badge">v<?php echo htmlspecialchars($current_version); ?></div>
        <?php if ($has_update): ?>
            <div class="update-tip">
                <img src="https://api.iconify.design/mdi:cloud-download.svg?color=%23f59e0b" width="18" height="18" alt="">
                <span>发现新版本 v<?php echo htmlspecialchars($latest_version); ?>，建议更新</span>
                <span>请联系站点管理员进行更新</span>
                <?php if (isAdmin()): ?>
                    <a href="/admin.php?page=update" class="btn-update-now">立即更新</a>
                <?php endif; ?>
            </div>
        <?php endif; ?>
        <p class="version-desc">专业软件授权管理平台，为开发者提供安全、稳定、高效的授权解决方案。</p>
    </div>

    <!-- 环境信息 -->
    <div class="info-grid">
        <div class="info-item">
            <div class="info-icon">
                <img src="https://api.iconify.design/mdi:package-variant.svg?color=%23667eea" width="36" height="36" alt="">
            </div>
            <div class="info-content">
                <div class="info-label">程序版本</div>
                <div class="info-value">v<?php echo htmlspecialchars($current_version); ?></div>
                <?php if ($has_update): ?>
                    <div class="info-status status-warning">
                        <img src="https://api.iconify.design/mdi:alert-circle.svg?color=%23f59e0b" width="12" height="12" alt="">
                        有新版本可用
                    </div>
                <?php else: ?>
                    <div class="info-status status-good">
                        <img src="https://api.iconify.design/mdi:check-circle.svg?color=%2310b981" width="12" height="12" alt="">
                        已是最新
                    </div>
                <?php endif; ?>
            </div>
        </div>
        <div class="info-item">
            <div class="info-icon">
                <img src="https://api.iconify.design/mdi:language-php.svg?color=%23777bb4" width="36" height="36" alt="">
            </div>
            <div class="info-content">
                <div class="info-label">PHP 版本</div>
                <div class="info-value"><?php echo htmlspecialchars($php_version); ?></div>
                <div class="info-status <?php echo version_compare(PHP_VERSION, '8.0.0', '>=') ? 'status-good' : 'status-warning'; ?>">
                    <?php if (version_compare(PHP_VERSION, '8.0.0', '>=')): ?>
                        <img src="https://api.iconify.design/mdi:check-circle.svg?color=%2310b981" width="12" height="12" alt="">
                        满足要求
                    <?php else: ?>
                        <img src="https://api.iconify.design/mdi:alert-circle.svg?color=%23f59e0b" width="12" height="12" alt="">
                        建议升级到 PHP 8.0+
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <div class="info-item">
            <div class="info-icon">
                <img src="https://api.iconify.design/mdi:database.svg?color=%23f59e0b" width="36" height="36" alt="">
            </div>
            <div class="info-content">
                <div class="info-label">MySQL 版本</div>
                <div class="info-value"><?php echo htmlspecialchars($mysql_version); ?></div>
                <div class="info-status status-good">
                    <img src="https://api.iconify.design/mdi:check-circle.svg?color=%2310b981" width="12" height="12" alt="">
                    运行正常
                </div>
            </div>
        </div>
        <div class="info-item">
            <div class="info-icon">
                <img src="https://api.iconify.design/mdi:server.svg?color=%236b7280" width="36" height="36" alt="">
            </div>
            <div class="info-content">
                <div class="info-label">Web 服务器</div>
                <div class="info-value"><?php echo htmlspecialchars($server_software); ?></div>
            </div>
        </div>
    </div>

    <!-- 系统特性 -->
    <div class="feature-list">
        <h3>
            <img src="https://api.iconify.design/mdi:star.svg?color=%23f59e0b" width="20" height="20" style="margin-right: 8px;" alt="">
            系统特性
        </h3>
        <div class="feature-grid">
            <div class="feature-tag">
                <img src="https://api.iconify.design/mdi:shield-key.svg?color=%23667eea" width="16" height="16" alt="">
                授权验证
            </div>
            <div class="feature-tag">
                <img src="https://api.iconify.design/mdi:credit-card.svg?color=%23667eea" width="16" height="16" alt="">
                在线支付
            </div>
            <div class="feature-tag">
                <img src="https://api.iconify.design/mdi:download.svg?color=%23667eea" width="16" height="16" alt="">
                资源下载
            </div>
            <div class="feature-tag">
                <img src="https://api.iconify.design/mdi:ticket.svg?color=%23667eea" width="16" height="16" alt="">
                工单系统
            </div>
            <div class="feature-tag">
                <img src="https://api.iconify.design/mdi:account-group.svg?color=%23667eea" width="16" height="16" alt="">
                会员管理
            </div>
            <div class="feature-tag">
                <img src="https://api.iconify.design/mdi:chart-bar.svg?color=%23667eea" width="16" height="16" alt="">
                数据统计
            </div>
            <div class="feature-tag">
                <img src="https://api.iconify.design/mdi:weather-night.svg?color=%23667eea" width="16" height="16" alt="">
                深色模式
            </div>
            <div class="feature-tag">
                <img src="https://api.iconify.design/mdi:cellphone.svg?color=%23667eea" width="16" height="16" alt="">
                移动适配
            </div>
            <div class="feature-tag">
                <img src="https://api.iconify.design/mdi:handshake.svg?color=%23667eea" width="16" height="16" alt="">
                代理中心
            </div>
            <div class="feature-tag">
                <img src="https://api.iconify.design/mdi:shield-lock.svg?color=%23667eea" width="16" height="16" alt="">
                PHP加密
            </div>
        </div>
    </div>

    <!-- 更新日志（从主站获取） -->
    <div class="update-log">
        <h3>
            <img src="https://api.iconify.design/mdi:history.svg?color=%23667eea" width="20" height="20" style="margin-right: 8px;" alt="">
            更新日志
        </h3>
        <?php if (empty($changelog)): ?>
            <div class="empty-state">
                <img src="https://api.iconify.design/mdi:note-off.svg?color=%239ca3af" width="48" height="48" alt="">
                <p>暂无更新日志</p>
            </div>
        <?php else: ?>
            <?php foreach ($changelog as $log): ?>
                <div class="log-item <?php echo ($log['is_featured'] ?? false) ? 'featured' : ''; ?>">
                    <div class="log-header">
                        <div class="log-version">
                            <img src="https://api.iconify.design/mdi:tag.svg?color=%23667eea" width="16" height="16" alt="">
                            v<?php echo htmlspecialchars($log['version']); ?>
                            <?php if ($log['is_featured'] ?? false): ?>
                                <span class="featured-badge">
                                    <img src="https://api.iconify.design/mdi:star.svg?color=white" width="12" height="12" alt="">
                                    重要更新
                                </span>
                            <?php endif; ?>
                        </div>
                        <div class="log-date">
                            <img src="https://api.iconify.design/mdi:calendar.svg?color=%236b7280" width="12" height="12" alt="">
                            <?php echo htmlspecialchars($log['release_date'] ?? $log['created_at'] ?? ''); ?>
                        </div>
                    </div>
                    <div class="log-content">
                        <?php 
                        $content = $log['content'] ?? $log['release_notes'] ?? '';
                        $lines = explode("\n", $content);
                        echo '<ul>';
                        foreach ($lines as $line) {
                            $line = trim($line);
                            if (!empty($line)) {
                                $icon = 'circle-small';
                                if (strpos($line, '新增') === 0 || strpos($line, '✨') === 0) {
                                    $icon = 'plus-circle';
                                } elseif (strpos($line, '修复') === 0 || strpos($line, '🐛') === 0) {
                                    $icon = 'bug';
                                } elseif (strpos($line, '优化') === 0 || strpos($line, '🎨') === 0) {
                                    $icon = 'palette';
                                } elseif (strpos($line, '移除') === 0) {
                                    $icon = 'minus-circle';
                                }
                                echo '<li><img src="https://api.iconify.design/mdi:' . $icon . '.svg?color=%23667eea" width="14" height="14" style="margin-right: 6px;" alt="">' . htmlspecialchars($line) . '</li>';
                            }
                        }
                        echo '</ul>';
                        ?>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
        
        <div class="log-footer">
            <img src="https://api.iconify.design/mdi:information.svg?color=%236b7280" width="14" height="14" alt="">
            <span>更新日志由主站同步，保持最新版本信息</span>
        </div>
    </div>
</div>

<style>
/* 基础变量 */
:root {
    --primary-color: #667eea;
    --primary-hover: #5a67d8;
    --primary-light: rgba(102, 126, 234, 0.1);
    --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: 20px;
}

.version-container {
    max-width: 1000px;
    margin: 0 auto;
    padding: 4px;
}

/* 页面头部 */
.version-header {
    text-align: center;
    margin-bottom: 36px;
}
.version-header h2 {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--text-primary);
}
.version-header p {
    color: var(--text-secondary);
    font-size: 15px;
}

/* 系统卡片 */
.version-card {
    background: linear-gradient(135deg, var(--primary-light) 0%, var(--bg-primary) 100%);
    border-radius: var(--radius-lg);
    padding: 44px 40px;
    text-align: center;
    margin-bottom: 32px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}
.version-icon {
    margin-bottom: 20px;
}
.version-card h3 {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 12px;
    color: var(--text-primary);
}
.version-badge {
    display: inline-block;
    background: var(--primary-color);
    color: white;
    padding: 8px 24px;
    border-radius: 40px;
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 16px;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}
.update-tip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: #fef3c7;
    color: #92400e;
    padding: 8px 18px;
    border-radius: 40px;
    font-size: 14px;
    margin-bottom: 16px;
}
.btn-update-now {
    background: #f59e0b;
    color: white;
    padding: 4px 12px;
    border-radius: 20px;
    text-decoration: none;
    font-size: 12px;
    font-weight: 500;
    margin-left: 8px;
    transition: all 0.2s;
}
.btn-update-now:hover {
    background: #d97706;
}
.version-desc {
    color: var(--text-secondary);
    max-width: 500px;
    margin: 0 auto;
    line-height: 1.7;
    font-size: 15px;
}

/* 环境信息网格 */
.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 20px;
    margin-bottom: 32px;
}
.info-item {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 24px;
    display: flex;
    gap: 16px;
    align-items: flex-start;
    border: 1px solid var(--border-color);
    transition: all 0.2s;
    box-shadow: var(--shadow-sm);
}
.info-item:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}
.info-icon {
    flex-shrink: 0;
}
.info-content {
    flex: 1;
}
.info-label {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}
.info-value {
    font-size: 22px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 8px;
}
.info-status {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    padding: 4px 10px;
    border-radius: 30px;
    font-weight: 500;
}
.status-good {
    background: #d1fae5;
    color: #065f46;
}
.status-warning {
    background: #fef3c7;
    color: #92400e;
}

/* 系统特性 */
.feature-list {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 24px 28px;
    margin-bottom: 32px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}
.feature-list h3 {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
}
.feature-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
}
.feature-tag {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--bg-secondary);
    padding: 10px 18px;
    border-radius: 40px;
    font-size: 14px;
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    transition: all 0.2s;
}
.feature-tag:hover {
    border-color: var(--primary-color);
    background: var(--primary-light);
}

/* 更新日志 */
.update-log {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    padding: 28px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}
.update-log h3 {
    display: flex;
    align-items: center;
    margin-bottom: 24px;
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
}
.log-item {
    margin-bottom: 28px;
}
.log-item:last-child {
    margin-bottom: 0;
}
.log-item.featured {
    background: linear-gradient(135deg, rgba(245, 158, 11, 0.05) 0%, var(--bg-primary) 100%);
    border-radius: var(--radius-md);
    padding: 20px;
    margin-bottom: 24px;
    border-left: 3px solid #f59e0b;
}
.log-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    flex-wrap: wrap;
    gap: 10px;
}
.log-version {
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: 600;
    font-size: 16px;
    color: var(--primary-color);
}
.featured-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: #f59e0b;
    color: white;
    padding: 3px 10px;
    border-radius: 30px;
    font-size: 11px;
    font-weight: 500;
}
.log-date {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 13px;
    color: var(--text-muted);
}
.log-content ul {
    margin: 0;
    padding-left: 8px;
    list-style: none;
}
.log-content li {
    display: flex;
    align-items: flex-start;
    color: var(--text-secondary);
    margin: 10px 0;
    line-height: 1.6;
    font-size: 14px;
}
.log-content li img {
    flex-shrink: 0;
    margin-top: 3px;
}
.log-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    margin-top: 24px;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
    font-size: 13px;
    color: var(--text-muted);
}

/* 空状态 */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 20px;
    text-align: center;
}
.empty-state img {
    opacity: 0.4;
    margin-bottom: 12px;
}
.empty-state p {
    color: var(--text-muted);
    font-size: 15px;
    margin: 0;
}

/* 响应式 */
@media (max-width: 768px) {
    .version-card {
        padding: 32px 20px;
    }
    .version-card h3 {
        font-size: 22px;
    }
    .version-badge {
        font-size: 16px;
    }
    .update-tip {
        flex-wrap: wrap;
        justify-content: center;
    }
    .info-grid {
        grid-template-columns: 1fr;
    }
    .info-item {
        padding: 20px;
    }
    .feature-list {
        padding: 20px;
    }
    .update-log {
        padding: 20px;
    }
    .log-header {
        flex-direction: column;
        align-items: flex-start;
    }
}
</style>