<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-5-20
 */

require_once __DIR__ . '/../../core/lib/license.php';
$license = new \Core\Lib\License();
$licenseLevel = $license->getLevel();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    requireAdminCsrfToken();
    $configs = isset($_POST['config']) ? $_POST['config'] : [];
    foreach ($configs as $key => $value) {
        $exists = $db->fetch("SELECT id FROM {$db->table('config')} WHERE config_key = ?", [$key]);
        if ($exists) {
            $db->update('config', ['config_value' => $value], 'config_key = ?', [$key]);
        } else {
            $db->insert('config', [
                'config_key' => $key,
                'config_value' => $value,
                'config_type' => 'text',
                'config_group' => 'basic'
            ]);
        }
    }
    echo '<script>alert("保存成功");location.href="?act=site"</script>';
    cmsWriteSeoStaticFiles();
    cmsWriteRewriteConfigFiles();
    exit;
}

$allConfigs = $db->fetchAll("SELECT * FROM {$db->table('config')} WHERE config_group = 'basic' ORDER BY id ASC");
$configData = [];
foreach ($allConfigs as $cfg) {
    $configData[$cfg['config_key']] = $cfg['config_value'];
}

$siteStatus = isset($configData['site_status']) ? $configData['site_status'] : 'open';
$updateRemind = isset($configData['update_remind']) ? $configData['update_remind'] : 'modal';
$logoUrl = !empty($configData['site_logo']) ? htmlspecialchars($configData['site_logo']) : '';
$wmUrl = !empty($configData['watermark_image']) ? htmlspecialchars($configData['watermark_image']) : '';
$rewriteRules = cmsGenerateRewriteRules(trim(parse_url((string) ($configData['site_url'] ?? ''), PHP_URL_PATH), '/'));
$urlModeValue = isset($configData['url_mode']) ? $configData['url_mode'] : 'dynamic';
$rewriteAutoEnabled = isset($configData['rewrite_auto_enabled']) ? $configData['rewrite_auto_enabled'] : '0';
$internalLinkEnabled = isset($configData['seo_internal_link_enabled']) ? $configData['seo_internal_link_enabled'] : '0';
$internalLinkMax = isset($configData['seo_internal_link_max']) ? $configData['seo_internal_link_max'] : '3';
$indexNowEnabled = isset($configData['indexnow_enabled']) ? $configData['indexnow_enabled'] : '0';
$baiduPushEnabled = isset($configData['baidu_push_enabled']) ? $configData['baidu_push_enabled'] : '0';
$baiduPushToken = isset($configData['baidu_push_token']) ? $configData['baidu_push_token'] : '';
$robotsExtraRules = isset($configData['robots_extra_rules']) ? $configData['robots_extra_rules'] : '';
$sitemapUrl = cmsAbsoluteUrl('/sitemap.xml');
$robotsUrl = cmsAbsoluteUrl('/robots.txt');
$indexNowKey = !empty($configData['indexnow_key']) ? $configData['indexnow_key'] : '';
$indexNowKeyUrl = $indexNowKey !== '' ? cmsAbsoluteUrl('/' . $indexNowKey . '.txt') : '';
?>
<div class="page-container fade-in">
    <div class="page-header">
        <div class="page-title">
            <div class="page-icon">
                <svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <circle cx="12" cy="12" r="3"></circle>
                    <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"></path>
                </svg>
            </div>
            <div>
                <h1>站点设置</h1>
                <p style="font-size:12px;color:#64748b;margin:0;">维护网站基础资料、展示资源与系统级参数。</p>
            </div>
        </div>
        <div class="page-actions">
            <button type="submit" form="siteForm" class="btn btn-success btn-lg">
                <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"></path>
                    <polyline points="17 21 17 13 7 13 7 21"></polyline>
                    <polyline points="7 3 7 8 15 8"></polyline>
                </svg>
                保存设置
            </button>
        </div>
    </div>

    <div class="site-page-overview">
        <div class="site-overview-item">
            <span class="overview-label">当前站点</span>
            <span class="overview-value"><?php echo !empty($configData['site_name']) ? htmlspecialchars($configData['site_name']) : '未填写站点名称'; ?></span>
        </div>
        <div class="site-overview-item">
            <span class="overview-label">访问状态</span>
            <span class="overview-value <?php echo $siteStatus === 'open' ? 'is-open' : 'is-close'; ?>"><?php echo $siteStatus === 'open' ? '正常开放' : '维护关闭'; ?></span>
        </div>
        <div class="site-overview-item">
            <span class="overview-label">更新提醒</span>
            <span class="overview-value"><?php echo $updateRemind === 'modal' ? '弹窗提醒' : ($updateRemind === 'backend' ? '后台提醒' : '已关闭提醒'); ?></span>
        </div>
        <div class="site-overview-item">
            <span class="overview-label">信息完整度</span>
            <span class="overview-value"><?php echo !empty($configData['site_url']) && !empty($configData['site_phone']) ? '基础信息已补齐' : '建议继续补充联系信息'; ?></span>
        </div>
    </div>

    <form method="post" id="siteForm">
        <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars(getAdminCsrfToken()); ?>">

        <div class="site-tabs">
            <button type="button" class="site-tab active" data-tab="basic" onclick="return switchSiteTab(this);">
                <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <circle cx="12" cy="12" r="3"></circle>
                    <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06A1.65 1.65 0 0 0 15 19.4 1.65 1.65 0 0 0 13.5 21v0a2 2 0 0 1-3 0v0A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.6 15 1.65 1.65 0 0 0 3 13.5h0a2 2 0 0 1 0-3h0A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.6 1.65 1.65 0 0 0 10.5 3h0a2 2 0 0 1 3 0h0A1.65 1.65 0 0 0 15 4.6a1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9 1.65 1.65 0 0 0 21 10.5h0a2 2 0 0 1 0 3h0A1.65 1.65 0 0 0 19.4 15z"></path>
                </svg>
                基础配置
            </button>
            <button type="button" class="site-tab" data-tab="seo" onclick="return switchSiteTab(this);">
                <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <circle cx="11" cy="11" r="8"></circle>
                    <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
                </svg>
                SEO设置
            </button>
            <button type="button" class="site-tab" data-tab="company" onclick="return switchSiteTab(this);">
                <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M3 21h18"></path>
                    <path d="M5 21V7l8-4v18"></path>
                    <path d="M19 21V11l-6-4"></path>
                    <path d="M9 9h1"></path>
                    <path d="M9 13h1"></path>
                    <path d="M9 17h1"></path>
                </svg>
                公司介绍与联系方式
            </button>
        </div>

        <div class="site-tab-panel active" id="tab-basic">
            <div class="form-section">
                <div class="form-section-header">
                    <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
                        <polyline points="14 2 14 8 20 8"></polyline>
                        <line x1="16" y1="13" x2="8" y2="13"></line>
                        <line x1="16" y1="17" x2="8" y2="17"></line>
                    </svg>
                    基本信息
                </div>
                <div class="form-section-body">
                    <div class="form-row">
                        <div class="form-group">
                            <label>网站名称</label>
                            <input type="text" name="config[site_name]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['site_name']) ? $configData['site_name'] : ''); ?>" placeholder="请输入网站名称">
                        </div>
                        <div class="form-group">
                            <label>网站URL</label>
                            <input type="text" name="config[site_url]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['site_url']) ? $configData['site_url'] : ''); ?>" placeholder="如：http://www.example.com">
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label>ICP备案号</label>
                            <input type="text" name="config[site_icp]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['site_icp']) ? $configData['site_icp'] : ''); ?>" placeholder="如：京ICP备xxxxxx号">
                        </div>
                        <div class="form-group">
                            <label>公安备案号</label>
                            <input type="text" name="config[site_police_icp]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['site_police_icp']) ? $configData['site_police_icp'] : ''); ?>" placeholder="如：京公网安备xxxxxx号">
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label>IndexNow 主动推送</label>
                            <select name="config[indexnow_enabled]" class="form-control">
                                <option value="1" <?php echo (string) $indexNowEnabled === '1' ? 'selected' : ''; ?>>开启</option>
                                <option value="0" <?php echo (string) $indexNowEnabled !== '1' ? 'selected' : ''; ?>>关闭</option>
                            </select>
                            <p class="form-help">保存后自动生成 IndexNow 验证文件，并可在 SEO 工具中一键推送。</p>
                        </div>
                        <div class="form-group">
                            <label>百度主动推送</label>
                            <select name="config[baidu_push_enabled]" class="form-control">
                                <option value="1" <?php echo (string) $baiduPushEnabled === '1' ? 'selected' : ''; ?>>开启</option>
                                <option value="0" <?php echo (string) $baiduPushEnabled !== '1' ? 'selected' : ''; ?>>关闭</option>
                            </select>
                            <p class="form-help">开启后可将站点 URL 批量主动提交到百度。</p>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label>百度推送 Token</label>
                            <input type="text" name="config[baidu_push_token]" class="form-control" value="<?php echo htmlspecialchars((string) $baiduPushToken); ?>" placeholder="Baidu push token">
                        </div>
                        <div class="form-group">
                            <label>Robots 额外规则</label>
                            <textarea name="config[robots_extra_rules]" class="form-control" rows="3" placeholder="Disallow: /member/"><?php echo htmlspecialchars((string) $robotsExtraRules); ?></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>SEO 静态文件</label>
                        <div style="display:grid;gap:8px;">
                            <div class="form-help" style="margin:0;">Sitemap: <a href="<?php echo htmlspecialchars($sitemapUrl); ?>" target="_blank"><?php echo htmlspecialchars($sitemapUrl); ?></a></div>
                            <div class="form-help" style="margin:0;">Robots: <a href="<?php echo htmlspecialchars($robotsUrl); ?>" target="_blank"><?php echo htmlspecialchars($robotsUrl); ?></a></div>
                            <?php if ($indexNowKeyUrl !== ''): ?>
                                <div class="form-help" style="margin:0;">IndexNow Key: <a href="<?php echo htmlspecialchars($indexNowKeyUrl); ?>" target="_blank"><?php echo htmlspecialchars($indexNowKeyUrl); ?></a></div>
                            <?php else: ?>
                                <div class="form-help" style="margin:0;">IndexNow Key: 开启并保存后自动生成。</div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-section">
                <div class="form-section-header">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
                        <circle cx="8.5" cy="8.5" r="1.5"></circle>
                        <polyline points="21 15 16 10 5 21"></polyline>
                    </svg>
                    站点形象
                </div>
                <div class="form-section-body">
                    <div class="form-row">
                        <div class="form-group">
                            <label>网站LOGO</label>
                            <div class="upload-box" id="logoUploadBox">
                                <input type="hidden" name="config[site_logo]" id="site_logo" value="<?php echo htmlspecialchars(isset($configData['site_logo']) ? $configData['site_logo'] : ''); ?>">
                                <?php if (!empty($logoUrl)): ?>
                                <div class="upload-preview" id="logoPreview">
                                    <img src="<?php echo $logoUrl; ?>" id="logoImg">
                                    <span class="upload-remove" onclick="removeUpload('logo')">×</span>
                                </div>
                                <div class="upload-placeholder" id="logoPlaceholder" onclick="triggerUpload('logo')" style="display:none;">
                                    <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
                                        <polyline points="17 8 12 3 7 8"></polyline>
                                        <line x1="12" y1="3" x2="12" y2="15"></line>
                                    </svg>
                                    <span>点击上传LOGO</span>
                                </div>
                                <?php else: ?>
                                <div class="upload-placeholder" id="logoPlaceholder" onclick="triggerUpload('logo')">
                                    <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
                                        <polyline points="17 8 12 3 7 8"></polyline>
                                        <line x1="12" y1="3" x2="12" y2="15"></line>
                                    </svg>
                                    <span>点击上传LOGO</span>
                                </div>
                                <div class="upload-preview" id="logoPreview" style="display:none;">
                                    <img src="" id="logoImg">
                                    <span class="upload-remove" onclick="removeUpload('logo')">×</span>
                                </div>
                                <?php endif; ?>
                            </div>
                            <input type="file" id="logoFile" style="display:none;" accept="image/*" onchange="handleFileUpload(this, 'logo')">
                            <p class="form-help">建议尺寸：200×60 像素，支持 JPG、PNG、SVG。</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-section">
                <div class="form-section-header">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <line x1="4" y1="21" x2="4" y2="14"></line>
                        <line x1="4" y1="10" x2="4" y2="3"></line>
                        <line x1="12" y1="21" x2="12" y2="12"></line>
                        <line x1="12" y1="8" x2="12" y2="3"></line>
                        <line x1="20" y1="21" x2="20" y2="16"></line>
                        <line x1="20" y1="12" x2="20" y2="3"></line>
                        <line x1="1" y1="14" x2="7" y2="14"></line>
                        <line x1="9" y1="8" x2="15" y2="8"></line>
                        <line x1="17" y1="16" x2="23" y2="16"></line>
                    </svg>
                    系统设置
                </div>
                <div class="form-section-body">
                    <div class="form-row">
                        <div class="form-group">
                            <label>系统更新提醒</label>
                            <select name="config[update_remind]" class="form-control">
                                <option value="modal" <?php echo $updateRemind === 'modal' ? 'selected' : ''; ?>>弹窗提醒</option>
                                <option value="backend" <?php echo $updateRemind === 'backend' ? 'selected' : ''; ?>>仅后台提醒</option>
                                <option value="off" <?php echo $updateRemind === 'off' ? 'selected' : ''; ?>>关闭提醒</option>
                            </select>
                            <p class="form-help">建议保留提醒，便于及时获知程序更新与后台提示。</p>
                        </div>
                        <div class="form-group">
                            <label>站点状态</label>
                            <div class="toggle-group" id="siteStatusGroup">
                                <label class="toggle-item<?php echo $siteStatus === 'open' ? ' active' : ''; ?>" data-value="open">
                                    <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <circle cx="12" cy="12" r="10"></circle>
                                        <line x1="2" y1="12" x2="22" y2="12"></line>
                                        <path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"></path>
                                    </svg>
                                    开启
                                </label>
                                <label class="toggle-item<?php echo $siteStatus === 'close' ? ' active' : ''; ?>" data-value="close">
                                    <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <circle cx="12" cy="12" r="10"></circle>
                                        <line x1="4.93" y1="4.93" x2="19.07" y2="19.07"></line>
                                    </svg>
                                    关闭
                                </label>
                            </div>
                            <input type="hidden" name="config[site_status]" id="siteStatusInput" value="<?php echo htmlspecialchars($siteStatus); ?>">
                            <p class="form-help">关闭后前台仅显示维护提示，适合临时停站或维护升级。</p>
                        </div>
                    </div>

                    <div class="form-group" id="closeReasonGroup" style="<?php echo $siteStatus === 'close' ? '' : 'display:none;'; ?>">
                        <label>关站提示信息</label>
                        <textarea name="config[site_close_msg]" class="form-control" rows="3" placeholder="站点关闭时显示的提示信息"><?php echo htmlspecialchars(isset($configData['site_close_msg']) ? $configData['site_close_msg'] : ''); ?></textarea>
                    </div>

                    <?php if ($licenseLevel == 1): ?>
                    <div class="license-tip-box">
                        <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                            <circle cx="12" cy="12" r="10"></circle>
                            <line x1="12" y1="8" x2="12" y2="12"></line>
                            <line x1="12" y1="16" x2="12.01" y2="16"></line>
                        </svg>
                        <div>
                            <strong>免费版限制：</strong>产品数量最多 <strong>5个</strong>，升级到基础版或专业版后可无限制使用。
                            <a href="?act=version">立即升级</a>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>

            <div class="form-section">
                <div class="form-section-header">
                    <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <circle cx="11" cy="11" r="8"></circle>
                        <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
                    </svg>
                    SEO设置
                </div>
                <div class="form-section-body">
                    <div class="form-group">
                        <label>SEO标题</label>
                        <input type="text" name="config[seo_title]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['seo_title']) ? $configData['seo_title'] : ''); ?>" placeholder="SEO标题">
                    </div>
                    <div class="form-group">
                        <label>SEO关键字</label>
                        <input type="text" name="config[seo_keywords]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['seo_keywords']) ? $configData['seo_keywords'] : ''); ?>" placeholder="关键字1,关键字2,关键字3">
                    </div>
                    <div class="form-group">
                        <label>SEO描述</label>
                        <textarea name="config[seo_description]" class="form-control" rows="3" placeholder="网站描述"><?php echo htmlspecialchars(isset($configData['seo_description']) ? $configData['seo_description'] : ''); ?></textarea>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label>访问模式</label>
                            <select name="config[url_mode]" class="form-control">
                                <option value="dynamic" <?php echo $urlModeValue === 'dynamic' ? 'selected' : ''; ?>>动态链接</option>
                                <option value="static" <?php echo $urlModeValue !== 'dynamic' ? 'selected' : ''; ?>>伪静态链接</option>
                            </select>
                            <p class="form-help">开启后，前台链接会优先输出为 `/article/123.html` 这类伪静态地址。</p>
                        </div>
                        <div class="form-group">
                            <label>伪静态自动适配</label>
                            <select name="config[rewrite_auto_enabled]" class="form-control">
                                <option value="1" <?php echo (string) $rewriteAutoEnabled === '1' ? 'selected' : ''; ?>>开启</option>
                                <option value="0" <?php echo (string) $rewriteAutoEnabled !== '1' ? 'selected' : ''; ?>>关闭</option>
                            </select>
                            <p class="form-help">未正确配置服务器规则时，系统会尽量自动识别伪静态请求并回退解析。</p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Apache 伪静态规则</label>
                        <textarea class="form-control" rows="8" readonly><?php echo htmlspecialchars($rewriteRules['apache']); ?></textarea>
                    </div>
                    <div class="form-group">
                        <label>Nginx 伪静态规则</label>
                        <textarea class="form-control" rows="10" readonly><?php echo htmlspecialchars($rewriteRules['nginx']); ?></textarea>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label>智能内链</label>
                            <select name="config[seo_internal_link_enabled]" class="form-control">
                                <option value="1" <?php echo (string) $internalLinkEnabled === '1' ? 'selected' : ''; ?>>开启</option>
                                <option value="0" <?php echo (string) $internalLinkEnabled !== '1' ? 'selected' : ''; ?>>关闭</option>
                            </select>
                            <p class="form-help">系统会在正文中自动为站内标题关键词补上相关文章、产品、单页链接。</p>
                        </div>
                        <div class="form-group">
                            <label>单页最多自动内链数</label>
                            <input type="number" min="1" max="10" name="config[seo_internal_link_max]" class="form-control" value="<?php echo htmlspecialchars((string) $internalLinkMax); ?>">
                            <p class="form-help">建议 2 到 5，避免正文里链接过密。</p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>死链检测与修复</label>
                        <div style="display:flex;gap:12px;flex-wrap:wrap;">
                            <a href="/admin/main.php?act=seo_tools" class="btn btn-primary">打开 SEO 工具</a>
                            <span class="form-help" style="margin:0;display:flex;align-items:center;">可扫描文章、产品、单页等正文中的坏链，并给出修复建议。</span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-section">
                <div class="form-section-header">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
                        <circle cx="8.5" cy="8.5" r="1.5"></circle>
                        <polyline points="21 15 16 10 5 21"></polyline>
                    </svg>
                    水印设置
                </div>
                <div class="form-section-body">
                    <div class="form-row">
                        <div class="form-group">
                            <label>图标水印</label>
                            <div class="upload-box" id="watermarkUploadBox">
                                <input type="hidden" name="config[watermark_image]" id="watermark_image" value="<?php echo htmlspecialchars(isset($configData['watermark_image']) ? $configData['watermark_image'] : ''); ?>">
                                <?php if ($wmUrl): ?>
                                <div class="upload-preview" id="watermarkPreview">
                                    <img src="<?php echo $wmUrl; ?>" id="watermarkImg">
                                    <span class="upload-remove" onclick="removeUpload('watermark')">×</span>
                                </div>
                                <div class="upload-placeholder" id="watermarkPlaceholder" onclick="triggerUpload('watermark')" style="display:none;">
                                    <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
                                        <polyline points="17 8 12 3 7 8"></polyline>
                                        <line x1="12" y1="3" x2="12" y2="15"></line>
                                    </svg>
                                    <span>点击上传水印图标</span>
                                </div>
                                <?php else: ?>
                                <div class="upload-placeholder" id="watermarkPlaceholder" onclick="triggerUpload('watermark')">
                                    <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
                                        <polyline points="17 8 12 3 7 8"></polyline>
                                        <line x1="12" y1="3" x2="12" y2="15"></line>
                                    </svg>
                                    <span>点击上传水印图标</span>
                                </div>
                                <div class="upload-preview" id="watermarkPreview" style="display:none;">
                                    <img src="" id="watermarkImg">
                                    <span class="upload-remove" onclick="removeUpload('watermark')">×</span>
                                </div>
                                <?php endif; ?>
                            </div>
                            <input type="file" id="watermarkFile" style="display:none;" accept="image/*" onchange="handleFileUpload(this, 'watermark')">
                            <p class="form-help">建议使用透明 PNG，便于覆盖在图片资源上。</p>
                        </div>
                        <div class="form-group">
                            <label>文字水印内容</label>
                            <input type="text" name="config[watermark_text]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['watermark_text']) ? $configData['watermark_text'] : ''); ?>" placeholder="如：品牌名称">
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label>水印位置</label>
                            <select name="config[watermark_position]" class="form-control">
                                <option value="右下" <?php echo (isset($configData['watermark_position']) ? $configData['watermark_position'] : '右下') === '右下' ? 'selected' : ''; ?>>右下角</option>
                                <option value="左下" <?php echo (isset($configData['watermark_position']) ? $configData['watermark_position'] : '右下') === '左下' ? 'selected' : ''; ?>>左下角</option>
                                <option value="右上" <?php echo (isset($configData['watermark_position']) ? $configData['watermark_position'] : '右下') === '右上' ? 'selected' : ''; ?>>右上角</option>
                                <option value="左上" <?php echo (isset($configData['watermark_position']) ? $configData['watermark_position'] : '右下') === '左上' ? 'selected' : ''; ?>>左上角</option>
                                <option value="居中" <?php echo (isset($configData['watermark_position']) ? $configData['watermark_position'] : '右下') === '居中' ? 'selected' : ''; ?>>正中间</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>水印透明度</label>
                            <select name="config[watermark_alpha]" class="form-control">
                                <option value="0.3" <?php echo ((isset($configData['watermark_alpha']) ? $configData['watermark_alpha'] : '0.5')) === '0.3' ? 'selected' : ''; ?>>30%</option>
                                <option value="0.5" <?php echo ((isset($configData['watermark_alpha']) ? $configData['watermark_alpha'] : '0.5')) === '0.5' ? 'selected' : ''; ?>>50%</option>
                                <option value="0.7" <?php echo ((isset($configData['watermark_alpha']) ? $configData['watermark_alpha'] : '0.5')) === '0.7' ? 'selected' : ''; ?>>70%</option>
                                <option value="1.0" <?php echo ((isset($configData['watermark_alpha']) ? $configData['watermark_alpha'] : '0.5')) === '1.0' ? 'selected' : ''; ?>>100%</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="site-tab-panel" id="tab-seo">
            <div class="form-section">
                <div class="form-section-header">
                    <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <circle cx="11" cy="11" r="8"></circle>
                        <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
                    </svg>
                    SEO设置
                </div>
                <div class="form-section-body">
                    <div class="form-group">
                        <label>SEO标题</label>
                        <input type="text" name="config[seo_title]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['seo_title']) ? $configData['seo_title'] : ''); ?>" placeholder="SEO标题">
                    </div>
                    <div class="form-group">
                        <label>SEO关键字</label>
                        <input type="text" name="config[seo_keywords]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['seo_keywords']) ? $configData['seo_keywords'] : ''); ?>" placeholder="关键词,关键词,关键词">
                    </div>
                    <div class="form-group">
                        <label>SEO描述</label>
                        <textarea name="config[seo_description]" class="form-control" rows="3" placeholder="网站描述"><?php echo htmlspecialchars(isset($configData['seo_description']) ? $configData['seo_description'] : ''); ?></textarea>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label>访问模式</label>
                            <select name="config[url_mode]" class="form-control">
                                <option value="dynamic" <?php echo $urlModeValue === 'dynamic' ? 'selected' : ''; ?>>动态链接</option>
                                <option value="static" <?php echo $urlModeValue !== 'dynamic' ? 'selected' : ''; ?>>伪静态链接</option>
                            </select>
                            <p class="form-help">开启后，前台链接优先输出为 `/article/123.html` 这类伪静态地址。</p>
                        </div>
                        <div class="form-group">
                            <label>伪静态自动适配</label>
                            <select name="config[rewrite_auto_enabled]" class="form-control">
                                <option value="1" <?php echo (string) $rewriteAutoEnabled === '1' ? 'selected' : ''; ?>>开启</option>
                                <option value="0" <?php echo (string) $rewriteAutoEnabled !== '1' ? 'selected' : ''; ?>>关闭</option>
                            </select>
                            <p class="form-help">未完整配置服务器规则时，系统会尽量自动识别伪静态请求并回退解析。</p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Apache 伪静态规则</label>
                        <textarea class="form-control" rows="8" readonly><?php echo htmlspecialchars($rewriteRules['apache']); ?></textarea>
                    </div>
                    <div class="form-group">
                        <label>Nginx 伪静态规则</label>
                        <textarea class="form-control" rows="10" readonly><?php echo htmlspecialchars($rewriteRules['nginx']); ?></textarea>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label>智能内链</label>
                            <select name="config[seo_internal_link_enabled]" class="form-control">
                                <option value="1" <?php echo (string) $internalLinkEnabled === '1' ? 'selected' : ''; ?>>开启</option>
                                <option value="0" <?php echo (string) $internalLinkEnabled !== '1' ? 'selected' : ''; ?>>关闭</option>
                            </select>
                            <p class="form-help">系统会在正文中自动为站内标题关键词补上相关文章、产品、单页链接。</p>
                        </div>
                        <div class="form-group">
                            <label>单页最大自动内链数</label>
                            <input type="number" min="1" max="10" name="config[seo_internal_link_max]" class="form-control" value="<?php echo htmlspecialchars((string) $internalLinkMax); ?>">
                            <p class="form-help">建议 2 到 5，避免正文里链接过密。</p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>死链检测与修复</label>
                        <div style="display:flex;gap:12px;flex-wrap:wrap;">
                            <a href="/admin/main.php?act=seo_tools" class="btn btn-primary">打开 SEO 工具</a>
                            <span class="form-help" style="margin:0;display:flex;align-items:center;">可扫描文章、产品、单页等正文中的坏链，并给出修复建议。</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="site-tab-panel" id="tab-company">
            <div class="form-section">
                <div class="form-section-header">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M3 21h18"></path>
                        <path d="M5 21V7l8-4v18"></path>
                        <path d="M19 21V11l-6-4"></path>
                        <path d="M9 9h1"></path>
                        <path d="M9 13h1"></path>
                        <path d="M9 17h1"></path>
                    </svg>
                    公司介绍
                </div>
                <div class="form-section-body">
                    <div class="form-group">
                        <label>公司介绍</label>
                        <textarea name="config[company_intro]" class="form-control" rows="8" placeholder="请输入公司介绍，可用于前台关于我们、页脚等位置展示"><?php echo htmlspecialchars(isset($configData['company_intro']) ? $configData['company_intro'] : ''); ?></textarea>
                        <p class="form-help">这里保留完整介绍内容，不再在后台顶部额外显示摘要卡。</p>
                    </div>
                </div>
            </div>

            <div class="form-section">
                <div class="form-section-header">
                    <svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.8 19.8 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6A19.8 19.8 0 0 1 2.12 4.18 2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.12.91.33 1.8.63 2.65a2 2 0 0 1-.45 2.11L8.09 9.69a16 16 0 0 0 6.22 6.22l1.21-1.21a2 2 0 0 1 2.11-.45c.85.3 1.74.51 2.65.63A2 2 0 0 1 22 16.92z"></path>
                    </svg>
                    联系方式
                </div>
                <div class="form-section-body">
                    <div class="form-row">
                        <div class="form-group">
                            <label>联系人</label>
                            <input type="text" name="config[contact_person]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['contact_person']) ? $configData['contact_person'] : ''); ?>" placeholder="如：张经理">
                        </div>
                        <div class="form-group">
                            <label>联系电话</label>
                            <input type="text" name="config[site_phone]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['site_phone']) ? $configData['site_phone'] : ''); ?>" placeholder="如：400-888-8888">
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label>联系邮箱</label>
                            <input type="email" name="config[site_email]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['site_email']) ? $configData['site_email'] : ''); ?>" placeholder="admin@example.com">
                        </div>
                        <div class="form-group">
                            <label>联系地址</label>
                            <input type="text" name="config[site_address]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['site_address']) ? $configData['site_address'] : ''); ?>" placeholder="请输入详细地址">
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label>微信号</label>
                            <input type="text" name="config[contact_wechat]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['contact_wechat']) ? $configData['contact_wechat'] : ''); ?>" placeholder="请输入微信号">
                        </div>
                        <div class="form-group">
                            <label>QQ</label>
                            <input type="text" name="config[contact_qq]" class="form-control" value="<?php echo htmlspecialchars(isset($configData['contact_qq']) ? $configData['contact_qq'] : ''); ?>" placeholder="请输入QQ号码">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<style>
