<?php
session_start();
require 'config.php';
if (!isset($conn)) {
    die("连接失败：\$conn 未定义");
}
// 检测是否存在有效的 Token
$token = isset($_COOKIE['auth_token']) ? $_COOKIE['auth_token'] : null;
if ($token) {
    $stmt = $conn->prepare("SELECT id, username FROM users WHERE token = ?");
    $stmt->bind_param("s", $token);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($id, $username);
    $stmt->fetch();

    if ($stmt->num_rows > 0) {
        // 存储用户信息到会话
        $_SESSION['user_id'] = $id;
        $_SESSION['username'] = $username;

        // 跳转到控制台页面
        header("Location: ../console/index.php");
        exit();
    }
}

// 如果没有有效的 Token，显示登录表单
include 'login.php'; // 包含 login.php 文件以显示登录表单
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>圆周云境</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <!-- 标题栏 -->
    <div class="top-bar">
        <div class="top-bar-content">
            <h2>圆周云境</h2>
        </div>
        <div class="blue-bar"></div>
    </div>

    <!-- 登录/注册切换 -->
    <div class="auth-container">
        <div class="auth-header">
            <h1 id="auth-title">登录至CCS</h1>
            <div class="switch-buttons">
                <button id="switch-to-login" class="switch-button" onclick="switchToLogin()">登录</button>
                <button id="switch-to-register" class="switch-button" onclick="switchToRegister()">注册</button>
            </div>
        </div>

        <!-- 登录表单 -->
        <div id="login-form" class="form-container" style="display: block;">
            <form action="./login.php" method="post">
                <label for="login-username">账号</label>
                <input type="text" id="login-username" name="username" placeholder="注册时填写的圆周云境用户账号" required>

                <label for="login-password">密码</label>
                <input type="password" id="login-password" name="password" placeholder="密码" required>

                <button type="submit">登录</button>
                <p>忘记密码？<a href="#">查看微信小程序</a></p>
            </form>
        </div>

        <!-- 注册表单 -->
        <div id="register-form" class="form-container" style="display: none;">
            <!--<form action="./register.php" method="post">
                <label for="register-username">账号</label>
                <input type="text" id="register-username" name="username" placeholder="请输入账号" required>

                <label for="register-email">邮箱</label>
                <input type="email" id="register-email" name="email" placeholder="请输入邮箱" required>

                <label for="register-password">密码</label>
                <input type="password" id="register-password" name="password" placeholder="请输入密码" required>

                <label for="register-confirm-password">确认密码</label>
                <input type="password" id="register-confirm-password" name="confirm-password" placeholder="请确认密码" required>

                <button type="submit">注册</button>
            </form>-->
            <center>
                <h2>503 Service Unavailable!</h2>
                <hr>
                <p>API程序接口服务器维护中，注册暂不可用!</p>
            </center>
        </div>
    </div>

    <script>
        function switchToLogin() {
            document.getElementById('auth-title').innerText = '登录至CCS';
            document.getElementById('login-form').style.display = 'block';
            document.getElementById('register-form').style.display = 'none';
        }

        function switchToRegister() {
            document.getElementById('auth-title').innerText = '注册至CCS';
            document.getElementById('login-form').style.display = 'none';
            document.getElementById('register-form').style.display = 'block';
        }

        // 默认显示登录表单
        window.onload = switchToLogin;
    </script>
</body>
</html>
