<?php
session_start();

// 清空本次IP登录错误锁定
$lockKey = "login_err_" . $_SERVER['REMOTE_ADDR'];
unset($_SESSION[$lockKey]);

// 已登录直接跳监控首页
if (!empty($_SESSION['user_id'])) {
    header("Location: dashboard.php");
    exit;
}

$dbHost = "10.150.117.190";
$dbUser = "root";
$dbPass = "hp93000";
$dbName = "alert_mail_stat";
$errMsg = "";

// 限制5分钟最多5次错误密码，防暴力破解
$lockKey = "login_err_" . $_SERVER['REMOTE_ADDR'];
if (!isset($_SESSION[$lockKey])) $_SESSION[$lockKey] = ['num'=>0,'time'=>time()];
$loginErr = $_SESSION[$lockKey];
// 超过5分钟重置计数
if (time() - $loginErr['time'] > 300) {
    $_SESSION[$lockKey] = ['num'=>0,'time'=>time()];
    $loginErr = $_SESSION[$lockKey];
}

if ($_POST) {
    $username = trim($_POST['username'] ?? '');
    $pwd = trim($_POST['pwd'] ?? '');
    if ($loginErr['num'] >= 5) {
        $errMsg = "密码错误次数过多，请5分钟后重试";
    } elseif (!$username || !$pwd) {
        $errMsg = "账号密码不能为空";
    } else {
        $conn = mysqli_connect($dbHost, $dbUser, $dbPass, $dbName);
        $u = mysqli_real_escape_string($conn, $username);
        $sql = "SELECT id,username,password,role FROM sys_user WHERE username='$u' LIMIT 1";
        $res = mysqli_query($conn, $sql);
        $user = mysqli_fetch_assoc($res);
        if ($user && password_verify($pwd, $user['password'])) {
            // 登录成功，清空错误计数，写入会话
            $_SESSION[$lockKey] = ['num'=>0,'time'=>time()];
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['role'] = $user['role'];
            $_SESSION['login_time'] = time();
            // 更新最后登录时间
            mysqli_query($conn, "UPDATE sys_user SET last_login=NOW() WHERE id=".$user['id']);
            header("Location: dashboard.php");
            exit;
        } else {
            $_SESSION[$lockKey]['num']++;
            $errMsg = "账号或密码错误";
        }
        mysqli_close($conn);
    }
}
?>
<!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.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{background:#152c5b;font-family:"PingFang SC","Microsoft YaHei";display:flex;align-items:center;justify-content:center;min-height:100vh}
.login-box{background:#fff;width:420px;padding:40px 32px;border-radius:16px;box-shadow:0 8px 40px rgba(0,0,0,0.2)}
.login-title{text-align:center;font-size:24px;color:#152c5b;margin-bottom:30px;display:flex;align-items:center;justify-content:center;gap:10px}
.login-title i{color:#36d399;font-size:26px}
.input-item{margin-bottom:22px}
.input-item label{display:block;color:#475569;margin-bottom:8px;font-size:14px}
.input-wrap{position:relative}
.input-wrap i{position:absolute;left:14px;top:50%;transform:translateY(-50%);color:#94a3b8}
.input-wrap input{width:100%;padding:14px 14px 14px 42px;border:1px solid #e2e8f0;border-radius:8px;font-size:15px}
.input-wrap input:focus{outline:none;border-color:#152c5b}
.login-btn{width:100%;padding:14px;background:#152c5b;color:#fff;border:none;border-radius:8px;font-size:16px;cursor:pointer;margin-top:8px}
.login-btn:hover{background:#0f1e42}
.tip{text-align:center;margin-top:16px;font-size:14px}
.err{color:#dc2626}
</style>
</head>
<body>
<div class="login-box">
    <div class="login-title">
        <i class="fa fa-bell"></i>
        <span>企业运维告警监控平台</span>
    </div>
    <?php if($errMsg):?>
        <div class="tip err"><?php echo $errMsg;?></div>
    <?php endif;?>
    <form method="post">
        <div class="input-item">
            <label>登录账号</label>
            <div class="input-wrap">
                <i class="fa fa-user"></i>
                <input type="text" name="username" placeholder="请输入账号" autocomplete="off" required>
            </div>
        </div>
        <div class="input-item">
            <label>登录密码</label>
            <div class="input-wrap">
                <i class="fa fa-lock"></i>
                <input type="password" name="pwd" placeholder="请输入密码" autocomplete="off" required>
            </div>
        </div>
        <button class="login-btn" type="submit">登录进入平台</button>
    </form>
</div>
</body>
</html>
