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

$licenseInfo = $license->getStatus();
$autoResetNotice = isset($_SESSION['license_auto_reset_notice']) ? $_SESSION['license_auto_reset_notice'] : null;
$remoteResetNotice = isset($_SESSION['license_remote_reset_notice']) ? $_SESSION['license_remote_reset_notice'] : null;
$templateResetNotice = isset($_SESSION['template_auto_reset_notice']) ? $_SESSION['template_auto_reset_notice'] : null;
if ($autoResetNotice) {
    unset($_SESSION['license_auto_reset_notice']);
}
if ($remoteResetNotice) {
    unset($_SESSION['license_remote_reset_notice']);
}
if ($templateResetNotice) {
    unset($_SESSION['template_auto_reset_notice']);
}

$levelNames = array(1 => '免费版', 2 => '基础版', 3 => '专业版');
$currentLevel = isset($licenseInfo['level']) ? (int)$licenseInfo['level'] : 1;
$currentLevelName = isset($levelNames[$currentLevel]) ? $levelNames[$currentLevel] : '未知';

$templateResetReasonMap = array(
    'license_level_denied' => '当前授权等级已不足以继续使用原模板',
    'manifest_invalid' => '模板清单与授权平台记录不一致',
    'sync_failed' => '模板重新校验后仍无法通过完整性检查',
    'integrity_invalid' => '模板目录文件已被改动或缺失',
    'template_missing' => '当前模板目录已丢失',
    'runtime_guard_denied' => '模板运行时站点绑定校验未通过',
    '' => '当前模板已不可用',
);

$buyUrl = 'http://www.yutoucms.cn/license-center/user.php?act=buy';
$licenseKey = isset($licenseInfo['license_key']) ? $licenseInfo['license_key'] : '';
$licenseDomain = isset($licenseInfo['domain']) ? $licenseInfo['domain'] : '';
$currentHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
$licenseDomainMismatch = false;
if (!empty($licenseDomain) && !empty($currentHost)) {
    if ($currentLevel <= 1) {
        $licenseDomainMismatch = ($licenseDomain !== $currentHost);
    } else {
        $domains = array_map('trim', explode(',', $licenseDomain));
        $licenseDomainMismatch = !in_array($currentHost, $domains);
    }
}
$licenseStatusText = $licenseDomainMismatch ? '域名待校验' : ($licenseKey ? '已绑定授权' : '免费版授权');

$compareRows = array(
    array('基础栏目 / 文章 / 单页', '支持', '支持', '支持', false),
    array('产品内容管理', '最多 5 条', '不限', '不限', true),
    array('产品详情展示', '支持', '支持', '支持', false),
    array('自定义变量 / 站点设置', '支持', '支持', '支持', false),
    array('模板下载 / 在线升级', '支持', '支持', '支持', false),
    array('页面装修 / 可视化搭建', '支持', '支持', '支持', true),
    array('商城模块浏览', '只读', '只读', '完整', true),
    array('订单管理 / 支付设置', '只读', '只读', '完整', true),
    array('购物车 / 收货地址', '只读', '只读', '完整', true),
    array('发货管理 / 物流单号', '不支持', '不支持', '支持', true),
    array('售后退款 / 审核记录', '不支持', '不支持', '支持', true),
    array('商品评价 / 审核回复', '不支持', '不支持', '支持', true),
    array('充值中心 / 财务明细', '支持', '支持', '支持', false),
    array('多域名授权', '不支持', '单域名', '多域名', true),
    array('去版权授权', '不支持', '支持', '支持', true),
);

$compareBadgeClass = function ($text) {
    if (strpos($text, '不支持') !== false) {
        return 'compare-no';
    }
    if (strpos($text, '只读') !== false || strpos($text, '最多 5 条') !== false || strpos($text, '单域名') !== false) {
        return 'compare-limit';
    }
    return 'compare-yes';
};

