<?php
include_once __DIR__ . '/../inc/admin_path.php';

$lock = __DIR__ . '/install.lock';
if(is_file($lock)){
    exit('系统已安装。如需重新安装，请先删除 /install/install.lock');
}

function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }

$step = intval($_GET['step'] ?? 1);
if($step < 1 || $step > 4) $step = 1;

$msg = '';
$dirs = array(
    '../cache',
    '../cache/static',
    '../cache/push',
    '../cache/oplog',
    '../cache/license',
    '../data',
    '../data/sites',
    '../assets/uploads',
    '../plugins',
    '../themes',
    '../data/content_center',
    '../data/content_center/global',
    '../data/content_center/global/news',
);

if($_SERVER['REQUEST_METHOD'] === 'POST' && $step === 2){
    foreach($dirs as $dir){
        $path = __DIR__ . '/' . $dir;
        if(!is_dir($path)) @mkdir($path, 0777, true);
    }
    file_put_contents(__DIR__ . '/../robots.txt', "User-agent: *\nAllow: /\n\nDisallow: /yanscms/\nDisallow: /cache/\nDisallow: /config/\nDisallow: /data/\nDisallow: /inc/\nDisallow: /install/\nDisallow: /rewrite/\n\nSitemap: /sitemap.xml\n");
    $step = 3;
    $msg = '运行目录与 robots.txt 已处理完成';
}

if($_SERVER['REQUEST_METHOD'] === 'POST' && $step === 3){
    $user = trim($_POST['admin_user'] ?? 'admin');
    $pass = trim($_POST['admin_pass'] ?? 'admin123');
    $hash = password_hash($pass, PASSWORD_DEFAULT);
    $config = "<?php\n";
    $config .= "define('ADMIN_USER', '" . addslashes($user) . "');\n";
    $config .= "define('ADMIN_PASS_HASH', '" . addslashes($hash) . "');\n";
    $config .= "define('ADMIN_PASS', '');\n";
    $config .= "define('ADMIN_MAX_FAILS', 5);\n";
    $config .= "define('ADMIN_LOCK_SECONDS', 600);\n";
    $config .= "define('ADMIN_CAPTCHA_TYPE', 'math');\n";
    file_put_contents(__DIR__ . '/../' . cms_admin_dir() . '/config.php', $config);
    $step = 4;
}

if($_SERVER['REQUEST_METHOD'] === 'POST' && $step === 4){
    $license = trim($_POST['license_key'] ?? '');
    $file = __DIR__ . '/../config/license.php';
    $cfg = is_file($file) ? include $file : array();
    $cfg['license_key'] = $license;
    if(empty($cfg['public_key'])) $cfg['public_key'] = '';
    if(empty($cfg['signature_alg'])) $cfg['signature_alg'] = 'RS256';
    $cfg['restrict_admin'] = true;
    file_put_contents($file, "<?php\nreturn " . var_export($cfg, true) . ";\n");
    file_put_contents($lock, date('Y-m-d H:i:s'));
    header('Location: ' . cms_admin_url('register.php'));
    exit;
}

