<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
require_once '../db/config.php';
require_once '../includes/i18n.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    $pdo = db();
    $stmt = $pdo->prepare("SELECT * FROM admins WHERE username = ?");
    $stmt->execute([$username]);
    $admin = $stmt->fetch();
    
    if ($admin && password_verify($password, $admin['password'])) {
        $_SESSION['admin_id'] = $admin['id'];
        $_SESSION['admin_username'] = $admin['username'];
        $_SESSION['admin_role'] = $admin['role'];
        header("Location: index.php");
        exit;
    } else {
        $error = "用户名或密码错误。";
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员登录 - NovaEx</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body { background: #f4f6f9; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; height: 100vh; display: flex; align-items: center; justify-content: center; margin: 0; }
        .login-card { background: white; padding: 40px; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,0.05); width: 100%; max-width: 400px; border: 1px solid #eaecef; }
        .logo { color: #f0b90b; font-size: 2rem; font-weight: 800; text-align: center; margin-bottom: 30px; }
        .form-group { margin-bottom: 20px; }
        .form-group label { display: block; margin-bottom: 8px; font-weight: 600; color: #474d57; font-size: 0.9rem; }
        .form-group input { width: 100%; padding: 12px; border: 1px solid #eaecef; border-radius: 8px; font-size: 1rem; outline: none; box-sizing: border-box; }
        .btn-login { width: 100%; padding: 14px; background: #f0b90b; border: none; border-radius: 8px; color: black; font-weight: bold; font-size: 1rem; cursor: pointer; transition: 0.2s; margin-top: 10px; }
        .btn-login:hover { background: #d9a60a; }
        .error { background: #fff5f5; color: #f6465d; padding: 10px; border-radius: 6px; font-size: 0.85rem; margin-bottom: 20px; text-align: center; border: 1px solid #ffe3e3; }
    </style>
</head>
<body>
    <div class="login-card">
        <div class="logo">NovaEx Admin</div>
        <?php if($error): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        <form method="POST">
            <div class="form-group">
                <label>管理员账号</label>
                <input type="text" name="username" required placeholder="请输入账号">
            </div>
            <div class="form-group">
                <label>登录密码</label>
                <input type="password" name="password" required placeholder="请输入密码">
            </div>
            <button type="submit" class="btn-login">立即登录</button>
        </form>
    </div>
</body>
</html>