$upgradePlans = array();
if ($currentLevel < 2) {
    $upgradePlans[] = array(
        'name' => '基础版',
        'price' => '￥188',
        'meta' => '年 / 单域名',
        'desc' => '适合内容站点与模板管理需求，支持去版权、在线升级与更多模板能力。',
        'btn' => '立即升级',
        'type' => 'basic',
    );
}
if ($currentLevel < 3) {
    $upgradePlans[] = array(
        'name' => '专业版',
        'price' => '￥388',
        'meta' => '永久 / 多域名可扩展',
        'desc' => '完整开放商城订单、支付、发货、退款、评价等后台操作能力，适合正式商业站点。',
        'btn' => '立即升级',
        'type' => 'pro',
        'recommended' => true,
    );
}
if ($currentLevel >= 3) {
    $upgradePlans[] = array(
        'name' => '当前已是专业版',
        'price' => '全部解锁',
        'meta' => '商城 / 授权 / 模板均可用',
        'desc' => '当前授权已覆盖商城后台、模板升级与扩展能力，如需更换域名或重新绑定可直接在本页操作。',
        'btn' => '查看授权',
        'type' => 'current',
    );
}
?>
<div class="page-container fade-in auth-page">
    <?php if ($autoResetNotice): ?>
    <div class="auth-notice auth-notice-success">
        <strong>授权已自动恢复免费版：</strong>
        检测到原授权域名 <?php echo htmlspecialchars($autoResetNotice['old_domain']); ?> 与当前访问域名 <?php echo htmlspecialchars($autoResetNotice['new_domain']); ?> 不匹配，系统已自动切换为当前域名免费授权。
    </div>
    <?php endif; ?>
    <?php if ($remoteResetNotice): ?>
    <div class="auth-notice auth-notice-info">
        <strong>授权已自动恢复免费版：</strong>
        检测到授权中心中的 KEY <?php echo htmlspecialchars($remoteResetNotice['old_key']); ?> 已失效或被删除，系统已切换为当前域名 <?php echo htmlspecialchars($remoteResetNotice['new_domain']); ?> 的免费授权。
    </div>
    <?php endif; ?>
    <?php if ($templateResetNotice): ?>
    <div class="auth-notice auth-notice-warn">
        <strong>模板已自动回退为免费模板：</strong>
        <?php echo htmlspecialchars(isset($templateResetReasonMap[$templateResetNotice['reason']]) ? $templateResetReasonMap[$templateResetNotice['reason']] : '模板状态异常'); ?>，系统已自动切换回默认模板以保证站点继续访问。
    </div>
    <?php endif; ?>
    <?php if ($licenseDomainMismatch): ?>
    <div class="auth-notice auth-notice-danger">
        <strong>授权域名不匹配：</strong>
        当前访问域名是 <?php echo htmlspecialchars($currentHost); ?>，本地授权绑定域名是 <?php echo htmlspecialchars($licenseDomain); ?>。请重新绑定正确的授权 KEY，或恢复为免费版授权。
    </div>
    <?php endif; ?>

    <div class="page-header auth-page-header">
        <div>
            <h2>
                <svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
                </svg>
                授权信息
            </h2>
            <p class="auth-header-text">后台授权管理、版本权限对比、升级入口统一放在这里，前台访问不受影响。</p>
        </div>
    </div>

    <div class="content-card auth-hero-card">
        <div class="auth-hero-top">
            <div>
                <div class="auth-hero-badge">授权控制台</div>
                <div class="auth-hero-title"><?php echo $currentLevelName; ?></div>
                <div class="auth-hero-desc">当前后台授权状态为 <?php echo $licenseStatusText; ?>，可在本页完成绑定、替换、恢复免费版与升级操作。</div>
            </div>
            <div class="auth-hero-chip"><?php echo $licenseStatusText; ?></div>
        </div>
        <div class="auth-stat-grid">
            <div class="auth-stat-item">
                <span>当前版本</span>
                <strong><?php echo $currentLevelName; ?></strong>
            </div>
            <div class="auth-stat-item">
                <span>当前域名</span>
                <strong><?php echo htmlspecialchars($currentHost ?: '未检测到'); ?></strong>
            </div>
            <div class="auth-stat-item">
                <span>授权状态</span>
                <strong><?php echo $licenseStatusText; ?></strong>
            </div>
        </div>
        <div class="auth-hero-bottom">
            <div class="auth-hero-field">
                <label>授权 KEY</label>
                <div class="auth-hero-value-row">
                    <span id="displayKey" class="auth-hero-value auth-hero-key">
                        <?php if ($licenseKey): ?>
                        <?php echo htmlspecialchars($licenseKey); ?>
                        <?php else: ?>
                        尚未激活授权 KEY
                        <?php endif; ?>
                    </span>
                    <?php if ($licenseKey): ?>
                    <button onclick="copyKey()" class="auth-copy-btn" type="button">复制</button>
                    <?php endif; ?>
                </div>
            </div>
            <div class="auth-hero-field">
                <label>授权域名</label>
                <div class="auth-hero-value-row auth-hero-value-row-single">
                    <span class="auth-hero-value"><?php echo htmlspecialchars($licenseDomain ?: '未绑定域名'); ?></span>
                </div>
            </div>
        </div>
    </div>

    <div class="auth-main-grid">
        <div class="content-card auth-compare-card">
            <div class="auth-section-head">
                <div>
                    <div class="auth-section-title">版本功能对比</div>
                    <div class="auth-section-subtitle">内容、模板、商城功能一次看清，避免后台功能开放层级混乱。</div>
                </div>
                <div class="compare-focus-tag">新增商城能力已纳入对比</div>
            </div>
            <div class="compare-intro">免费版与基础版可查看商城相关能力，实际操作以专业版授权为准；发货、退款、评价等新增功能已单独列出。</div>
            <div class="compare-table-wrap">
                <table class="license-compare-table">
                    <thead>
                        <tr>
                            <th>功能项</th>
                            <th>免费版</th>
                            <th>基础版</th>
                            <th>专业版</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($compareRows as $row): ?>
                        <tr class="<?php echo !empty($row[4]) ? 'compare-row-highlight' : ''; ?>">
                            <td class="feature-cell">
                                <span class="compare-feature-name"><?php echo htmlspecialchars($row[0]); ?></span>
                                <?php if (!empty($row[4])): ?>
                                <span class="compare-upgrade-badge">重点能力</span>
                                <?php endif; ?>
                            </td>
                            <td><span class="compare-value <?php echo $compareBadgeClass($row[1]); ?>"><?php echo htmlspecialchars($row[1]); ?></span></td>
                            <td><span class="compare-value <?php echo $compareBadgeClass($row[2]); ?>"><?php echo htmlspecialchars($row[2]); ?></span></td>
                            <td><span class="compare-value <?php echo $compareBadgeClass($row[3]); ?>"><?php echo htmlspecialchars($row[3]); ?></span></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="auth-side-stack">
            <div class="content-card auth-activate-card">
                <div class="auth-panel-title">
                    <svg style="width:20px;height:20px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M21 2l-2 2m-7.61 7.61a5.5 5.5 0 1 1-7.778 7.778 5.5 5.5 0 0 1 7.777-7.777zm0 0L15.5 7.5m0 0l3 3L22 7l-3-3m-3.5 3.5L19 4"/>
                    </svg>
                    <span>绑定 / 替换授权 KEY</span>
                </div>
                <div class="form-group">
                    <label class="auth-label">授权 KEY</label>
                    <input type="text" id="inputKey" placeholder="XXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" class="auth-input auth-input-key">
                </div>
                <div class="form-group">
                    <label class="auth-label">授权域名</label>
                    <input type="text" id="inputDomain" value="<?php echo htmlspecialchars($licenseDomain ?: $currentHost); ?>" placeholder="example.com" class="auth-input">
                </div>
                <button onclick="doActivate()" class="btn btn-primary auth-btn-primary" type="button">保存并替换授权</button>
                <button onclick="doResetLicense()" type="button" class="btn auth-btn-reset">恢复免费版 / 清除当前授权</button>
                <div class="auth-form-note">填写新的授权 KEY 并保存后，会直接覆盖当前本地授权信息；恢复免费版后会自动按当前域名重新生成免费授权。</div>
                <div id="activateMsg" class="auth-message"></div>
            </div>

            <div class="content-card auth-plan-card">
                <div class="auth-panel-title">
                    <svg style="width:20px;height:20px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M2 4l3 12h14l3-12-6 7-4-7-4 7-6-7z"/>
                        <path d="M5 16v4h14v-4"/>
                    </svg>
                    <span>升级解锁更多后台能力</span>
                </div>
                <div class="auth-plan-note">建议按模板管理、商城操作、授权范围三个方向选择升级。</div>
                <div class="auth-plan-grid<?php echo count($upgradePlans) === 1 ? ' auth-plan-grid-single' : ''; ?>">
                    <?php foreach ($upgradePlans as $plan): ?>
                    <div class="auth-plan-item auth-plan-item-<?php echo htmlspecialchars($plan['type']); ?>">
                        <?php if (!empty($plan['recommended'])): ?>
                        <div class="auth-plan-recommend">推荐</div>
                        <?php endif; ?>
                        <div class="auth-plan-name"><?php echo htmlspecialchars($plan['name']); ?></div>
                        <div class="auth-plan-price"><?php echo htmlspecialchars($plan['price']); ?> <span><?php echo htmlspecialchars($plan['meta']); ?></span></div>
                        <div class="auth-plan-desc"><?php echo htmlspecialchars($plan['desc']); ?></div>
                        <button onclick="openBuyWindow()" type="button" class="btn auth-plan-btn <?php echo $plan['type'] === 'pro' ? 'auth-plan-btn-pro' : ''; ?>"><?php echo htmlspecialchars($plan['btn']); ?></button>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>
