<?php
ob_start();
/**
 * 2026企业CMS - 后台模板管理（支持10套模板切换）
 */
require_once __DIR__ . '/../includes/functions.php';

$themes = get_themes();
$currentTheme = get_setting("template") ?: (defined("CURRENT_THEME") ? CURRENT_THEME : "default");

// 处理模板切换（必须在任何输出之前处理，否则 header() 会报错）
if (isset($_GET['activate'])) {
    $slug = trim($_GET['activate']);
    // 清除所有激活状态
    db_exec("UPDATE `themes` SET `is_active`=0");
    // 设置当前模板为激活
    $exists = db_query_one("SELECT `id` FROM `themes` WHERE `slug`=?", [$slug]);
    if ($exists) {
        db_exec("UPDATE `themes` SET `is_active`=1 WHERE `slug`=?", [$slug]);
    } else {
        db_exec("INSERT INTO `themes` (`name`,`slug`,`is_active`) VALUES (?,?,1)", [$slug, $slug]);
    }
    update_setting('template', $slug);
    add_log('theme_switch', 'themes', '切换模板: ' . $slug);
    header('Location: /admin/themes.php?msg=' . urlencode('模板切换成功！'));
    exit;
}

// 所有可能发送 header 的逻辑处理完毕后，再引入 header.php
require_once __DIR__ . '/header.php';
check_permission('admin');
$pageTitle = '模板管理';
?>

<div class="page-header">
    <h2>模板管理</h2>
    <span style="color:#999;">当前模板: <strong style="color:#2c82e0;"><?php echo xss_clean($currentTheme); ?></strong></span>
</div>

<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:20px;">
    <?php foreach ($themes as $t): ?>
    <div class="card" style="position:relative;<?php echo $t['slug']===$currentTheme?'border:2px solid #2c82e0;':''; ?>">
        <!-- 模板缩略图 -->
        <div style="height:180px;background:linear-gradient(135deg,#2c82e0,#1a6bc4);display:flex;align-items:center;justify-content:center;color:#fff;font-size:48px;border-radius:8px 8px 0 0;">
            🎨
        </div>
        <div class="card-body">
            <div class="card-title" style="font-size:16px;"><?php echo xss_clean($t['name']); ?></div>
            <div class="card-desc" style="font-size:12px;color:#999;margin-top:5px;">
                <?php echo xss_clean($t['description'] ?? ''); ?>
            </div>
            <div style="margin-top:10px;font-size:12px;color:#999;">
                标识: <?php echo xss_clean($t['slug']); ?> | 版本: <?php echo xss_clean($t['version'] ?? '1.0'); ?>
            </div>
            <div style="margin-top:15px;display:flex;gap:8px;">
                <?php if ($t['slug'] === $currentTheme): ?>
                <span class="badge badge-success" style="padding:5px 12px;">当前使用中</span>
                <?php else: ?>
                <a href="/admin/themes.php?activate=<?php echo $t['slug']; ?>" class="btn btn-sm btn-primary">启用此模板</a>
                <?php endif; ?>
                <a href="/?preview_theme=<?php echo $t['slug']; ?>" target="_blank" class="btn btn-sm" style="border:1px solid #ddd;">预览</a>
            </div>
        </div>
        <?php if ($t['slug'] === $currentTheme): ?>
        <div style="position:absolute;top:10px;right:10px;background:#28a745;color:#fff;padding:3px 10px;border-radius:10px;font-size:12px;">已启用</div>
        <?php endif; ?>
    </div>
    <?php endforeach; ?>
</div>

<?php
require_once __DIR__ . '/footer.php';
?>
