<?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']) ? $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);
    }
}
?>
<div class="page-container fade-in">
    <?php if ($autoResetNotice): ?>
    <div style="margin-bottom:20px;padding:14px 16px;border-radius:12px;background:#ecfdf5;border:1px solid #86efac;color:#166534;font-size:13px;line-height:1.8;">
        <strong>授权已自动恢复免费版：</strong> 检测到原授权域名 <?php echo htmlspecialchars($autoResetNotice['old_domain']); ?> 与当前访问域名 <?php echo htmlspecialchars($autoResetNotice['new_domain']); ?> 不匹配，系统已自动切换为当前域名免费授权。
    </div>
    <?php endif; ?>
    <?php if ($remoteResetNotice): ?>
    <div style="margin-bottom:20px;padding:14px 16px;border-radius:12px;background:#eff6ff;border:1px solid #93c5fd;color:#1d4ed8;font-size:13px;line-height:1.8;">
        <strong>授权已自动恢复免费版：</strong> 检测到授权中心中的 KEY <?php echo htmlspecialchars($remoteResetNotice['old_key']); ?> 已失效或已被删除，系统已切换为当前域名 <?php echo htmlspecialchars($remoteResetNotice['new_domain']); ?> 的免费授权。
    </div>
    <?php endif; ?>
    <?php if ($templateResetNotice): ?>
    <div style="margin-bottom:20px;padding:14px 16px;border-radius:12px;background:#fff7ed;border:1px solid #fdba74;color:#9a3412;font-size:13px;line-height:1.8;">
        <strong>模板已自动回退为免费模板：</strong> <?php echo htmlspecialchars(isset($templateResetReasonMap[$templateResetNotice['reason']]) ? $templateResetReasonMap[$templateResetNotice['reason']] : '模板状态异常'); ?>，系统已自动切换回默认模板以保证站点可继续访问。
    </div>
    <?php endif; ?>
    <?php if ($licenseDomainMismatch): ?>
    <div style="margin-bottom:20px;padding:14px 16px;border-radius:12px;background:#fef2f2;border:1px solid #fecaca;color:#b91c1c;font-size:13px;line-height:1.8;">
        <strong>授权域名不匹配：</strong> 当前访问域名是 <?php echo htmlspecialchars($currentHost); ?>，本地授权绑定域名是 <?php echo htmlspecialchars($licenseDomain); ?>。
        请重新绑定正确的授权KEY，或点击“恢复免费版 / 清除当前授权”重置当前站点授权。
    </div>
    <?php endif; ?>
    <!-- 页面标题 -->
    <div class="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 style="font-size:13px;color:var(--text-light);margin:4px 0 0 0;">绑定、替换授权KEY，或恢复为免费版</p>
        </div>
    </div>

    <!-- 授权KEY卡片 -->
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:20px;">
        <!-- 当前授权状态 -->
        <div class="content-card" style="padding:24px;">
            <div style="display:flex;align-items:center;gap:16px;margin-bottom:20px;">
                <div style="width:60px;height:60px;background:linear-gradient(135deg,var(--primary),#4080FF);border-radius:16px;display:flex;align-items:center;justify-content:center;">
                    <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>
                </div>
                <div>
                    <div style="font-size:13px;color:var(--text-light);margin-bottom:4px;">当前版本</div>
                    <div style="font-size:22px;font-weight:700;color:var(--dark);"><?php echo $currentLevelName; ?></div>
                </div>
            </div>

            <!-- 授权KEY -->
            <div style="margin-bottom:16px;">
                <div style="font-size:12px;color:var(--text-light);margin-bottom:6px;text-transform:uppercase;letter-spacing:0.5px;">授权KEY</div>
                <div style="background:var(--gray-50);border:1px solid var(--border);border-radius:var(--radius);padding:12px 16px;display:flex;align-items:center;justify-content:space-between;gap:12px;">
                    <span id="displayKey" style="font-family:monospace;font-size:13px;color:var(--dark);word-break:break-all;flex:1;">
                        <?php if ($licenseKey): ?>
                        <?php echo htmlspecialchars($licenseKey); ?>
                        <?php else: ?>
                        <span style="color:var(--text-light);font-family:none;">尚未激活授权KEY</span>
                        <?php endif; ?>
                    </span>
                    <?php if ($licenseKey): ?>
                    <button onclick="copyKey()" style="padding:6px 12px;background:var(--primary-bg);color:var(--primary);border:none;border-radius:var(--radius-sm);font-size:12px;cursor:pointer;white-space:nowrap;" title="复制KEY">
                        <svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg> 复制
                    </button>
                    <?php endif; ?>
                </div>
            </div>

            <!-- 授权域名 -->
            <div>
                <div style="font-size:12px;color:var(--text-light);margin-bottom:6px;text-transform:uppercase;letter-spacing:0.5px;">授权域名</div>
                <div style="background:var(--gray-50);border:1px solid var(--border);border-radius:var(--radius);padding:12px 16px;">
                    <span style="font-size:13px;color:var(--dark);word-break:break-all;">
                        <?php if ($licenseDomain): ?>
                        <?php echo htmlspecialchars($licenseDomain); ?>
                        <?php else: ?>
                        <span style="color:var(--text-light);">未绑定域名</span>
                        <?php endif; ?>
                    </span>
                </div>
            </div>
        </div>

        <!-- 版本功能对比 -->
        <div class="content-card" style="padding:20px;">
            <div style="display:flex;align-items:center;justify-content:space-between;gap:12px;margin-bottom:14px;">
                <div style="font-size:14px;font-weight:600;color:var(--dark);">版本功能对比</div>
                <div class="compare-focus-tag">升级后立即解锁更多能力</div>
            </div>
            <div class="compare-intro">已重点标出收费版本新增功能，基础版和专业版的差异会更明显。</div>
            <table class="license-compare-table" style="width:100%;border-collapse:collapse;font-size:13px;">
                <thead>
                    <tr style="background:var(--gray-50);">
                        <th style="padding:8px 12px;text-align:left;border-bottom:1px solid var(--border);color:var(--text-light);font-weight:500;">功能</th>
                        <th style="padding:8px 12px;text-align:center;border-bottom:1px solid var(--border);color:var(--text-light);font-weight:500;">免费版</th>
                        <th style="padding:8px 12px;text-align:center;border-bottom:1px solid var(--border);color:var(--text-light);font-weight:500;">基础版</th>
                        <th style="padding:8px 12px;text-align:center;border-bottom:1px solid var(--border);color:var(--text-light);font-weight:500;">专业版</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $rows = [
                        ['产品数量', '5个', '不限', '不限', true],
                        ['文章/下载管理', '✔', '✔', '✔', false],
                        ['在线升级', '✘', '✔', '✔', true],
                        ['订单管理', '✘', '✘', '✔', true],
                        ['支付接口', '✘', '✘', '✔', true],
                        ['多域名授权', '✘', '✘', '✔', true],
                        ['去版权', '✘', '✔', '✔', true],
                    ];
                    foreach ($rows as $r): ?>
                    <tr class="<?php echo !empty($r[4]) ? 'compare-row-highlight' : ''; ?>">
                        <td style="padding:9px 12px;border-bottom:1px solid var(--border-light);color:var(--text);">
                            <span class="compare-feature-name"><?php echo $r[0]; ?></span>
                            <?php if (!empty($r[4])): ?>
                            <span class="compare-upgrade-badge">升级重点</span>
                            <?php endif; ?>
                        </td>
                        <td style="padding:9px 12px;text-align:center;border-bottom:1px solid var(--border-light);">
                            <span class="compare-value <?php echo $r[1]==='✘' ? 'compare-no' : 'compare-yes'; ?>"><?php echo $r[1]; ?></span>
                        </td>
                        <td style="padding:9px 12px;text-align:center;border-bottom:1px solid var(--border-light);">
                            <span class="compare-value <?php echo $r[2]==='✘' ? 'compare-no' : 'compare-yes'; ?>"><?php echo $r[2]; ?></span>
                        </td>
                        <td style="padding:9px 12px;text-align:center;border-bottom:1px solid var(--border-light);">
                            <span class="compare-value <?php echo $r[3]==='✘' ? 'compare-no' : 'compare-yes'; ?>"><?php echo $r[3]; ?></span>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- 激活/升级区域 -->
    <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
        <!-- 激活KEY -->
        <div class="content-card" style="padding:24px;">
            <div style="font-size:14px;font-weight:600;color:var(--dark);margin-bottom:16px;">
                <svg style="width:22px;height:22px;" 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> 更换 / 重新绑定授权KEY
            </div>
            <div class="form-group">
                <label style="font-size:13px;color:var(--text);margin-bottom:6px;display:block;">授权KEY</label>
                <input type="text" id="inputKey" placeholder="XXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" style="width:100%;padding:10px 14px;border:1px solid var(--border);border-radius:var(--radius);font-size:14px;font-family:monospace;box-sizing:border-box;">
            </div>
            <div class="form-group">
                <label style="font-size:13px;color:var(--text);margin-bottom:6px;display:block;">授权域名</label>
                <input type="text" id="inputDomain" value="<?php echo htmlspecialchars($licenseDomain ?: $_SERVER['HTTP_HOST']); ?>" placeholder="example.com" style="width:100%;padding:10px 14px;border:1px solid var(--border);border-radius:var(--radius);font-size:14px;box-sizing:border-box;">
            </div>
            <button onclick="doActivate()" class="btn btn-primary" style="width:100%;padding:12px;font-size:14px;">
                <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"/></svg> 保存并替换授权
            </button>
            <button onclick="doResetLicense()" type="button" class="btn" style="width:100%;padding:12px;font-size:14px;margin-top:10px;background:#fff7ed;color:#c2410c;border:1px solid #fdba74;">
                <svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 6h18"/><path d="M8 6V4h8v2"/><path d="M19 6l-1 14H6L5 6"/></svg> 恢复免费版 / 清除当前授权
            </button>
            <div style="margin-top:8px;font-size:12px;color:var(--text-light);line-height:1.7;">填写新的授权KEY并保存后，会直接覆盖当前本地授权信息。</div>
            <div id="activateMsg" style="margin-top:10px;font-size:13px;text-align:center;"></div>
        </div>

        <!-- 升级套餐 -->
        <div style="background:linear-gradient(135deg,#fef3c7,#fff8f0);border:1px solid #fde68a;border-radius:var(--radius-lg);padding:24px;">
            <div style="display:flex;align-items:center;gap:10px;margin-bottom:16px;">
                <div style="width:40px;height:40px;background:#f59e0b;border-radius:10px;display:flex;align-items:center;justify-content:center;">
                    <svg style="font-size:20px;color:#fff;" 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>
                </div>
                <div>
                    <div style="font-size:14px;font-weight:600;color:#92400e;">升级解锁更多功能</div>
                    <div style="font-size:12px;color:#b45309;">更高版本，更强能力</div>
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;">
                <?php if ($currentLevel < 2): ?>
                <div style="background:#fff;border:1px solid #e5e7eb;border-radius:var(--radius);padding:16px;">
                    <div style="font-size:16px;font-weight:700;color:var(--dark);margin-bottom:4px;">基础版</div>
                    <div style="font-size:22px;font-weight:700;color:#2563eb;margin-bottom:8px;">¥188 <span style="font-size:12px;font-weight:400;color:var(--text-light);">/永久 /域名</span></div>
                    <div style="font-size:12px;color:var(--text-light);margin-bottom:12px;">订单管理 · 在线升级 · 去版权</div>
                    <button onclick="openBuyWindow()" type="button" class="btn" style="width:100%;padding:9px;background:#2563eb;color:#fff;border:none;border-radius:var(--radius);font-size:13px;cursor:pointer;">立即升级</button>
                </div>
                <?php endif; ?>
                <?php if ($currentLevel < 3): ?>
                <div style="background:#fff;border:2px solid #7c3aed;border-radius:var(--radius);padding:16px;position:relative;">
                    <div style="position:absolute;top:-10px;right:12px;background:#7c3aed;color:#fff;font-size:11px;padding:2px 8px;border-radius:10px;">推荐</div>
                    <div style="font-size:16px;font-weight:700;color:var(--dark);margin-bottom:4px;">专业版</div>
                    <div style="font-size:22px;font-weight:700;color:#7c3aed;margin-bottom:8px;">¥388 <span style="font-size:12px;font-weight:400;color:var(--text-light);">/永久 /域名</span></div>
                    <div style="font-size:12px;color:var(--text-light);margin-bottom:12px;">全功能 · 支付接口 · 多域名</div>
                    <button onclick="openBuyWindow()" type="button" class="btn" style="width:100%;padding:9px;background:#7c3aed;color:#fff;border:none;border-radius:var(--radius);font-size:13px;cursor:pointer;">立即升级</button>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<style>