</div>
<style>
.auth-page-header {
    margin-bottom: 16px;
}
.auth-header-text {
    margin: 6px 0 0;
    font-size: 13px;
    color: var(--text-light);
}
.auth-notice {
    margin-bottom: 14px;
    padding: 14px 16px;
    border-radius: 14px;
    border: 1px solid;
    font-size: 13px;
    line-height: 1.8;
}
.auth-notice-success {
    background: #ecfdf5;
    border-color: #86efac;
    color: #166534;
}
.auth-notice-info {
    background: #eff6ff;
    border-color: #93c5fd;
    color: #1d4ed8;
}
.auth-notice-warn {
    background: #fff7ed;
    border-color: #fdba74;
    color: #9a3412;
}
.auth-notice-danger {
    background: #fef2f2;
    border-color: #fecaca;
    color: #b91c1c;
}
.auth-hero-card,
.auth-compare-card,
.auth-activate-card,
.auth-plan-card {
    border-radius: 22px;
}
.auth-hero-card {
    padding: 28px;
    margin-bottom: 20px;
    overflow: hidden;
    position: relative;
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 54%, #1d4ed8 100%);
    color: #fff;
}
.auth-hero-card::before {
    content: '';
    position: absolute;
    top: -120px;
    right: -70px;
    width: 260px;
    height: 260px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.08);
}
.auth-hero-top,
.auth-stat-grid,
.auth-hero-bottom {
    position: relative;
    z-index: 1;
}
.auth-hero-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 18px;
}
.auth-hero-badge {
    display: inline-flex;
    align-items: center;
    padding: 6px 12px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.12);
    font-size: 12px;
    color: rgba(255, 255, 255, 0.88);
    margin-bottom: 14px;
}
.auth-hero-title {
    font-size: 32px;
    font-weight: 900;
    line-height: 1.1;
    margin-bottom: 8px;
}
.auth-hero-desc {
    max-width: 680px;
    font-size: 14px;
    line-height: 1.8;
    color: rgba(255, 255, 255, 0.82);
}
.auth-hero-chip {
    flex-shrink: 0;
    padding: 10px 14px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    font-size: 13px;
    font-weight: 700;
    white-space: nowrap;
}
.auth-stat-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 18px;
}
.auth-stat-item {
    padding: 14px 16px;
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.08);
    backdrop-filter: blur(4px);
}
.auth-stat-item span {
    display: block;
    font-size: 12px;
    color: rgba(255, 255, 255, 0.65);
    margin-bottom: 6px;
}
.auth-stat-item strong {
    display: block;
    font-size: 15px;
    font-weight: 800;
    word-break: break-all;
}
.auth-hero-bottom {
    display: grid;
    grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr);
    gap: 14px;
}
.auth-hero-field {
    padding: 14px 16px;
    border-radius: 16px;
    background: rgba(10, 18, 34, 0.28);
    border: 1px solid rgba(255, 255, 255, 0.08);
}
.auth-hero-field label {
    display: block;
    font-size: 12px;
    color: rgba(255, 255, 255, 0.66);
    margin-bottom: 8px;
}
.auth-hero-value-row {
    display: flex;
    align-items: center;
    gap: 12px;
}
.auth-hero-value-row-single {
    min-height: 24px;
}
.auth-hero-value {
    flex: 1;
    min-width: 0;
    font-size: 13px;
    color: #fff;
    word-break: break-all;
}
.auth-hero-key {
    font-family: Consolas, Monaco, monospace;
}
.auth-copy-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    font-size: 12px;
    white-space: nowrap;
}
.auth-main-grid {
    display: grid;
    grid-template-columns: minmax(0, 1.25fr) minmax(320px, 0.75fr);
    gap: 20px;
}
.auth-compare-card,
.auth-activate-card,
.auth-plan-card {
    padding: 24px;
}
.auth-compare-card {
    background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
}
.auth-section-head,
.auth-panel-title {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}
.auth-section-title {
    font-size: 18px;
    font-weight: 900;
    color: var(--dark);
    margin-bottom: 4px;
}
.auth-section-subtitle {
    font-size: 12px;
    color: var(--text-light);
}
.compare-focus-tag {
    display: inline-flex;
    align-items: center;
    padding: 6px 10px;
    border-radius: 999px;
    background: #dbeafe;
    color: #1d4ed8;
    font-size: 12px;
    font-weight: 700;
}
.compare-intro,
.auth-plan-note,
.auth-form-note {
    font-size: 12px;
    color: var(--text-light);
    line-height: 1.8;
}
.compare-intro {
    margin-bottom: 14px;
}
.compare-table-wrap {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
}
.license-compare-table {
    width: 100%;
    min-width: 0 !important;
    table-layout: fixed;
    border-collapse: separate;
    border-spacing: 0;
}
.license-compare-table th,
.license-compare-table td {
    padding: 12px 8px;
    text-align: center;
    border-bottom: 1px solid #e5e7eb;
    font-size: 13px;
}
.license-compare-table th:first-child,
.license-compare-table td:first-child {
    width: 43%;
    text-align: left;
}
.license-compare-table th:not(:first-child),
.license-compare-table td:not(:first-child) {
    width: 19%;
}
.license-compare-table thead th {
    position: sticky;
    top: 0;
    background: #f8fafc;
    color: #64748b;
    font-size: 12px;
    font-weight: 800;
    z-index: 1;
}
.feature-cell {
    min-width: 0;
    overflow-wrap: anywhere;
}
.compare-feature-name {
    font-weight: 800;
    color: var(--dark);
    line-height: 1.5;
}
.compare-row-highlight {
    background: #fcfcff;
}
.compare-upgrade-badge {
    display: inline-flex;
    align-items: center;
    margin-left: 8px;
    padding: 4px 8px;
    border-radius: 999px;
    background: #fee2e2;
    color: #b91c1c;
    font-size: 11px;
    font-weight: 800;
}
.compare-value {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 48px;
    max-width: 100%;
    padding: 6px 8px;
    border-radius: 999px;
    font-weight: 800;
    line-height: 1;
}
.compare-yes {
    background: #dcfce7;
    color: #166534;
}
.compare-limit {
    background: #fef3c7;
    color: #92400e;
}
.compare-no {
    background: #fee2e2;
    color: #b91c1c;
}
.auth-side-stack {
    display: grid;
    gap: 20px;
    align-content: start;
}
.auth-panel-title {
    color: var(--dark);
    font-weight: 800;
    margin-bottom: 18px;
}
.auth-label {
    display: block;
    margin-bottom: 6px;
    font-size: 13px;
    color: var(--text);
}
.auth-input {
    width: 100%;
    padding: 11px 14px;
    border: 1px solid var(--border);
    border-radius: 14px;
    font-size: 14px;
    box-sizing: border-box;
    background: #fff;
}
.auth-input-key {
    font-family: Consolas, Monaco, monospace;
}
.auth-btn-primary,
.auth-btn-reset,
.auth-plan-btn {
    width: 100%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 14px;
    border-radius: 14px;
    font-size: 13px;
    font-weight: 800;
}
.auth-btn-primary {
    background: linear-gradient(135deg, #1d4ed8 0%, #2563eb 100%);
    color: #fff;
    border: none;
}
.auth-btn-reset {
    margin-top: 10px;
    background: #fff7ed;
    color: #c2410c;
    border: 1px solid #fdba74;
}
.auth-message {
    margin-top: 12px;
    font-size: 13px;
    text-align: center;
}
.auth-plan-card {
    background: linear-gradient(180deg, #fffdf5 0%, #ffffff 100%);
}
.auth-plan-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 14px;
}
.auth-plan-grid-single {
    grid-template-columns: 1fr;
}
.auth-plan-item {
    position: relative;
    border-radius: 18px;
    padding: 18px;
    background: #fff;
    border: 1px solid #e5e7eb;
}
.auth-plan-item-basic {
    box-shadow: inset 0 0 0 1px rgba(37, 99, 235, 0.06);
}
.auth-plan-item-pro {
    border: 2px solid #1d4ed8;
    box-shadow: 0 16px 38px rgba(29, 78, 216, 0.10);
}
.auth-plan-item-current {
    background: linear-gradient(135deg, #eff6ff 0%, #ffffff 100%);
    border: 1px solid #bfdbfe;
}
.auth-plan-recommend {
    position: absolute;
    top: -10px;
    right: 14px;
    padding: 3px 9px;
    border-radius: 999px;
    background: #1d4ed8;
    color: #fff;
    font-size: 11px;
    font-weight: 800;
}
.auth-plan-name {
    font-size: 17px;
    font-weight: 900;
    color: var(--dark);
    margin-bottom: 8px;
}
.auth-plan-price {
    font-size: 24px;
    font-weight: 900;
    color: var(--dark);
    margin-bottom: 10px;
}
.auth-plan-price span {
    font-size: 12px;
    font-weight: 500;
    color: var(--text-light);
}
.auth-plan-desc {
    font-size: 12px;
    line-height: 1.8;
    color: var(--text-light);
    margin-bottom: 14px;
}
.auth-plan-btn {
    background: #e2e8f0;
    color: #0f172a;
    border: none;
}
.auth-plan-btn-pro {
    background: linear-gradient(135deg, #1d4ed8 0%, #2563eb 100%);
    color: #fff;
}
@media (max-width: 1200px) {
    .auth-main-grid,
    .auth-hero-bottom {
        grid-template-columns: 1fr;
    }
}
@media (max-width: 900px) {
    .auth-stat-grid {
        grid-template-columns: 1fr;
    }
    .auth-hero-top {
        flex-direction: column;
    }
}
@media (max-width: 768px) {
    .auth-hero-card,
    .auth-compare-card,
    .auth-activate-card,
    .auth-plan-card {
        padding: 18px;
    }
    .auth-hero-title {
        font-size: 26px;
    }
    .auth-hero-value-row {
        flex-direction: column;
        align-items: stretch;
    }
    .auth-copy-btn {
        width: 100%;
    }
    .license-compare-table th,
    .license-compare-table td {
        padding: 10px 4px;
        font-size: 12px;
    }
    .license-compare-table th:first-child,
    .license-compare-table td:first-child {
        width: 46%;
    }
    .license-compare-table th:not(:first-child),
    .license-compare-table td:not(:first-child) {
        width: 18%;
    }
    .compare-value {
        min-width: 0;
        padding: 5px 6px;
        font-size: 11px;
        white-space: nowrap;
    }
    .compare-upgrade-badge {
        margin: 4px 0 0;
        padding: 3px 6px;
        font-size: 10px;
    }
}
</style>

<script>
function openBuyWindow() {
    var url = <?php echo json_encode($buyUrl); ?>;
    var width = 1280;
    var height = 860;
    var left = Math.max(0, Math.round((window.screen.width - width) / 2));
    var top = Math.max(0, Math.round((window.screen.height - height) / 2));
    var features = 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',resizable=yes,scrollbars=yes';
    var win = window.open(url, 'yutouLicenseBuy', features);
    if (win) {
        win.focus();
    } else {
        window.location.href = url;
    }
}
function copyKey() {
    var key = document.getElementById('displayKey').textContent.trim();
    if (!key || key === '尚未激活授权 KEY') return;
    navigator.clipboard.writeText(key).then(function() {
        alert('授权KEY已复制到剪贴板');
    }).catch(function() {
        var ta = document.createElement('textarea');
        ta.value = key;
        document.body.appendChild(ta);
        ta.select();
        document.execCommand('copy');
        document.body.removeChild(ta);
        alert('授权KEY已复制到剪贴板');
    });
}
function doActivate() {
    var csrfToken = <?php echo json_encode(getAdminCsrfToken(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>;
    var key = document.getElementById('inputKey').value.trim();
    var domain = document.getElementById('inputDomain').value.trim();
    var msg = document.getElementById('activateMsg');
    if (!key) {
        msg.innerHTML = '<span style="color:var(--danger);">请输入授权KEY</span>';
        return;
    }
    msg.innerHTML = '<span style="color:var(--text-light);">正在验证授权...</span>';
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '?act=auth&action=activate', true);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.setRequestHeader('X-CSRF-Token', csrfToken);
    xhr.onload = function() {
        try {
            var d = JSON.parse(xhr.responseText);
            if (d.code == 200) {
                msg.innerHTML = '<span style="color:var(--success);">授权激活成功，页面将自动刷新...</span>';
                setTimeout(function() { location.reload(); }, 1000);
            } else {
                var debugText = '';
                if (d.debug) {
                    if (d.debug.resp) {
                        debugText = '<br><span style="color:#94a3b8;font-size:12px;word-break:break-all;">' + String(d.debug.resp) + '</span>';
                    } else if (d.debug.curl_err) {
                        debugText = '<br><span style="color:#94a3b8;font-size:12px;word-break:break-all;">' + String(d.debug.curl_err) + '</span>';
                    }
                }
                msg.innerHTML = '<span style="color:var(--danger);">' + (d.msg || '激活失败') + '</span>' + debugText;
            }
        } catch (e) {
            msg.innerHTML = '<span style="color:var(--danger);">连接授权平台失败</span>';
        }
    };
    xhr.onerror = function() {
        msg.innerHTML = '<span style="color:var(--danger);">无法连接到授权平台</span>';
    };
    xhr.send(JSON.stringify({license_key: key, domain: domain, csrf_token: csrfToken}));
}
function doResetLicense() {
    var csrfToken = <?php echo json_encode(getAdminCsrfToken(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>;
    if (!confirm('确定要清除当前授权，并恢复为当前域名的免费版授权吗？')) {
        return;
    }
    var msg = document.getElementById('activateMsg');
    msg.innerHTML = '<span style="color:var(--text-light);">正在恢复免费授权...</span>';
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '?act=auth&action=reset', true);
    xhr.setRequestHeader('X-CSRF-Token', csrfToken);
    xhr.onload = function() {
        try {
            var d = JSON.parse(xhr.responseText);
            if (d.code == 200) {
                msg.innerHTML = '<span style="color:var(--success);">' + (d.msg || '已恢复免费版') + '，页面将自动刷新...</span>';
                setTimeout(function() { location.reload(); }, 1000);
            } else {
                msg.innerHTML = '<span style="color:var(--danger);">' + (d.msg || '操作失败') + '</span>';
            }
        } catch (e) {
            msg.innerHTML = '<span style="color:var(--danger);">操作失败，请稍后重试</span>';
        }
    };
    xhr.onerror = function() {
        msg.innerHTML = '<span style="color:var(--danger);">无法连接到服务器</span>';
    };
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(JSON.stringify({csrf_token: csrfToken}));
}
</script>
