<?php
if (!isset($ADMINKEY)) { exit('404'); }
include('../Php/Admin/cookie.php');

$configFile = dirname(__DIR__, 4) . '/PPQSQL/Admin/Basic/AdminBasic.php';
$settings = is_file($configFile) ? json_decode(file_get_contents($configFile), true) : array();
if (!is_array($settings)) $settings = array();
$languageOptions = array('zh-CN' => '简体中文', 'zh-TW' => '繁體中文', 'en' => 'English', 'ja' => '日本語', 'ko' => '한국어', 'vi' => 'Tiếng Việt', 'id' => 'Bahasa Indonesia', 'ms' => 'Bahasa Melayu', 'th' => 'ไทย', 'fil' => 'Filipino', 'hi' => 'हिन्दी', 'fr' => 'Français');
$notice = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_translation'])) {
    $selected = isset($_POST['FrontendTranslateLanguages']) && is_array($_POST['FrontendTranslateLanguages']) ? array_values(array_intersect(array_keys($languageOptions), $_POST['FrontendTranslateLanguages'])) : array();
    $selected = array_values(array_intersect($selected, array('zh-CN', 'zh-TW', 'en')));  // 免费版锁定
    if (!in_array('zh-CN', $selected, true)) array_unshift($selected, 'zh-CN');
    $settings['FrontendTranslateOpen'] = isset($_POST['FrontendTranslateOpen']) ? '1' : '0';
    $settings['FrontendTranslateAuto'] = isset($_POST['FrontendTranslateAuto']) ? '1' : '0';
    $settings['FrontendTranslatePosition'] = isset($_POST['FrontendTranslatePosition']) && $_POST['FrontendTranslatePosition'] === 'bottom-left' ? 'bottom-left' : 'bottom-right';
    $settings['FrontendTranslateLanguages'] = $selected;
    file_put_contents($configFile, json_encode($settings, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
    $notice = '多语言翻译设置已保存，仅影响前台页面。';
}
$enabled = isset($settings['FrontendTranslateOpen']) ? (string)$settings['FrontendTranslateOpen'] === '1' : true;
$auto = !isset($settings['FrontendTranslateAuto']) || (string)$settings['FrontendTranslateAuto'] === '1';
$position = isset($settings['FrontendTranslatePosition']) ? $settings['FrontendTranslatePosition'] : 'bottom-right';
$selectedLanguages = isset($settings['FrontendTranslateLanguages']) && is_array($settings['FrontendTranslateLanguages']) ? $settings['FrontendTranslateLanguages'] : array_keys($languageOptions);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>多语言翻译 - 管理中心</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>多语言翻译</span></div>
  <h1><i class="fa fa-language"></i> 多语言翻译</h1><p class="translation-subtitle">仅翻译前台访客页面；后台管理始终保持中文。翻译服务由前台本地脚本按访客语言执行。</p>
  <?php if ($notice): ?><div class="translation-notice"><i class="fa fa-check-circle"></i> <?php echo htmlspecialchars($notice, ENT_QUOTES, 'UTF-8'); ?></div><?php endif; ?>
  <form method="post" class="translation-card"><div class="translation-card-title"><i class="fa fa-globe"></i> 前台语言策略</div><div class="translation-card-body">
    <label class="translation-switch"><span><strong>启用前台多语言</strong><small>在前台右下角或左下角显示语言切换入口。</small></span><input type="checkbox" name="FrontendTranslateOpen" value="1" <?php echo $enabled ? 'checked' : ''; ?>><b></b></label>
    <label class="translation-switch"><span><strong>按访客浏览器语言自动选择</strong><small>首次访问时自动匹配；访客仍可手动切换语言。</small></span><input type="checkbox" name="FrontendTranslateAuto" value="1" <?php echo $auto ? 'checked' : ''; ?>><b></b></label>
    <div class="translation-row"><div><strong>语言入口位置</strong><small>移动端同样使用该位置，并自动缩小为图标。</small></div><select name="FrontendTranslatePosition"><option value="bottom-right" <?php echo $position === 'bottom-right' ? 'selected' : ''; ?>>右下角</option><option value="bottom-left" <?php echo $position === 'bottom-left' ? 'selected' : ''; ?>>左下角</option></select></div>
    <div class="translation-languages"><strong>允许访客选择的语言</strong><small>免费版仅开放 简体中文 / 繁體中文 / English，其余 9 种语言为付费版功能。<a href="https://ppqzy.com" target="_blank" style="color:#e4393c;">升级解锁</a></small><div class="translation-language-grid"><?php foreach ($languageOptions as $code => $label): ?><label><input type="checkbox" name="FrontendTranslateLanguages[]" value="<?php echo $code; ?>" <?php echo in_array($code, $selectedLanguages, true) ? 'checked' : ''; ?> <?php echo ($code === 'zh-CN' || !in_array($code, array('zh-CN', 'zh-TW', 'en'), true)) ? 'disabled' : ''; ?>><?php if ($code === 'zh-CN'): ?><input type="hidden" name="FrontendTranslateLanguages[]" value="zh-CN"><?php endif; ?><span><?php echo $label; ?></span></label><?php endforeach; ?></div></div>
    <div class="translation-help"><i class="fa fa-shield"></i><div><strong>版式保护已启用</strong><p>导航、分类、按钮和视频标题最多显示两行；简介类文本最多三行，避免外语长度破坏现有模板布局。</p></div></div>
    <div class="translation-actions"><button type="submit" name="save_translation" value="1"><i class="fa fa-save"></i> 保存多语言设置</button></div>
  </div></form>
</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-card{background:#fff;border:1px solid #dbe7e4;border-radius:14px;overflow:hidden;box-shadow:0 10px 28px rgba(27,50,74,.07)}.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,.translation-languages small{color:#8091a6;font-weight:400}.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-row select{min-width:160px;padding:10px 12px;border:1px solid #cbdad6;border-radius:8px;background:#fff;color:#22344f;font-size:15px}.translation-languages{padding:20px 0;display:grid;gap:8px}.translation-language-grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px;margin-top:8px}.translation-language-grid label{display:flex;align-items:center;gap:9px;padding:13px;border:1px solid #d6e4e0;border-radius:9px;color:#2e4058;font-weight:700;cursor:pointer}.translation-language-grid input{accent-color:#3e9c88;width:18px;height:18px}.translation-help{display:flex;gap:12px;padding:16px;margin-top:4px;border-radius:10px;background:#fff8e8;color:#7e6421}.translation-help i{font-size:21px;margin-top:2px}.translation-help p{margin:4px 0 0;color:#907b45}.translation-actions{padding-top:22px;text-align:right}.translation-actions button{border:0;border-radius:8px;background:#3e9c88;color:#fff;padding:12px 18px;font-size:16px;font-weight:800;cursor:pointer}@media(max-width:760px){.translation-page h1{font-size:25px}.translation-switch,.translation-row{align-items:flex-start}.translation-language-grid{grid-template-columns:repeat(2,minmax(0,1fr))}.translation-actions{text-align:stretch}.translation-actions button{width:100%}}
</style>
<?php include(dirname(__DIR__, 2) . '/footer.php'); ?>
</body>
</html>
