<?php
ob_start();
require_once __DIR__ . '/../../includes/LicenseClient.php';

$user = currentUser();

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;
    $changelog = $client->getChangelog();
} catch (Exception $e) {
    $current_version = defined('SYS_VERSION') ? SYS_VERSION : '1.0.0';
    $has_update = false;
    $changelog = [];
}

$php_version = PHP_VERSION;
$mysql_version = $db->fetchOne("SELECT VERSION() as ver")['ver'] ?? '未知';
?>

<!-- 当前版本卡片 -->
<div class="card" style="text-align: center;">
    <img src="https://api.iconify.design/mdi:information.svg?color=%23667eea" width="48" height="48" alt="">
    <div style="font-size: 14px; color: #999; margin-top: 8px;">当前版本</div>
    <div style="font-size: 36px; font-weight: 700; color: #667eea;">v<?php echo htmlspecialchars($current_version); ?></div>
</div>

<?php if ($has_update): ?>
<div class="card" style="background: #fef3c7;">
    <div style="display: flex; align-items: center; gap: 12px;">
        <img src="https://api.iconify.design/mdi:cloud-download.svg?color=%23f59e0b" width="24" height="24" alt="">
        <div>
            <div style="font-weight: 600;">发现新版本</div>
            <div>v<?php echo htmlspecialchars($latest_version); ?></div>
        </div>
    </div>
    <p style="margin-top: 12px; color: #92400e;">请联系管理员进行更新</p>
</div>
<?php else: ?>
<div class="card" style="background: #d1fae5; text-align: center;">
    <img src="https://api.iconify.design/mdi:check-circle.svg?color=%2310b981" width="32" height="32" alt="">
    <p style="color: #065f46;">已是最新版本</p>
</div>
<?php endif; ?>

<!-- 系统信息 -->
<div class="card">
    <h3 style="margin-bottom: 12px; font-size: 15px;">系统信息</h3>
    <div style="display: flex; flex-direction: column; gap: 10px;">
        <div style="display: flex; justify-content: space-between;">
            <span style="color: #999;">PHP版本</span>
            <span><?php echo $php_version; ?></span>
        </div>
        <div style="display: flex; justify-content: space-between;">
            <span style="color: #999;">MySQL版本</span>
            <span><?php echo htmlspecialchars($mysql_version); ?></span>
        </div>
    </div>
</div>

<!-- 系统特性 -->
<div class="card">
    <h3 style="margin-bottom: 12px; font-size: 15px;">系统特性</h3>
    <div style="display: flex; flex-wrap: wrap; gap: 8px;">
        <span style="background:#f5f5f5; padding:6px 12px; border-radius:20px; font-size:12px;">
            <img src="https://api.iconify.design/mdi:shield-key.svg?color=%23667eea" width="14" height="14" alt=""> 授权验证
        </span>
        <span style="background:#f5f5f5; padding:6px 12px; border-radius:20px; font-size:12px;">
            <img src="https://api.iconify.design/mdi:credit-card.svg?color=%23667eea" width="14" height="14" alt=""> 在线支付
        </span>
        <span style="background:#f5f5f5; padding:6px 12px; border-radius:20px; font-size:12px;">
            <img src="https://api.iconify.design/mdi:cellphone.svg?color=%23667eea" width="14" height="14" alt=""> 移动适配
        </span>
        <span style="background:#f5f5f5; padding:6px 12px; border-radius:20px; font-size:12px;">
            <img src="https://api.iconify.design/mdi:ticket.svg?color=%23667eea" width="14" height="14" alt=""> 工单系统
        </span>
    </div>
</div>

<!-- 更新日志 -->
<div class="card">
    <h3 style="margin-bottom: 16px; display: flex; align-items: center; gap: 8px;">
        <img src="https://api.iconify.design/mdi:history.svg?color=%23667eea" width="20" height="20" alt="">
        更新日志
    </h3>
    
    <?php if (empty($changelog)): ?>
        <div style="text-align: center; padding: 30px; color: #999;">
            <img src="https://api.iconify.design/mdi:note-off.svg?color=%23999" width="32" height="32" alt="">
            <p style="margin-top: 8px;">暂无更新日志</p>
        </div>
    <?php else: ?>
        <?php foreach (array_slice($changelog, 0, 5) as $log): ?>
            <div style="margin-bottom: 20px; padding-bottom: 16px; border-bottom: 1px solid #f0f0f0;">
                <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px;">
                    <div style="display: flex; align-items: center; gap: 6px;">
                        <img src="https://api.iconify.design/mdi:tag.svg?color=%23667eea" width="14" height="14" alt="">
                        <span style="font-weight: 600; color: #667eea;">v<?php echo htmlspecialchars($log['version']); ?></span>
                        <?php if ($log['is_featured'] ?? false): ?>
                            <span style="background: #f59e0b; color: white; padding: 2px 8px; border-radius: 20px; font-size: 10px;">重要</span>
                        <?php endif; ?>
                    </div>
                    <span style="font-size: 11px; color: #999;">
                        <?php echo htmlspecialchars($log['release_date'] ?? $log['created_at'] ?? ''); ?>
                    </span>
                </div>
                <div style="font-size: 13px; color: #666; line-height: 1.6;">
                    <?php 
                    $content = $log['content'] ?? $log['release_notes'] ?? '';
                    $lines = explode("\n", $content);
                    foreach ($lines as $line):
                        $line = trim($line);
                        if (!empty($line)):
                    ?>
                        <div style="display: flex; margin-bottom: 4px;">
                            <span style="margin-right: 8px; color: #667eea;">•</span>
                            <span><?php echo htmlspecialchars($line); ?></span>
                        </div>
                    <?php 
                        endif;
                    endforeach;
                    ?>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
    
    <div style="text-align: center; margin-top: 12px; font-size: 12px; color: #999;">
        <img src="https://api.iconify.design/mdi:information.svg?color=%23999" width="12" height="12" alt="">
        更新日志由主站同步
    </div>
</div>