<?php
/**
 * 后台管理系统登录页
 */
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/AdminAuth.php';

$auth = new AdminAuth();

// 已登录则跳转
if ($auth->isLoggedIn()) {
    header('Location: index.php');
    exit;
}

$error = '';

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

    if ($auth->login($username, $password)) {
        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>后台登录 - 一课</title>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { background: #f0f2f5; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; }
        .login-wrapper {
            display: flex; justify-content: center; align-items: center;
            min-height: 100vh; padding: 20px;
        }
        .login-card {
            background: white; border-radius: 12px; padding: 40px 32px;
            width: 100%; max-width: 400px;
            box-shadow: 0 8px 40px rgba(0,0,0,0.08);
        }
        .login-logo { text-align: center; margin-bottom: 32px; }
        .login-logo i { font-size: 48px; color: #374151; margin-bottom: 12px; }
        .login-logo h2 { font-size: 22px; color: #1f2937; font-weight: 600; }
        .login-logo p { font-size: 13px; color: #9ca3af; margin-top: 4px; }
        .form-group { margin-bottom: 20px; }
        .form-label { display: block; margin-bottom: 6px; font-size: 14px; color: #374151; font-weight: 500; }
        .form-input {
            width: 100%; padding: 10px 14px; border: 1px solid #d1d5db;
            border-radius: 8px; font-size: 14px; transition: border-color 0.2s;
        }
        .form-input:focus { outline: none; border-color: #374151; box-shadow: 0 0 0 3px rgba(55,65,81,0.1); }
        .btn-login {
            width: 100%; padding: 12px; background: #374151; color: white;
            border: none; border-radius: 8px; font-size: 15px; font-weight: 500;
            cursor: pointer; transition: background 0.15s;
        }
        .btn-login:hover { background: #1f2937; }
        .error-msg { background: #fef2f2; color: #ef4444; padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; font-size: 14px; }
        .login-tip { margin-top: 24px; padding: 14px; background: #f9fafb; border-radius: 8px; font-size: 13px; color: #6b7280; }
    </style>
</head>
<body>
<div class="login-wrapper">
    <div class="login-card">
        <div class="login-logo">
            <i class="fas fa-shield-alt"></i>
            <h2>一课管理后台</h2>
            <p>Administration Panel</p>
        </div>
        <?php if ($error): ?>
        <div class="error-msg"><i class="fas fa-exclamation-circle"></i> <?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>
        <form method="POST">
            <div class="form-group">
                <label class="form-label">用户名</label>
                <input type="text" class="form-input" name="username" required placeholder="请输入管理员账号">
            </div>
            <div class="form-group">
                <label class="form-label">密码</label>
                <input type="password" class="form-input" name="password" required placeholder="请输入密码">
            </div>
            <button type="submit" class="btn-login"><i class="fas fa-sign-in-alt"></i> 登录</button>
        </form>
        <div class="login-tip">
            <i class="fas fa-info-circle"></i> 默认账号: admin / admin123
        </div>
    </div>
</div>
</body>
</html>