<?php
// PPQCMS SEO 设置（视觉风格完全沿用 Translation.php：translation-card / translation-switch / translation-row）
if (!isset($ADMINKEY)) { exit('404'); }
include('../Php/Admin/cookie.php');
require_once '../Php/Home/Seo.php';

$MODULE      = 'Basic';
$MODULE_FILE = 'Seo';

$AdminBasic = json_decode(file_get_contents('../PPQSQL/Admin/Basic/AdminBasic.php'), true);
if (!is_array($AdminBasic)) $AdminBasic = array();

$msg = '';
$msgType = 'ok';

// 保存 SEO 设置
if (isset($_POST['submit'])) {
    $keys = array('SeoHomeTitle','SeoHomeDesc','SeoOgImage','SeoBaiduToken');
    foreach ($keys as $k) {
        $AdminBasic[$k] = isset($_POST[$k]) ? trim(strip_tags($_POST[$k])) : '';
    }
    $AdminBasic['SeoCanonical'] = isset($_POST['SeoCanonical']) ? '1' : '0';
    $AdminBasic['SeoOg']        = isset($_POST['SeoOg']) ? '1' : '0';
    $AdminBasic['SeoJsonLd']    = isset($_POST['SeoJsonLd']) ? '1' : '0';
    file_put_contents('../PPQSQL/Admin/Basic/AdminBasic.php', json_encode($AdminBasic, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
    $msg = 'SEO 设置已保存。';
}

// 生成 sitemap.xml
if (isset($_POST['gen_sitemap'])) {
    $r = ppq_build_sitemap();
    $msg = 'sitemap.xml 已生成：共 ' . intval($r['total']) . ' 条（其中视频 ' . intval($r['videos']) . ' 条）。';
}

// 推送给百度
if (isset($_POST['push_baidu'])) {
    $token = isset($AdminBasic['SeoBaiduToken']) ? trim($AdminBasic['SeoBaiduToken']) : '';
    if (!$token) {
        $msg = '未填写百度推送 Token，无法推送。请先在上方填写并保存。';
        $msgType = 'err';
    } else {
        ppq_build_sitemap();
        $xml = @file_get_contents(ppq_root() . '/sitemap.xml');
        $urls = array();
        if (preg_match_all('#<loc>([^<]+)</loc>#', $xml, $m)) $urls = $m[1];
        $pushed = 0;
        foreach (array_chunk($urls, 2000) as $chunk) {
            $ret = ppq_baidu_push($chunk, $token);
            if (!empty($ret['ok'])) $pushed += count($chunk);
        }
        $msg = '已向百度推送 ' . $pushed . ' 条链接（按账号额度处理，未收录可稍后重试）。';
    }
}

$get = function($k, $d='') use ($AdminBasic) { return isset($AdminBasic[$k]) ? $AdminBasic[$k] : $d; };
// 自动检测当前域名
        $proto = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
        $host  = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
        $siteUrl = $proto . '://' . $host;
$homeTitle  = $get('SeoHomeTitle', $get('WebTitle', 'PPQCMS'));
$homeDesc   = $get('SeoHomeDesc', '');
$shareText  = $homeTitle;
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>SEO 设置 - 管理中心</title>
  <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <style>
    *{box-sizing:border-box}html,body{margin:0;min-height:100%;font-family:'Segoe UI','Microsoft YaHei',sans-serif}body{background:#eef2f1;color:#223047}
    :root{--translation-sidebar:260px;--translation-header:82px}
    .header{position:fixed;z-index:90;top:0;left:var(--translation-sidebar);right:0;height:var(--translation-header);padding:0 28px;display:flex;align-items:center;justify-content:space-between}
    .header-left,.header-right{display:flex;align-items:center;gap:18px}.header-right{margin-left:auto}.header-icon{font-size:16px;text-decoration:none}.header-icon i{font-style:normal}.mobile-toggle{display:none;border:0;background:transparent;color:inherit;font-size:22px;cursor:pointer}
    .main-content{min-height:100vh;margin-left:var(--translation-sidebar);padding:calc(var(--translation-header) + 30px) 30px 42px;transition:margin-left .25s ease}
    .sidebar.collapsed~.main-content{margin-left:70px}.sidebar-overlay{display:none}
    @media(max-width:768px){.header{left:0;height:68px;padding:0 16px}.header-right{gap:10px}.header .user-name{display:none}.mobile-toggle{display:block}.sidebar{z-index:110}.sidebar:not(.mobile-open){transform:translateX(-100%)}.sidebar.mobile-open{transform:translateX(0)}.sidebar.mobile-open~.sidebar-overlay{display:block;position:fixed;inset:0;z-index:105;background:rgba(17,28,42,.46)}.main-content,.sidebar.collapsed~.main-content{margin-left:0;padding:92px 15px 28px}}
  </style>
<style id="ppq-unified">
/* PPQCMS 统一后台样式 — 仅视觉统一，不改布局 */
html,body{font-family:'Segoe UI','Microsoft YaHei',sans-serif}
body{background:#eef2f1!important;color:#223047!important}
.sidebar,.admin-sidebar,.menu-box,.left-menu{background:linear-gradient(180deg,#223047 0%,#172133 100%)!important;color:#fff!important}
.sidebar .logo-text,.sidebar .logo i{color:#fff!important}
.sidebar .menu-item,.sidebar .submenu-item{color:#DCE8E4!important}
.sidebar .menu-item:hover,.sidebar .submenu-item:hover{background:rgba(255,255,255,.08)!important;color:#fff!important}
.sidebar .menu-item.active,.sidebar .submenu-item.active,.sidebar .menu-item.selected{background:#3e9c88!important;color:#fff!important}
.header,.top-bar,.admin-header{background:#223047!important;color:#fff!important;box-shadow:0 2px 12px rgba(23,33,51,.18)!important}
.header a,.header .header-icon,.header .user-name{color:#F7FAF9!important}
.header .user-avatar{background:#3D4C62!important;color:#fff!important}
.settings-card,.card,.panel,.box,.content-card,.form-card,.table-card,.section-card,.admin-card,.data-card,.list-card,.info-card,.stat-card,.usage-guide{background:#fff!important;border:1px solid #dbe7e4!important;border-radius:14px!important;box-shadow:0 10px 28px rgba(27,50,74,.07)!important;overflow:hidden}
.settings-card-header,.card-header,.panel-header,.section-header,.content-card-header{background:#edf6f3!important;border-bottom:1px solid #dcebe6!important;color:#22344f!important;font-weight:800!important;font-size:20px!important;padding:18px 22px!important}
h1{font-size:31px!important;color:#22344f!important;margin:22px 0 8px!important}
h2,h3,.settings-title{color:#22344f!important}
.page-subtitle,.text-muted,.breadcrumb{color:#75879e!important}
.breadcrumb a{color:#3e9c88!important}
input[type=text],input[type=password],input[type=number],input[type=url],input[type=email],select,textarea{background:#fff!important;border:1px solid #cbdad6!important;color:#223047!important;border-radius:8px!important;padding:10px 12px!important;font-size:15px!important}
input:focus,select:focus,textarea:focus{border-color:#3e9c88!important;box-shadow:0 0 0 3px rgba(62,156,136,.14)!important;outline:none!important}
.btn-primary,.btn-success,.am-btn-primary,.am-btn-success,button.btn-primary{background:#3e9c88!important;border-color:#3e9c88!important;color:#fff!important;border-radius:8px!important;font-weight:800!important;padding:12px 18px!important;font-size:15px!important;cursor:pointer!important}
.btn-primary:hover,.btn-success:hover{background:#34876f!important;border-color:#34876f!important}
.btn-warning,.am-btn-warning{background:#C9953D!important;border-color:#C9953D!important;color:#fff!important}
.translation-switch input:checked+b{background:#3e9c88!important}
table{background:#fff!important;border-radius:12px!important;overflow:hidden}
th{background:#edf6f3!important;color:#22344f!important}
.alert-success,.am-alert-success{background:#E8F3EF!important;border-color:#B9DCCC!important;color:#21866f!important}
.alert-warning,.am-alert-warning{background:#FFF8E8!important;border-color:#EEDAA7!important;color:#7e6421!important}
</style>

</head>
<body>
<?php include(dirname(__DIR__, 2) . '/list.php'); ?>
<?php include(dirname(__DIR__, 2) . '/header.php'); ?>
<div class="sidebar-overlay" id="sidebarOverlay"></div>
<main class="main-content"><div class="content-wrapper translation-page">
  <div class="breadcrumb"><a href="./?Php=Home/index">首页</a><span>/</span><a href="./?Php=Home/Basic/Basicsetup">基本设置</a><span>/</span><span>SEO 设置</span></div>
  <h1><i class="fa fa-search"></i> SEO 设置</h1><p class="translation-subtitle">这些设置只影响搜索引擎和分享卡片，页面外观不变。</p>
  <?php if ($msg): ?><div class="translation-notice<?php echo $msgType === 'err' ? ' err' : ''; ?>"><i class="fa fa-<?php echo $msgType === 'err' ? 'exclamation-circle' : 'check-circle'; ?>"></i> <?php echo htmlspecialchars($msg, ENT_QUOTES, 'UTF-8'); ?></div><?php endif; ?>

  <!-- ===== 基础设置 ===== -->
  <form method="post" class="translation-card"><div class="translation-card-title"><i class="fa fa-sliders"></i> SEO 基础设置</div><div class="translation-card-body">

    <div class="translation-field">
      <div class="translation-field-label"><strong>首页标题</strong><small>显示在浏览器标签和搜索结果里的首页名字。视频/分类页已自动用「网站名 - 分类 - 名称」。</small></div>
      <input type="text" name="SeoHomeTitle" value="<?php echo htmlspecialchars($get('SeoHomeTitle'), ENT_QUOTES, 'UTF-8'); ?>" placeholder="留空则使用网站名称（<?php echo htmlspecialchars($get('WebTitle','PPQCMS'), ENT_QUOTES, 'UTF-8'); ?>）">
    </div>

    <div class="translation-field">
      <div class="translation-field-label"><strong>首页描述</strong><small>给搜索引擎看的一句网站介绍（建议 30-80 字）。</small></div>
      <textarea name="SeoHomeDesc" rows="3" placeholder="留空则使用「网站描述」"><?php echo htmlspecialchars($get('SeoHomeDesc'), ENT_QUOTES, 'UTF-8'); ?></textarea>
    </div>

    <div class="translation-divider"></div>

    <label class="translation-switch"><span><strong>开启 canonical（网页身份证）</strong><small>告诉搜索引擎哪个网址是标准版，避免重复内容被判作弊。建议开启。</small></span><input type="checkbox" name="SeoCanonical" value="1" <?php echo $get('SeoCanonical','1') === '1' ? 'checked' : ''; ?>><b></b></label>

    <label class="translation-switch"><span><strong>开启 OG 分享卡片</strong><small>开启后，在 X / Telegram / 微信 / QQ 分享链接时会显示标题+图+描述，而不是纯链接。</small></span><input type="checkbox" name="SeoOg" value="1" <?php echo $get('SeoOg','1') === '1' ? 'checked' : ''; ?>><b></b></label>

    <div class="translation-field">
      <div class="translation-field-label"><strong>OG 默认分享图（图片 URL）</strong><small>分享时默认带的图。视频详情页会自动改用该视频的封面图。</small></div>
      <input type="text" name="SeoOgImage" value="<?php echo htmlspecialchars($get('SeoOgImage'), ENT_QUOTES, 'UTF-8'); ?>" placeholder="留空则使用网站 Logo">
    </div>

    <label class="translation-switch"><span><strong>开启 JSON-LD 结构化数据</strong><small>给 Google 一段「这是视频」的标注，搜索结果里能直接带出视频信息。视频详情页用 VideoObject，其它页用 WebSite。</small></span><input type="checkbox" name="SeoJsonLd" value="1" <?php echo $get('SeoJsonLd','1') === '1' ? 'checked' : ''; ?>><b></b></label>

    <div class="translation-divider"></div>

    <div class="translation-field">
      <div class="translation-field-label"><strong>百度主动推送 Token（选填）</strong><small>填了之后，点下方「推送给百度」可加快收录。不填也能用，只是百度收录慢一点。</small></div>
      <input type="text" name="SeoBaiduToken" value="<?php echo htmlspecialchars($get('SeoBaiduToken'), ENT_QUOTES, 'UTF-8'); ?>" placeholder="去百度搜索资源平台获取，留空不影响其它功能">
    </div>

    <div class="translation-actions"><button type="submit" name="submit" value="1"><i class="fa fa-save"></i> 保存设置</button></div>
  </div></form>

  <!-- ===== 网站地图 & 分享链接 ===== -->
  <div class="translation-card"><div class="translation-card-title"><i class="fa fa-share-alt"></i> 网站地图 & 分享链接</div><div class="translation-card-body">

    <p class="translation-subtitle" style="margin:14px 0 0">sitemap.xml 让 Google / 必应 / 百度 一次性知道你所有页面。下方是 X / Telegram / 微信 / QQ 的分享链接模板，开启 OG 分享卡片后会自动带上标题和封面。</p>

    <form method="post">
      <div class="translation-actions" style="padding-top:14px;padding-bottom:0">
        <button type="submit" name="gen_sitemap" value="1"><i class="fa fa-sitemap"></i> 生成 / 刷新 sitemap.xml</button>
        <button type="submit" name="push_baidu" value="1" class="btn-outline"><i class="fa fa-paper-plane"></i> 推送给百度</button>
      </div>
    </form>

    <div class="translation-divider"></div>

    <div class="translation-field">
      <div class="translation-field-label"><strong>sitemap.xml 地址</strong><small>复制到各站长平台提交。</small></div>
      <code class="translation-code"><?php echo $siteUrl; ?>/sitemap.xml</code>
    </div>

    <div class="translation-divider"></div>

    <div class="translation-share-row">
      <div class="translation-share-label"><i class="fa fa-twitter" style="color:#1da1f2"></i> X (Twitter)</div>
      <code class="translation-share-url">https://twitter.com/intent/tweet?text=<?php echo urlencode($shareText); ?>&amp;url=<?php echo urlencode($siteUrl); ?></code>
    </div>

    <div class="translation-share-row">
      <div class="translation-share-label"><i class="fa fa-telegram" style="color:#0088cc"></i> Telegram</div>
      <code class="translation-share-url">https://t.me/share/url?url=<?php echo urlencode($siteUrl); ?>&amp;text=<?php echo urlencode($shareText); ?></code>
    </div>

    <div class="translation-share-row">
      <div class="translation-share-label"><i class="fa fa-weixin" style="color:#07c160"></i> 微信</div>
      <code class="translation-share-url">微信不支持网页直链分享。复制链接 <?php echo $siteUrl; ?> 后，在微信「文件传输助手」或聊天窗口粘贴发送即可。</code>
    </div>

    <div class="translation-share-row">
      <div class="translation-share-label"><i class="fa fa-qq" style="color:#12b7f5"></i> QQ</div>
      <code class="translation-share-url">https://connect.qq.com/widget/shareqq/index.html?url=<?php echo urlencode($siteUrl); ?>&amp;title=<?php echo urlencode($shareText); ?>&amp;desc=<?php echo urlencode($homeDesc); ?></code>
    </div>

  </div></div>

</div></main>
<style>
.translation-page{max-width:1500px}.translation-page h1{color:#22344f;margin:22px 0 8px;font-size:31px}.translation-subtitle{color:#75879e;font-size:16px;margin:0 0 22px}.translation-notice{padding:14px 18px;margin:0 0 18px;border-radius:10px;background:#ebf8f2;color:#21866f;border:1px solid #bce4d4;font-weight:600}.translation-notice.err{background:#fff0f3;color:#d6336c;border-color:#ffd0dc}.translation-card{background:#fff;border:1px solid #dbe7e4;border-radius:14px;overflow:hidden;box-shadow:0 10px 28px rgba(27,50,74,.07);margin-bottom:18px}.translation-card-title{padding:18px 22px;background:#edf6f3;border-bottom:1px solid #dcebe6;color:#22344f;font-weight:800;font-size:20px}.translation-card-body{padding:8px 22px 24px}.translation-switch,.translation-row{display:flex;align-items:center;justify-content:space-between;gap:24px;padding:19px 0;border-bottom:1px solid #e7eeec}.translation-switch span,.translation-row div{display:grid;gap:6px;color:#22344f}.translation-switch small,.translation-row small{color:#8091a6;font-weight:400;font-size:13px;line-height:1.5}.translation-switch input{display:none}.translation-switch b{width:52px;height:28px;border-radius:18px;background:#c6d1d5;position:relative;flex:none;cursor:pointer}.translation-switch b:after{content:"";position:absolute;width:22px;height:22px;top:3px;left:3px;border-radius:50%;background:#fff;transition:.2s}.translation-switch input:checked+b{background:#3e9c88}.translation-switch input:checked+b:after{left:27px}.translation-field{padding:19px 0;border-bottom:1px solid #e7eeec}.translation-field-label{display:grid;gap:6px;color:#22344f;margin-bottom:10px}.translation-field-label strong{font-size:16px;font-weight:700}.translation-field-label small{color:#8091a6;font-weight:400;font-size:13px;line-height:1.5}.translation-field input,.translation-field textarea{width:100%;padding:10px 12px;border:1px solid #cbdad6;border-radius:8px;background:#fff;color:#22344f;font-size:15px;font-family:inherit;box-sizing:border-box}.translation-field input:focus,.translation-field textarea:focus{border-color:#3e9c88;outline:none;box-shadow:0 0 0 3px rgba(62,156,136,.14)}.translation-field textarea{resize:vertical;min-height:80px}.translation-divider{height:1px;background:#e7eeec;margin:0}.translation-code{display:block;background:#f4f6fa;padding:10px 13px;border-radius:8px;font-size:13px;color:#43536b;border:1px solid #e7eeec;word-break:break-all;font-weight:500;font-family:monospace}.translation-actions{padding-top:22px;text-align:right;display:flex;gap:12px;justify-content:flex-end;flex-wrap:wrap}.translation-actions button{border:0;border-radius:8px;background:#3e9c88;color:#fff;padding:12px 18px;font-size:16px;font-weight:800;cursor:pointer;display:inline-flex;align-items:center;gap:6px}.translation-actions button.btn-outline{background:#fff;color:#3e9c88;border:1px solid #3e9c88}.translation-share-row{display:flex;align-items:center;gap:16px;padding:14px 0;border-bottom:1px solid #e7eeec}.translation-share-row:last-child{border-bottom:0}.translation-share-label{display:flex;align-items:center;gap:10px;color:#22344f;font-weight:700;font-size:15px;flex-shrink:0;width:150px}.translation-share-label i{font-size:18px;width:24px;text-align:center}.translation-share-url{font-size:12px;color:#43536b;background:#f4f6fa;padding:8px 12px;border-radius:6px;border:1px solid #e7eeec;word-break:break-all;flex:1;font-family:monospace;line-height:1.6}@media(max-width:760px){.translation-page h1{font-size:25px}.translation-switch,.translation-row{flex-direction:column;align-items:flex-start;gap:12px}.translation-share-row{flex-direction:column;align-items:flex-start;gap:8px}.translation-share-label{width:auto}.translation-actions{text-align:stretch}.translation-actions button{width:100%;justify-content:center}}
</style>
<?php include(dirname(__DIR__, 2) . '/footer.php'); ?>
</body>
</html>
