<?php
ob_start();
/**
 * 2026企业CMS - 后台SEO设置
 */
require_once __DIR__ . '/header.php';
check_permission('admin');
$pageTitle = 'SEO设置';

// 处理保存
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save'])) {
	// CSRF 保护
	if (!csrf_validate($_POST["csrf_token"] ?? "")) {
	    die("CSRF 验证失败");
	}

    foreach ($_POST['settings'] as $key => $value) {
        update_setting($key, $value);
    }
    // 生成sitemap
    generate_sitemap();
    add_log('update', 'seo', '更新SEO设置并生成sitemap');
    header('Location: /admin/seo.php?msg=' . urlencode('保存成功！已生成sitemap.xml'));
    exit;
}

// 获取SEO相关设置
$seoSettings = db_query("SELECT * FROM `settings` WHERE `group`='seo' OR `key` IN ('site_name','site_url','seo_title','seo_keywords','seo_description') ORDER BY `sort_order` ASC");
$siteUrl = get_setting('site_url', 'https://barju.cn');
?>
<div class="page-header">
    <h2>SEO设置</h2>
    <div>
        <a href="<?php echo $siteUrl; ?>/sitemap.xml" target="_blank" class="btn" style="margin-right:10px;">查看Sitemap</a>
        <a href="/admin/settings.php" class="btn">← 返回设置</a>
    </div>
</div>

<form method="post" action="">
        <?php echo csrf_input(); ?>
    <div class="form-card" style="margin-bottom:20px;">
        <h3 style="margin-bottom:20px;font-size:16px;color:#333;border-bottom:2px solid #2c82e0;display:inline-block;padding-bottom:5px;">全局SEO设置</h3>
        
        <div class="form-group">
            <label>默认SEO标题</label>
            <input type="text" name="settings[seo_title]" value="<?php echo xss_clean(get_setting('seo_title')); ?>">
            <div class="hint">留空则使用网站名称</div>
        </div>
        <div class="form-group">
            <label>默认SEO关键词（逗号分隔）</label>
            <input type="text" name="settings[seo_keywords]" value="<?php echo xss_clean(get_setting('seo_keywords')); ?>">
        </div>
        <div class="form-group">
            <label>默认SEO描述</label>
            <textarea name="settings[seo_description]" rows="3"><?php echo xss_clean(get_setting('seo_description')); ?></textarea>
        </div>
    </div>

    <div class="form-card" style="margin-bottom:20px;">
        <h3 style="margin-bottom:20px;font-size:16px;color:#333;border-bottom:2px solid #2c82e0;display:inline-block;padding-bottom:5px;">URL设置</h3>
        
        <div class="form-row">
            <div class="form-group">
                <label>URL模式</label>
                <select name="settings[url_mode]">
                    <option value="1" <?php echo get_setting('url_mode','2')=='1'?'selected':''; ?>>普通模式 (?p=xxx)</option>
                    <option value="2" <?php echo get_setting('url_mode','2')=='2'?'selected':''; ?>>伪静态 (/xxx.html)</option>
                    <option value="3" <?php echo get_setting('url_mode')=='3'?'selected':''; ?>>纯静态 (生成HTML文件)</option>
                </select>
            </div>
            <div class="form-group">
                <label>伪静态后缀</label>
                <select name="settings[伪静态后缀]">
                    <option value=".html" <?php echo get_setting('伪静态后缀','.html')=='.html'?'selected':''; ?>>.html</option>
                    <option value=".htm" <?php echo get_setting('伪静态后缀')=='.htm'?'selected':''; ?>>.htm</option>
                    <option value="/" <?php echo get_setting('伪静态后缀')=='/'?'selected':''; ?>>/ (无后缀)</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label>网站URL（务必修改为实际域名）</label>
            <input type="url" name="settings[site_url]" value="<?php echo xss_clean(get_setting('site_url')); ?>">
            <div class="hint">生成sitemap和SEO链接时使用，如：https://www.example.com</div>
        </div>
    </div>

    <div class="form-card" style="margin-bottom:20px;">
        <h3 style="margin-bottom:20px;font-size:16px;color:#333;border-bottom:2px solid #2c82e0;display:inline-block;padding-bottom:5px;">Sitemap（站点地图）</h3>
        <p style="color:#666;margin-bottom:15px;">Sitemap用于向搜索引擎提交网站页面，提升收录效果。</p>
        <div style="display:flex;gap:15px;">
            <button type="submit" name="save" class="btn btn-primary">保存设置并生成Sitemap</button>
            <a href="<?php echo $siteUrl; ?>/sitemap.xml" target="_blank" class="btn">查看当前Sitemap</a>
        </div>
        <div style="margin-top:15px;color:#999;font-size:13px;">
            Sitemap文件路径：<?php echo __DIR__ . '/../sitemap.xml'; ?>
        </div>
    </div>

    <div style="margin-top:20px;">
        <button type="submit" name="save" class="btn btn-primary" style="padding:12px 40px;font-size:16px;">保存所有设置</button>
    </div>
</form>

<!-- SEO检测提示 -->
<div class="form-card" style="margin-top:30px;">
    <h3 style="margin-bottom:15px;font-size:16px;color:#333;">SEO优化建议</h3>
    <ul style="color:#666;line-height:2;padding-left:20px;">
        <li>确保每个页面都有唯一的<code>&lt;title&gt;</code>标签（不超过60字符）</li>
        <li>确保每个页面都有<code>description</code>描述（不超过160字符）</li>
        <li>使用<code>&lt;h1&gt;</code>到<code>&lt;h6&gt;</code>合理组织页面结构</li>
        <li>图片添加<code>alt</code>属性描述</li>
        <li>在百度搜索资源平台（ziyuan.baidu.com）提交网站</li>
        <li>在Google Search Console提交Sitemap</li>
    </ul>
</div>
<?php require_once __DIR__ . '/footer.php'; ?>
