<?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];
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>Advantest 爱德万测试 - 运维监控登录</title>
<link rel="stylesheet" href="/static/css/font-awesome.min.css">
<script src="/static/js/jquery.min.js"></script>
<style>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    font-family: "Inter", "PingFang SC", "Microsoft YaHei", sans-serif;
    position: relative;
    /* 官方品牌大图全屏背景 */
    background: url("https://www.advantest.com/img/common/img-ogp.png") center center / cover no-repeat fixed;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* 高透纯白遮罩，保留完整背景图，高级柔和不压抑 */
body::before {
    content: "";
    position: absolute;
    inset: 0;
    background: rgba(255, 255, 255, 0.86);
    z-index: 1;
}

/* 登录主卡片 高端质感 */
.login-card {
    position: relative;
    z-index: 2;
    width: 480px;
    background: #ffffff;
    padding: 60px 48px;
    border-radius: 24px;
    box-shadow: 0 12px 60px rgba(21, 44, 91, 0.08);
    border: 1px solid rgba(21, 44, 91, 0.06);
    backdrop-filter: blur(6px);
}

/* 品牌标题区域 */
.brand-box {
    text-align: center;
    margin-bottom: 48px;
}
.brand-en {
    font-size: 36px;
    font-weight: 700;
    letter-spacing: 4px;
    color: #152c5b;
    margin-bottom: 8px;
}
.brand-cn {
    font-size: 16px;
    color: #64748b;
    letter-spacing: 1px;
}

/* 错误提示 */
.error-tip {
    text-align: center;
    padding: 10px;
    background: #fef2f2;
    color: #dc2626;
    border-radius: 10px;
    font-size: 14px;
    margin-bottom: 24px;
}

/* 输入项通用 */
.form-item {
    margin-bottom: 26px;
}
.form-label {
    display: block;
    font-size: 14px;
    color: #334155;
    margin-bottom: 10px;
    font-weight: 500;
}
.input-wrap {
    position: relative;
}
.input-icon {
    position: absolute;
    left: 18px;
    top: 50%;
    transform: translateY(-50%);
    color: #94a3b8;
    font-size: 16px;
}
.form-input {
    width: 100%;
    padding: 18px 18px 18px 52px;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    font-size: 15px;
    background: #f9fafc;
    transition: all 0.25s ease;
}
.form-input:focus {
    outline: none;
    border-color: #152c5b;
    background: #ffffff;
    box-shadow: 0 0 0 3px rgba(21, 44, 91, 0.08);
}

/* 登录按钮 */
.login-submit {
    width: 100%;
    padding: 18px;
    margin-top: 10px;
    background: #152c5b;
    color: #ffffff;
    border: none;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.25s ease;
}
.login-submit:hover {
    background: #0f1e42;
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(21, 44, 91, 0.16);
}

/* 底部地区文字 */
.region-footer {
    position: absolute;
    z-index: 2;
    right: 48px;
    bottom: 36px;
    font-size: 14px;
    color: #152c5b;
    font-weight: 500;
    letter-spacing: 1px;
}

/* 移动端自适应 */
@media screen and (max-width: 540px) {
    .login-card {
        width: 92%;
        padding: 48px 32px;
    }
    .brand-en {
        font-size: 30px;
    }
    .region-footer {
        right: 24px;
        bottom: 20px;
        font-size: 13px;
    }
}
</style>
</head>
<body>
<div class="login-card">
    <div class="brand-box">
        <div class="brand-en">Advantest</div>
        <div class="brand-cn">爱德万测试 · 运维告警监控平台</div>
    </div>

    <?php if (!empty($errMsg)): ?>
        <div class="error-tip"><?php echo $errMsg; ?></div>
    <?php endif; ?>

    <form method="POST">
        <div class="form-item">
            <label class="form-label">登录账号</label>
            <div class="input-wrap">
                <i class="fa fa-user input-icon"></i>
                <input class="form-input" type="text" name="username" placeholder="请输入登录账号" autocomplete="off" required>
            </div>
        </div>
        <div class="form-item">
            <label class="form-label">登录密码</label>
            <div class="input-wrap">
                <i class="fa fa-lock input-icon"></i>
                <input class="form-input" type="password" name="pwd" placeholder="请输入登录密码" autocomplete="off" required>
            </div>
        </div>
        <button class="login-submit" type="submit">登录进入系统</button>
    </form>
</div>

<div class="region-footer">中国上海</div>
</body>
</html>
