<?php
/**
 * 2026企业CMS - 后台登录页面
 */
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

// 引入配置文件（必须在输出 HTML 之前）
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/auth.php';

// 已登录则跳转后台
if (isset($_SESSION['admin_id'])) {
    header('Location: /admin/index.php');
    exit;
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    
    if (!$username || !$password) {
        $error = '请输入用户名和密码！';
    } elseif (admin_login($username, $password)) {
        header('Location: /admin/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>后台登录 - <?php echo SITE_NAME; ?></title>
    <style>
        * { margin:0; padding:0; box-sizing:border-box; }
        body { font-family:'Microsoft YaHei',Arial,sans-serif; background:#f0f2f5; min-height:100vh; display:flex; align-items:center; justify-content:center; }
        .login-box { width:400px; background:#fff; border-radius:8px; box-shadow:0 2px 20px rgba(0,0,0,0.1); padding:40px; }
        .login-logo { text-align:center; margin-bottom:30px; }
        .login-logo h1 { font-size:28px; color:#2c82e0; }
        .login-logo p { color:#666; font-size:14px; margin-top:5px; }
        .login-branding { text-align:center; margin-top:20px; padding-top:20px; border-top:1px solid #eee; }
        .login-branding p { color:#999; font-size:12px; }
        .login-branding a { color:#2c82e0; text-decoration:none; }
        .login-branding a:hover { text-decoration:underline; }
        .form-group { margin-bottom:20px; }
        .form-group label { display:block; margin-bottom:8px; color:#333; font-weight:bold; }
        .form-group input { width:100%; padding:12px 15px; border:1px solid #ddd; border-radius:4px; font-size:14px; transition:border-color 0.3s; }
        .form-group input:focus { outline:none; border-color:#2c82e0; }
        .btn-login { width:100%; padding:12px; background:#2c82e0; color:#fff; border:none; border-radius:4px; font-size:16px; cursor:pointer; transition:background 0.3s; }
        .btn-login:hover { background:#1a6bc4; }
        .error-msg { background:#f8d7da; color:#721c24; padding:12px; border-radius:4px; margin-bottom:20px; text-align:center; }
        .copyright { text-align:center; color:#999; font-size:12px; margin-top:20px; }
    </style>
</head>
<body>
    <div class="login-box">
        <div class="login-logo">
            <h1><?php echo SITE_NAME; ?></h1>
            <p>企业内容管理系统</p>
        </div>
        
        <?php if ($error): ?>
        <div class="error-msg"><?php echo $error; ?></div>
        <?php endif; ?>
        
        <form method="post" action="">
            <div class="form-group">
                <label>用户名</label>
                <input type="text" name="username" required autofocus 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 class="copyright">
            © 2026 2026企业CMS - 长沙吧聚网络科技有限公司
            <br><small>默认账号:admin 密码:admin888</small>
        </div>
        <div class="login-branding">
            <p>Powered by <a href="https://barju.cn" target="_blank">长沙吧聚网络科技有限公司</a></p>
        </div>
    </div>
</body>
</html>
