<?php
/**
 * Name: Nxz官网系统
 * URI: https://www.nxzlm.com/
 * Description: 逆行者官网系统源码，简单好用带后台
 * Version: 1.0.0
 * Author: 逆行者联盟
 * Ps: 需要多的功能欢迎联系我们扩展定制喔
 */

header('Content-Type: text/html; charset=utf-8');

// 检查是否已经安装
if (file_exists('install.lock')) {
    die('系统已安装！如果需要重新安装，请删除 install/install.lock 文件。');
}

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $db_host = $_POST['db_host'] ?? '127.0.0.1';
    $db_user = $_POST['db_user'] ?? '';
    $db_pass = $_POST['db_pass'] ?? '';
    $db_name = $_POST['db_name'] ?? '';
    $admin_user = $_POST['admin_user'] ?? '';
    $admin_pass = $_POST['admin_pass'] ?? '';

    if (empty($db_host) || empty($db_user) || empty($db_name) || empty($admin_user) || empty($admin_pass)) {
        $error = '请填写完整的数据库信息和管理员信息！';
    } else {
        try {
            // 连接数据库
            $dsn = "mysql:host=$db_host;charset=utf8mb4";
            $pdo = new PDO($dsn, $db_user, $db_pass);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            // 创建数据库并选择
            $pdo->exec("CREATE DATABASE IF NOT EXISTS `$db_name` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci");
            $pdo->exec("USE `$db_name`");

            // 导入SQL文件
            $sqlFile = __DIR__ . '/install.sql';
            if (file_exists($sqlFile)) {
                $sqlContent = file_get_contents($sqlFile);
                $queries = explode(';', $sqlContent);
                foreach ($queries as $query) {
                    $query = trim($query);
                    if (!empty($query)) {
                        $pdo->exec($query);
                    }
                }
            } else {
                throw new Exception('找不到数据库文件 install.sql');
            }

            // 插入管理员账号
            $stmt = $pdo->prepare("INSERT INTO `nxz_admin` (`username`, `password`) VALUES (?, ?)");
            // 密码加密方式，这里使用简单的 md5 加密
            $stmt->execute([$admin_user, md5($admin_pass)]);

            // 生成配置文件
            $configContent = "<?php\n" .
                "/**\n" .
                " * Name: Nxz官网系统\n" .
                " * URI: https://www.nxzlm.com/\n" .
                " * Description: 逆行者官网系统源码，简单好用带后台\n" .
                " * Version: 1.0.0\n" .
                " * Author: 逆行者联盟\n" .
                " */\n\n" .
                "return [\n" .
                "    'host' => '{$db_host}',\n" .
                "    'user' => '{$db_user}',\n" .
                "    'pass' => '{$db_pass}',\n" .
                "    'name' => '{$db_name}',\n" .
                "    'port' => '3306',\n" .
                "];\n";
            file_put_contents(__DIR__ . '/../includes/config.php', $configContent);

            // 生成安装锁定文件
            file_put_contents('install.lock', 'installed');

            $success = '系统安装成功！为了安全起见，建议删除 install 目录。';
        } catch (PDOException $e) {
            $error = '数据库连接失败：' . $e->getMessage();
        } catch (Exception $e) {
            $error = '安装失败：' . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>系统安装 - Nxz官网系统</title>
    <link rel="stylesheet" href="../assets/layui/css/layui.css">
    <style>
        body { background-color: #f2f2f2; }
        .install-container { width: 600px; margin: 100px auto; background-color: #fff; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        .install-header { text-align: center; margin-bottom: 30px; }
        .install-header h2 { font-weight: 300; color: #333; }
        .install-header p { color: #999; margin-top: 10px; }
    </style>
</head>
<body>

<div class="install-container">
    <div class="install-header">
        <h2>Nxz官网系统 - 安装向导</h2>
        <p>欢迎使用逆行者联盟全栈解决方案</p>
    </div>

    <?php if ($error): ?>
    <div class="layui-panel" style="padding: 15px; margin-bottom: 20px; color: #ff5722; border-color: #ff5722;">
        <?php echo $error; ?>
    </div>
    <?php endif; ?>

    <?php if ($success): ?>
    <div class="layui-panel" style="padding: 15px; margin-bottom: 20px; color: #16b777; border-color: #16b777;">
        <?php echo $success; ?>
        <br><br>
        <a href="../index.php" class="layui-btn">访问前台</a>
        <a href="../admin/index.php" class="layui-btn layui-btn-normal">进入后台</a>
    </div>
    <?php else: ?>
    <form class="layui-form layui-form-pane" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label">数据库地址</label>
            <div class="layui-input-block">
                <input type="text" name="db_host" value="127.0.0.1" lay-verify="required" placeholder="请输入数据库地址" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数据库用户</label>
            <div class="layui-input-block">
                <input type="text" name="db_user" lay-verify="required" placeholder="请输入数据库用户名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数据库密码</label>
            <div class="layui-input-block">
                <input type="password" name="db_pass" placeholder="请输入数据库密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数据库名称</label>
            <div class="layui-input-block">
                <input type="text" name="db_name" lay-verify="required" placeholder="请输入数据库名称" class="layui-input">
            </div>
        </div>

        <hr style="margin: 30px 0;">

        <div class="layui-form-item">
            <label class="layui-form-label">管理员账号</label>
            <div class="layui-input-block">
                <input type="text" name="admin_user" value="admin" lay-verify="required" placeholder="请输入管理员账号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">管理员密码</label>
            <div class="layui-input-block">
                <input type="password" name="admin_pass" lay-verify="required" placeholder="请输入管理员密码" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit>立即安装</button>
        </div>
    </form>
    <?php endif; ?>
</div>

<script src="../assets/layui/layui.js"></script>
<script>
layui.use(['form'], function(){
    var form = layui.form;
});
</script>
</body>
</html>