.site-page-overview {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}

.site-overview-item {
    background: linear-gradient(180deg, #ffffff 0%, #f8fbff 100%);
    border: 1px solid #e2e8f0;
    border-radius: 14px;
    padding: 14px 16px;
    box-shadow: 0 8px 20px rgba(15, 23, 42, 0.04);
}

.overview-label {
    display: block;
    font-size: 12px;
    color: #94a3b8;
    margin-bottom: 6px;
}

.overview-value {
    display: block;
    font-size: 14px;
    font-weight: 700;
    color: #1e293b;
}

.overview-value.is-open {
    color: #059669;
}

.overview-value.is-close {
    color: #dc2626;
}

.site-tabs {
    display: flex;
    gap: 10px;
    margin: 0 0 16px 0;
    background: rgba(255, 255, 255, 0.98);
    border: 1px solid #e5edf6;
    border-radius: 16px;
    padding: 8px;
    box-shadow: 0 10px 24px rgba(15, 23, 42, 0.04);
}

.site-tab {
    display: flex;
    align-items: center;
    gap: 8px;
    border: 0;
    background: transparent;
    color: #64748b;
    padding: 11px 18px;
    border-radius: 11px;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    transition: all .2s;
}

.site-tab:hover {
    background: #f1f5f9;
    color: #1d4ed8;
}

.site-tab.active {
    background: linear-gradient(135deg, #3b82f6, #1d4ed8);
    color: #fff;
    box-shadow: 0 10px 18px rgba(59, 130, 246, .22);
}

.site-tab-panel {
    display: none;
}

.site-tab-panel.active {
    display: block;
}

#tab-basic .form-section:nth-of-type(4),
#tab-basic .form-section[data-seo-hidden="1"] {
    display: none;
}

.toggle-group {
    display: flex;
    gap: 8px;
    margin-top: 6px;
    flex-wrap: wrap;
}

.toggle-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 9px 16px;
    border: 1px solid #d1d5db;
    border-radius: 10px;
    cursor: pointer;
    font-size: 13px;
    color: #64748b;
    transition: all .2s;
    background: #fff;
}

