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

require_once ROOT_PATH . 'core/db/db.php';
require_once ROOT_PATH . 'core/lib/license.php';
require_once ROOT_PATH . 'core/lib/license_guard.php';
require_once ROOT_PATH . 'core/lib/shop_config.php';

$licenseObj = new \Core\Lib\License();
$readonlyMode = !\Core\Lib\LicenseGuard::canUseOrder($licenseObj);

$db = isset($db) && $db instanceof \Core\Db\DB ? $db : \Core\Db\DB::getInstance();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    requireAdminCsrfToken();
    if ($readonlyMode) {
        echo '<div class="alert alert-warning" style="margin-bottom:16px;">当前为只读模式，升级专业版后可保存配置</div>'; 
    } else {
    \Core\Lib\ShopConfig::save($db, [
        'review_enabled' => isset($_POST['review_enabled']) ? '1' : '0',
        'review_audit' => isset($_POST['review_audit']) ? '1' : '0',
    ]);
    echo '<div class="alert alert-success" style="margin-bottom:16px;">商城配置已保存</div>';
    }
}
$config = \Core\Lib\ShopConfig::get($db);
?>
<div class="page-container fade-in">
    <?php if ($readonlyMode): ?>
    <div style="margin-bottom:20px;padding:18px 20px;border:1px solid #fdba74;background:linear-gradient(135deg,#fff7ed 0%,#ffffff 100%);border-radius:14px;display:flex;align-items:center;justify-content:space-between;gap:16px;flex-wrap:wrap;"><div><div style="font-size:16px;font-weight:700;color:#9a3412;">商城配置当前为只读模式</div><div style="margin-top:6px;font-size:13px;color:#7c2d12;line-height:1.8;">免费版可查看当前商城开关状态，保存配置需升级专业版授权后使用。</div></div><a href="?act=auth" style="display:inline-flex;align-items:center;justify-content:center;padding:11px 18px;border-radius:10px;background:linear-gradient(135deg,#ea580c 0%,#dc2626 100%);color:#fff;text-decoration:none;font-weight:700;white-space:nowrap;">升级授权</a></div>
    <?php endif; ?>
    <div class="page-header">
        <div>
            <h1 style="margin:0;font-size:20px;color:#0f172a;">商城配置</h1>
            <p style="margin:6px 0 0;color:#64748b;font-size:13px;">配置商城评价、审核等开关。</p>
        </div>
    </div>
    <form method="post" class="content-card" style="padding:24px;max-width:760px;">
        <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars(getAdminCsrfToken()); ?>">
        <div class="yt-shop-config-row">
            <div>
                <strong>开启商品评价</strong>
                <p>关闭后前台不展示评价提交入口，也不允许会员提交新评价。</p>
            </div>
            <label class="yt-switch"><input type="checkbox" name="review_enabled" value="1" <?php echo $config['review_enabled'] === '1' ? 'checked' : ''; ?>><span></span></label>
        </div>
        <div class="yt-shop-config-row">
            <div>
                <strong>评价需要审核</strong>
                <p>开启后会员提交评价先进入待审核，后台通过后才显示。</p>
            </div>
            <label class="yt-switch"><input type="checkbox" name="review_audit" value="1" <?php echo $config['review_audit'] === '1' ? 'checked' : ''; ?>><span></span></label>
        </div>
        <div style="margin-top:24px;">
            <?php if ($readonlyMode): ?>
            <a href="?act=auth" class="btn btn-secondary">升级后保存</a>
            <?php else: ?>
            <button type="submit" class="btn btn-primary">保存配置</button>
            <?php endif; ?>
        </div>
    </form>
</div>
<style>
.yt-shop-config-row{display:flex;align-items:center;justify-content:space-between;gap:18px;padding:18px 0;border-bottom:1px solid #eef2f7;}
.yt-shop-config-row:first-child{padding-top:0;}
.yt-shop-config-row strong{display:block;font-size:15px;color:#0f172a;margin-bottom:6px;}
.yt-shop-config-row p{margin:0;color:#64748b;font-size:13px;line-height:1.7;}
.yt-switch{position:relative;display:inline-flex;width:54px;height:30px;flex:0 0 auto;}
.yt-switch input{display:none;}
.yt-switch span{position:absolute;inset:0;background:#cbd5e1;border-radius:999px;cursor:pointer;transition:.2s;}
.yt-switch span:before{content:"";position:absolute;width:24px;height:24px;left:3px;top:3px;background:#fff;border-radius:50%;box-shadow:0 2px 6px rgba(15,23,42,.18);transition:.2s;}
.yt-switch input:checked + span{background:#2563eb;}
.yt-switch input:checked + span:before{transform:translateX(24px);}
</style>
