<?php
require __DIR__ . '/../../app/bootstrap.php';
require __DIR__ . '/../../app/database.php';
require_login();

$packCode = isset($_GET['code']) ? trim((string)$_GET['code']) : '';
$fileKey  = isset($_GET['tab']) ? trim((string)$_GET['tab']) : '';

$manifest = load_template_pack_manifest($packCode);
if (!$manifest) {
    http_response_code(404);
    exit('模板套件不存在。');
}

$packType = isset($manifest['pack_type']) ? (string)$manifest['pack_type'] : 'variation';
if ($packType === 'page') {
    $fileKeys = array(
        'page'   => '页面模板',
        'header' => '页眉模板',
        'footer' => '页脚模板',
        'css'    => '共享样式 (CSS)',
        'js'     => '共享脚本 (JS)'
    );
    if (!isset($fileKeys[$fileKey]) || $fileKey === 'css' || $fileKey === 'js') {
        $fileKey = 'page';
    }
} else {
    $fileKeys = template_pack_file_keys();
    if (!isset($fileKeys[$fileKey]) || $fileKey === 'css' || $fileKey === 'js') {
        $fileKey = 'homepage';
    }
}

$sampleVars = array(
    '{{site_name}}'       => '{{site_name}} 站点名称',
    '{{domain}}'          => '{{domain}} 域名',
    '{{year}}'            => date('Y'),
    '{{title}}'           => '{{title}} 页面标题',
    '{{seo_title}}'       => '{{seo_title}} SEO标题',
    '{{seo_keywords}}'    => '{{seo_keywords}} SEO关键词',
    '{{seo_description}}' => '{{seo_description}} SEO描述',
    '{{path}}'            => '{{path}} 页面路径',
    '{{footer_text}}'     => '{{footer_text}} 页脚文案 © ' . date('Y'),
    '{{menu_nav}}'        => '<a href="#">首页</a><a href="#">关于我们</a><a href="#">产品中心</a><a href="#">新闻资讯</a><a href="#">联系我们</a>',
    '{{friend_links}}'    => '<a href="#">友链A</a> <a href="#">友链B</a> <a href="#">友链C</a>',
    '{{content}}'         => '<p>{{content}} 正文内容 - 这是示例正文，用于预览模板排版效果。</p>',
    '{{article_date}}'    => '{{article_date}} ' . date('Y-m-d'),
    '{{cover_image}}'     => 'data:image/svg+xml,' . rawurlencode('<svg xmlns="http://www.w3.org/2000/svg" width="800" height="400" viewBox="0 0 800 400"><rect fill="#e2e8f0" width="800" height="400"/><text x="400" y="210" text-anchor="middle" fill="#94a3b8" font-size="24">{{cover_image}} 封面图 800×400</text></svg>'),
    '{{excerpt}}'         => '{{excerpt}} 文章摘要示例文字',
    '{{breadcrumb}}'      => '<a href="#">首页</a> &gt; <a href="#">栏目</a> &gt; <span>当前页</span>',
    '{{keyword_links}}'   => '<a href="#">关键词A</a> <a href="#">关键词B</a>',
    '{{article_navigation}}' => '<div class="article-navigation"><a href="#" class="article-nav-prev"><span class="article-nav-label">← 上一篇</span><span class="article-nav-title">上一篇文章标题</span></a><a href="#" class="article-nav-next"><span class="article-nav-label">下一篇 →</span><span class="article-nav-title">下一篇文章标题</span></a></div>',
    '{{related_articles}}'=> '<div><a href="#">相关文章标题一</a></div><div><a href="#">相关文章标题二</a></div>',
    '{{random_articles}}' => '<div><a href="#">随机推荐标题一</a></div><div><a href="#">随机推荐标题二</a></div>',
    '{{latest_articles}}' => '<div><a href="#">最新文章标题一</a></div><div><a href="#">最新文章标题二</a></div>',
    '{{cross_site_articles}}' => '<span style="color:#94a3b8;">{{cross_site_articles}} 跨站互链</span>',
    '{{article_ad_top}}'     => '<div style="background:#f8fafc;padding:8px;text-align:center;color:#94a3b8;font-size:12px;">{{article_ad_top}} 顶部广告位</div>',
    '{{article_ad_middle}}'  => '<div style="background:#f8fafc;padding:8px;text-align:center;color:#94a3b8;font-size:12px;">{{article_ad_middle}} 中部广告位</div>',
    '{{article_ad_bottom}}'  => '<div style="background:#f8fafc;padding:8px;text-align:center;color:#94a3b8;font-size:12px;">{{article_ad_bottom}} 底部广告位</div>',
    '{{article_ad_sidebar}}' => '<div style="background:#f8fafc;padding:8px;text-align:center;color:#94a3b8;font-size:12px;">{{article_ad_sidebar}} 侧边栏广告位</div>',
    '{{menu_keywords}}'      => '{{menu_keywords}} 栏目关键词',
    '{{menu_description}}'   => '{{menu_description}} 栏目描述',
);

$schema = homepage_template_variable_schema();
foreach ($schema as $var => $meta) {
    if (!isset($sampleVars[$var])) {
        $varName = trim($var, '{}');
        $label = isset($meta['label']) ? (string)$meta['label'] : '';
        $sampleVars[$var] = $var . ($label !== '' ? ' ' . $label : '');
    }
}

$allVars = template_variables_for_file_key($fileKey);
foreach ($allVars as $var => $label) {
    if (!isset($sampleVars[$var])) {
        $sampleVars[$var] = $var . ($label !== '' ? ' ' . $label : '');
    }
}

if ($packType === 'page') {
    $GLOBALS['beeai_skip_sitemap_injection'] = true;
}
$html = render_template_pack_page($packCode, $fileKey, $sampleVars);
unset($GLOBALS['beeai_skip_sitemap_injection']);

if ($html === '') {
    echo '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>预览</title></head><body style="display:flex;align-items:center;justify-content:center;height:100vh;font-family:sans-serif;color:#64748b;"><div style="text-align:center;"><h2>暂无内容</h2><p>当前模板文件为空，请先编辑模板内容。</p></div></body></html>';
    exit;
}

echo $html;