.toggle-item:hover {
    border-color: #2563eb;
    color: #2563eb;
}

.toggle-item.active {
    border-color: #2563eb;
    background: #eff6ff;
    color: #1d4ed8;
}

.upload-box {
    position: relative;
    min-height: 120px;
    border: 2px dashed #dbe5f0;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    transition: border-color .2s, background .2s;
    background: #fbfdff;
}

.upload-box:hover {
    border-color: #3b82f6;
    background: #f8fbff;
}

.upload-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 20px;
    color: #94a3b8;
    cursor: pointer;
}

.upload-placeholder span {
    font-size: 12px;
}

.upload-preview {
    position: relative;
    width: 100%;
    min-height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;
}

.upload-preview img {
    max-width: 100%;
    max-height: 110px;
    object-fit: contain;
}

.upload-remove {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 24px;
    height: 24px;
    background: rgba(239, 68, 68, 0.92);
    color: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    cursor: pointer;
    line-height: 1;
}

.upload-remove:hover {
    background: #dc2626;
}

.form-help {
    font-size: 12px;
    color: #94a3b8;
    margin: 6px 0 0 0;
    line-height: 1.6;
}

.license-tip-box {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    background: #fff7ed;
    border: 1px solid #fed7aa;
    border-radius: 12px;
    padding: 12px 14px;
    color: #9a3412;
    font-size: 13px;
    line-height: 1.7;
}

