<?php
/**
 * 后台 - 登录页面
 */
require_once __DIR__ . '/../includes/init.php';

// 已登录则跳转
if (isLoggedIn()) {
    header('Location: ' . SITE_URL . '/admin/');
    exit;
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!verifyCsrfToken($_POST[CSRF_TOKEN_NAME] ?? '')) {
        $error = '安全验证失败，请刷新页面重试';
    } else {
        $username = sanitizeInput($_POST['username'] ?? '');
        $password = $_POST['password'] ?? '';

        if (empty($username) || empty($password)) {
            $error = '请输入用户名和密码';
        } else {
            $result = attemptLogin($username, $password);
            if ($result['ok']) {
                $redirect = $_SESSION['redirect_after_login'] ?? (SITE_URL . '/admin/');
                unset($_SESSION['redirect_after_login']);
                header('Location: ' . $redirect);
                exit;
            } else {
                $error = $result['error'];
            }
        }
    }
}

$pageTitle = '登录';
?><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="theme-color" content="<?php echo themeColor(); ?>">
    <title>登录 - <?php echo h(getSetting('site_name', '深夜房间')); ?></title>
    <link rel="stylesheet" href="<?php echo MDUI_CSS; ?>">
    <link rel="stylesheet" href="<?php echo ICONS_CSS; ?>">
    <link rel="stylesheet" href="<?php echo SITE_URL; ?>/assets/css/style.css">
    <style>:root { --theme-color: <?php echo themeColor(); ?>; --accent-color: <?php echo accentColor(); ?>; }</style>
</head>
<body class="mdui-theme-layout-dark" style="background:#1a1d23;">
<div class="bg-layer"></div>

<div class="login-page">
    <div class="mdui-card login-card">
        <div class="login-header">
            <i class="mdui-icon material-icons">nights_stay</i>
            <h2><?php echo h(getSetting('site_name', '深夜房间')); ?></h2>
            <p style="color:#78909c;font-size:13px;">登录到你的私人空间</p>
        </div>
        <div class="mdui-card-content" style="padding:24px;">
            <?php if ($error): ?>
            <div class="mdui-chip" style="background:rgba(244,67,54,0.1);color:#ef9a9a;margin-bottom:16px;width:100%;">
                <span class="mdui-chip-title"><?php echo h($error); ?></span>
            </div>
            <?php endif; ?>

            <form method="post">
                <?php echo csrfField(); ?>
                <div class="mdui-textfield mdui-textfield-floating-label">
                    <i class="mdui-textfield-icon mdui-icon material-icons">person</i>
                    <input type="text" class="mdui-textfield-input" name="username" required autofocus>
                    <label class="mdui-textfield-label">用户名</label>
                </div>
                <div class="mdui-textfield mdui-textfield-floating-label">
                    <i class="mdui-textfield-icon mdui-icon material-icons">lock</i>
                    <input type="password" class="mdui-textfield-input" name="password" required>
                    <label class="mdui-textfield-label">密码</label>
                </div>
                <button type="submit" class="mdui-btn mdui-btn-raised mdui-ripple mdui-color-deep-orange mdui-btn-block" style="margin-top:8px;">进入房间</button>
            </form>

            <div style="text-align:center;margin-top:20px;">
                <a href="<?php echo SITE_URL; ?>/" style="font-size:13px;color:#546e7a;">
                    <i class="mdui-icon material-icons" style="font-size:14px;">arrow_back</i> 返回首页
                </a>
            </div>
        </div>
    </div>
</div>

<script src="<?php echo JQUERY_JS; ?>"></script>
<script src="<?php echo MDUI_JS; ?>"></script>
</body>
</html>
