<?php
require_once __DIR__ . '/auth.php';

if (is_logged_in()) {
    header('Location: index.php');
    exit;
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if (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 href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Ma+Shan+Zheng&display=swap" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Inter', sans-serif;
        }
        .login-card {
            width: 100%;
            max-width: 400px;
            padding: 40px;
            background: #fff;
            border-radius: 16px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.05);
        }
        .login-brand {
            font-family: 'Ma+Shan+Zheng', cursive;
            font-size: 2.5rem;
            text-align: center;
            margin-bottom: 30px;
        }
        .login-brand span:nth-child(1) { color: #3b82f6; }
        .login-brand span:nth-child(2) { color: #10b981; }
        .login-brand span:nth-child(3) { color: #f59e0b; }
        .login-brand span:nth-child(4) { color: #ef4444; }
        
        .form-control {
            padding: 12px;
            border-radius: 8px;
            border: 1px solid #dee2e6;
        }
        .btn-primary {
            padding: 12px;
            border-radius: 8px;
            width: 100%;
            font-weight: 600;
        }
    </style>
</head>
<body>
    <div class="login-card shadow-lg">
        <div class="login-brand">
            <span>智</span><span>域</span><span>科</span><span>技</span>
        </div>
        <h5 class="text-center mb-4 text-muted">后台管理登录</h5>
        
        <?php if ($error): ?>
        <div class="alert alert-danger py-2 small"><?= $error ?></div>
        <?php endif; ?>
        
        <form method="POST">
            <div class="mb-3">
                <label class="form-label small text-muted">用户名</label>
                <input type="text" name="username" class="form-control" required autofocus>
            </div>
            <div class="mb-4">
                <label class="form-label small text-muted">密码</label>
                <input type="password" name="password" class="form-control" required>
            </div>
            <button type="submit" class="btn btn-primary">立即登录</button>
        </form>
        
        <div class="text-center mt-4">
            <a href="../index.php" class="text-decoration-none small text-muted">返回网站首页</a>
        </div>
    </div>
</body>
</html>
