<?php
/**
 * 王一一 · 管理员登录
 */
require_once __DIR__ . '/../config.php';

// 如果已经登录，直接跳转到写日记页
if (is_logged_in()) {
    header('Location: write.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $password = isset($_POST['password']) ? trim($_POST['password']) : '';

    if ($password === '') {
        $error = '请输入密码';
    } elseif ($password === ADMIN_PASSWORD) {
        $_SESSION['admin'] = true;
        header('Location: write.php');
        exit;
    } else {
        $error = '密码不对哦～再试试？';
    }
}

$pageTitle = '登录 — ' . SITE_NAME;
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= h($pageTitle) ?></title>
    <link rel="stylesheet" href="../style.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Fredoka:wght@400;500;600;700&family=Nunito:wght@400;500;600;700&family=Noto+Sans+SC:wght@400;500;700&family=ZCOOL+KuaiLe&display=swap" rel="stylesheet">
</head>
<body>

<!-- 导航栏 -->
<nav class="navbar scrolled">
    <div class="container nav-container">
        <a href="../index.html" class="nav-logo"><?= SITE_NAME ?></a>
        <ul class="nav-menu">
            <li><a href="../index.html" class="nav-link">首页</a></li>
            <li><a href="../index.html#about" class="nav-link">关于我</a></li>
            <li><a href="../index.html#works" class="nav-link">作品集</a></li>
            <li><a href="../diary.php" class="nav-link active">📝 日记</a></li>
            <li><a href="../index.html#contact" class="nav-link">联系我</a></li>
        </ul>
        <button class="nav-toggle" aria-label="切换菜单">
            <span></span><span></span><span></span>
        </button>
    </div>
</nav>

<section class="login-section">
    <div class="login-card">
        <div class="login-icon">🔐</div>
        <h2>管理员登录</h2>
        <p class="login-desc">输入密码后就可以写日记啦～</p>

        <?php if ($error): ?>
        <div class="login-error"><?= h($error) ?></div>
        <?php endif; ?>

        <form method="POST" class="login-form">
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码" required autofocus>
            </div>
            <button type="submit" class="btn btn-primary btn-submit">登录</button>
        </form>

        <p class="login-back">
            <a href="../diary.php">← 回到日记列表</a>
        </p>
    </div>
</section>

<script src="../script.js"></script>
</body>
</html>