.compare-focus-tag {
    display: inline-flex;
    align-items: center;
    padding: 6px 10px;
    border-radius: 999px;
    background: linear-gradient(135deg, #dbeafe, #ede9fe);
    color: #4338ca;
    font-size: 12px;
    font-weight: 700;
}
.compare-intro {
    margin-bottom: 14px;
    padding: 10px 12px;
    border-radius: 12px;
    background: #fff7ed;
    border: 1px solid #fdba74;
    color: #9a3412;
    font-size: 12px;
    line-height: 1.7;
}
.license-compare-table th:nth-child(3),
.license-compare-table th:nth-child(4) {
    background: #f8fafc;
}
.compare-row-highlight {
    background: linear-gradient(90deg, rgba(255, 247, 237, 0.9), rgba(255, 255, 255, 0.98));
}
.compare-feature-name {
    font-weight: 700;
    color: #111827;
}
.compare-upgrade-badge {
    display: inline-flex;
    align-items: center;
    margin-left: 8px;
    padding: 2px 8px;
    border-radius: 999px;
    background: #fee2e2;
    color: #b91c1c;
    font-size: 11px;
    font-weight: 700;
}
.compare-value {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 56px;
    padding: 5px 10px;
    border-radius: 999px;
    font-weight: 700;
    line-height: 1;
}
.compare-yes {
    background: #dcfce7;
    color: #166534;
}
.compare-no {
    background: #fee2e2;
    color: #b91c1c;
}
</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 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.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 {
                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.send(JSON.stringify({license_key: key, domain: domain}));
}
function doResetLicense() {
    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.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.send('');
}
</script>