$checks = array(
    'PHP 版本 >= 7.1' => version_compare(PHP_VERSION, '7.1.0', '>='),
    'curl 扩展' => function_exists('curl_init'),
    'openssl 扩展' => extension_loaded('openssl'),
    'json 扩展' => function_exists('json_encode'),
);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>炎枢站群安装向导</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
*{box-sizing:border-box}
body{margin:0;font-family:Arial,"Microsoft YaHei",sans-serif;background:#f5f7fb;color:#172033}
body:before{content:"";position:fixed;inset:0;background:linear-gradient(135deg,rgba(31,111,235,.08),rgba(15,23,42,0) 42%),radial-gradient(circle at 14% 12%,rgba(31,111,235,.12),transparent 28%);pointer-events:none}
.page{position:relative;min-height:100vh;display:flex;align-items:center;justify-content:center;padding:42px 18px}
.shell{width:min(1080px,100%);display:grid;grid-template-columns:330px minmax(0,1fr);background:#fff;border:1px solid #d9e1ec;border-radius:18px;box-shadow:0 22px 58px rgba(15,23,42,.12);overflow:hidden}
.brand{background:#111827;color:#eef2f7;padding:34px 30px;display:flex;flex-direction:column;justify-content:space-between;min-height:640px}
.brand-mark{width:48px;height:48px;border-radius:14px;background:#1f6feb;display:flex;align-items:center;justify-content:center;color:#fff;font-weight:800;font-size:22px;margin-bottom:18px;box-shadow:0 14px 30px rgba(31,111,235,.28)}
.brand h1{margin:0;font-size:28px;line-height:1.25;letter-spacing:0}
.brand p{margin:12px 0 0;color:#cbd5e1;line-height:1.8;font-size:14px}
.brand-meta{display:grid;gap:12px;margin-top:30px}
.meta-item{padding:14px 0;border-top:1px solid rgba(226,232,240,.12)}
.meta-item strong{display:block;font-size:13px;color:#f8fafc;margin-bottom:4px}
.meta-item span{font-size:13px;color:#aab6c7}
.content{padding:34px}
.topline{display:flex;justify-content:space-between;gap:16px;align-items:flex-start;margin-bottom:26px}
.topline h2{margin:0;font-size:24px;color:#111827;letter-spacing:0}
.topline p{margin:8px 0 0;color:#64748b;line-height:1.7}
.badge{white-space:nowrap;border:1px solid #d9e1ec;background:#f8fafc;color:#475569;border-radius:999px;padding:8px 12px;font-size:13px}
.steps{display:grid;grid-template-columns:repeat(4,1fr);gap:10px;margin:0 0 28px}
.step{border:1px solid #d9e1ec;border-radius:12px;padding:12px;background:#f8fafc;color:#64748b}
.step b{display:block;color:#334155;font-size:14px;margin-bottom:4px}
.step small{font-size:12px}
.step.active{border-color:#1f6feb;background:#eef5ff;box-shadow:inset 0 0 0 1px rgba(31,111,235,.12)}
.step.active b{color:#1558c7}
.panel{border:1px solid #d9e1ec;border-radius:14px;background:#fff;padding:24px;box-shadow:0 10px 30px rgba(15,23,42,.05)}
.panel h3{margin:0 0 10px;font-size:20px;color:#111827}
.panel-desc{margin:0 0 22px;color:#64748b;line-height:1.7}
.notice{margin:0 0 20px;background:#f0fdf4;color:#166534;border:1px solid #bbf7d0;padding:12px 14px;border-radius:12px}
label{display:block;margin-bottom:18px;color:#334155;font-weight:700}
.field-note{display:block;font-weight:400;color:#64748b;font-size:13px;margin:6px 0 0;line-height:1.6}
input,textarea{width:100%;padding:13px 14px;border:1px solid #d9e1ec;border-radius:10px;margin-top:8px;font:inherit;color:#111827;background:#fbfdff;outline:none;transition:.18s border,.18s box-shadow,.18s background}
input:focus,textarea:focus{border-color:#1f6feb;box-shadow:0 0 0 4px rgba(31,111,235,.12);background:#fff}
textarea{min-height:150px;resize:vertical}
.actions{display:flex;align-items:center;gap:12px;margin-top:22px}
button,.btn{display:inline-flex;align-items:center;justify-content:center;min-height:42px;padding:0 18px;background:#1f6feb;color:#fff;border:0;border-radius:10px;text-decoration:none;font-weight:700;cursor:pointer;box-shadow:none}
button:hover,.btn:hover{background:#1558c7}
.check-list{display:grid;gap:12px;margin-top:18px}
.check-card{display:flex;justify-content:space-between;gap:16px;align-items:center;padding:14px 16px;border:1px solid #d9e1ec;border-radius:12px;background:#f8fafc}
.check-card span{display:block;color:#64748b;font-size:13px;margin-top:4px}
.status-pill{border-radius:999px;padding:6px 10px;font-size:13px;font-weight:700;font-style:normal}
.status-pill.ok{background:#dcfce7;color:#15803d}
.status-pill.bad{background:#fee2e2;color:#b91c1c}
.summary{display:grid;grid-template-columns:repeat(3,1fr);gap:12px;margin-top:20px}
.summary div{padding:14px;border-radius:12px;background:#f8fafc;border:1px solid #d9e1ec}
.summary strong{display:block;color:#111827;font-size:14px;margin-bottom:4px}
.summary span{color:#64748b;font-size:13px;line-height:1.6}
@media(max-width:860px){
    .shell{grid-template-columns:1fr}
    .brand{min-height:auto}
    .steps{grid-template-columns:repeat(2,1fr)}
    .topline{display:block}
    .badge{display:inline-flex;margin-top:12px}
}
@media(max-width:520px){
    .page{padding:18px 10px}
    .content,.brand{padding:24px 18px}
    .steps,.summary{grid-template-columns:1fr}
    .actions{display:block}
    .actions .btn,.actions button{width:100%;margin-top:10px}
}
</style>
</head>
<body>
<div class="page">
    <div class="shell">
        <aside class="brand">
            <div>
                <div class="brand-mark">炎</div>
                <h1>炎枢站群</h1>
                <p>面向多行业企业站点的轻量站群系统，完成环境检查、目录初始化、管理员配置与授权接入后即可进入后台。</p>
                <div class="brand-meta">
                    <div class="meta-item"><strong>部署模式</strong><span>文件型 CMS，适合中小规模企业站群快速交付。</span></div>
                    <div class="meta-item"><strong>授权方式</strong><span>授权中心私钥签发，客户端公钥验签，支持在线状态同步。</span></div>
                    <div class="meta-item"><strong>后台入口</strong><span>安装完成后自动进入客户注册与后台初始化流程。</span></div>
                </div>
            </div>
            <p>YanShu Site Cluster CMS</p>
        </aside>
        <main class="content">
            <div class="topline">
                <div>
                    <h2>安装向导</h2>
                    <p>按步骤完成基础环境、运行目录、管理员账号和系统授权配置。</p>
                </div>
                <div class="badge">当前步骤 <?php echo intval($step); ?> / 4</div>
            </div>

            <div class="steps">
                <div class="step <?php echo $step === 1 ? 'active' : ''; ?>"><b>1 环境检测</b><small>PHP 与扩展检查</small></div>
                <div class="step <?php echo $step === 2 ? 'active' : ''; ?>"><b>2 初始化</b><small>目录与 robots.txt</small></div>
                <div class="step <?php echo $step === 3 ? 'active' : ''; ?>"><b>3 管理员</b><small>后台账号密码</small></div>
                <div class="step <?php echo $step === 4 ? 'active' : ''; ?>"><b>4 系统授权</b><small>授权码写入</small></div>
            </div>

            <?php if($msg): ?><p class="notice"><?php echo h($msg); ?></p><?php endif; ?>

            <?php if($step === 1): ?>
            <section class="panel">
                <h3>环境检测</h3>
                <p class="panel-desc">安装前请确认 PHP 版本和必要扩展可用。未通过项目建议先在服务器环境中启用后再继续。</p>
                <div class="check-list">
                    <?php foreach($checks as $k => $v): ?>
                    <div class="check-card">
                        <div><strong><?php echo h($k); ?></strong><span>系统运行必要条件</span></div>
                        <em class="status-pill <?php echo $v ? 'ok' : 'bad'; ?>"><?php echo $v ? '通过' : '未通过'; ?></em>
                    </div>
                    <?php endforeach; ?>
                </div>
                <div class="actions"><a class="btn" href="?step=2">下一步</a></div>
            </section>
            <?php endif; ?>

            <?php if($step === 2): ?>
            <section class="panel">
                <h3>初始化运行目录</h3>
                <p class="panel-desc">系统会创建缓存、日志、上传、站点数据等必要目录，并生成基础 robots.txt 文件。</p>
                <div class="summary">
                    <div><strong>缓存目录</strong><span>cache/static、cache/push、cache/license</span></div>
                    <div><strong>数据目录</strong><span>data/sites、data/content_center</span></div>
                    <div><strong>上传目录</strong><span>assets/uploads</span></div>
                </div>
                <form method="post"><div class="actions"><button>执行初始化</button></div></form>
            </section>
            <?php endif; ?>

            <?php if($step === 3): ?>
            <section class="panel">
                <h3>设置管理员账号</h3>
                <p class="panel-desc">该账号用于登录客户端后台。上线前请使用强密码，避免继续使用默认密码。</p>
                <form method="post">
                    <label>管理员账号
                        <input name="admin_user" value="admin" autocomplete="username">
                    </label>
                    <label>管理员密码
                        <input name="admin_pass" value="admin123" type="password" autocomplete="new-password">
                        <span class="field-note">建议使用 8 位以上，包含字母、数字和符号的密码。</span>
                    </label>
                    <div class="actions"><button>保存管理员</button></div>
                </form>
            </section>
            <?php endif; ?>

            <?php if($step === 4): ?>
            <section class="panel">
                <h3>写入系统授权</h3>
                <p class="panel-desc">请粘贴授权中心签发的完整授权码。授权码保存后会生成安装锁，并跳转到客户注册页面。</p>
                <form method="post">
                    <label>授权码
                        <textarea name="license_key" placeholder="粘贴授权中心签发的 license_key"></textarea>
                        <span class="field-note">授权码格式为 payload.signature，请勿删改中间的英文句点。</span>
                    </label>
                    <div class="actions"><button>完成安装</button></div>
                </form>
            </section>
            <?php endif; ?>
        </main>
    </div>
</div>
</body>
</html>