.license-tip-box a {
    color: #2563eb;
    text-decoration: underline;
}

@media (max-width: 1200px) {
    .site-page-overview {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 768px) {
    .site-page-overview {
        grid-template-columns: 1fr;
    }

    .site-tabs {
        flex-direction: column;
    }
}
</style>

<script>
function triggerUpload(type) {
    var fileInputId = type === 'watermark' ? 'watermarkFile' : type + 'File';
    document.getElementById(fileInputId).click();
}

function handleFileUpload(fileInput, type) {
    var file = fileInput.files[0];
    if (!file) return;

    var formData = new FormData();
    formData.append('file', file);
    formData.append('upload_type', 'image');
    formData.append('apply_watermark', type === 'watermark' || type === 'logo' ? '0' : '1');
    formData.append('csrf_token', '<?php echo rawurlencode(getAdminCsrfToken()); ?>');

    var xhr = new XMLHttpRequest();
    xhr.open('POST', '?act=upload', true);
    xhr.onload = function() {
        if (xhr.status === 200) {
            var res = JSON.parse(xhr.responseText);
            if (res.code === 200) {
                var url = res.data.url;
                var inputId = type === 'watermark' ? 'watermark_image' : 'site_' + type;
                document.getElementById(inputId).value = url;
                document.getElementById(type + 'Img').src = url;
                document.getElementById(type + 'Placeholder').style.display = 'none';
                document.getElementById(type + 'Preview').style.display = 'flex';
            } else {
                alert(res.msg || '上传失败');
            }
        }
    };
    xhr.send(formData);
}

function removeUpload(type) {
    var inputId = type === 'watermark' ? 'watermark_image' : 'site_' + type;
    document.getElementById(inputId).value = '';
    document.getElementById(type + 'Preview').style.display = 'none';
    document.getElementById(type + 'Placeholder').style.display = 'flex';
}

function switchSiteTab(btn) {
    var target = btn.getAttribute('data-tab');
    var tabs = document.querySelectorAll('.site-tab');
    var panels = document.querySelectorAll('.site-tab-panel');

    for (var i = 0; i < tabs.length; i++) {
        tabs[i].classList.remove('active');
    }
    for (var j = 0; j < panels.length; j++) {
        panels[j].classList.remove('active');
    }

    btn.classList.add('active');
    var panel = document.getElementById('tab-' + target);
    if (panel) {
        panel.classList.add('active');
    }
    return false;
}

var siteTabs = document.querySelectorAll('.site-tab');
for (var i = 0; i < siteTabs.length; i++) {
    siteTabs[i].onclick = function() {
        return switchSiteTab(this);
    };
}

var statusGroup = document.getElementById('siteStatusGroup');
if (statusGroup) {
    var labels = statusGroup.querySelectorAll('label');
    labels.forEach(function(label) {
        label.addEventListener('click', function() {
            var val = label.getAttribute('data-value');
            labels.forEach(function(item) {
                item.classList.remove('active');
            });
            label.classList.add('active');
            document.getElementById('siteStatusInput').value = val;

            var closeReason = document.getElementById('closeReasonGroup');
            if (closeReason) {
                closeReason.style.display = val === 'close' ? '' : 'none';
            }
        });
    });
}

if (window.lucide && typeof window.lucide.createIcons === 'function') { window.lucide.createIcons(); }
</script>
