<?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";
// 权限用户库 monitor（存放sys_user、sys_role、sys_role_permission）
$dbMonitorName = "monitor";
// 业务库 alert_mail_stat（工单、SMTP配置、大盘数据）
$dbAlertName = "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 {
        // 连接monitor权限库查询用户
        $connMonitor = mysqli_connect($dbHost, $dbUser, $dbPass, $dbMonitorName);
        if (!$connMonitor) {
            $errMsg = "数据库连接失败：" . mysqli_connect_error();
        } else {
            mysqli_set_charset($connMonitor, "utf8mb4");
            $u = mysqli_real_escape_string($connMonitor, $username);
            // 适配真实表字段，新增is_admin查询
            $sql = "SELECT id,username,password,role_id,is_admin,enable FROM sys_user WHERE username='$u' LIMIT 1";
            $res = mysqli_query($connMonitor, $sql);
            // 新增判断：查询成功才读取数据
            if ($res) {
                $user = mysqli_fetch_assoc($res);
                // 先判断账号是否启用
                if (!$user) {
                    $_SESSION[$lockKey]['num']++;
                    $errMsg = "账号或密码输入错误";
                } elseif ($user['enable'] != 1) {
                    $errMsg = "该账号已被禁用，无法登录";
                } elseif (password_verify($pwd, $user['password'])) {
                    // 登录成功逻辑
                    $_SESSION[$lockKey] = ['num' => 0, 'time' => time()];
                    $_SESSION['user_id'] = $user['id'];
                    $_SESSION['username'] = $user['username'];
                    $_SESSION['role_id'] = $user['role_id'];
                    $_SESSION['is_admin'] = $user['is_admin'];
                    $_SESSION['login_time'] = time();
                    mysqli_query($connMonitor, "UPDATE sys_user SET last_login=NOW() WHERE id=" . $user['id']);
                    mysqli_close($connMonitor);
                    header("Location: dashboard.php");
                    exit;
                } else {
                    $_SESSION[$lockKey]['num']++;
                    $errMsg = "账号或密码输入错误";
                }
            } else {
                // 捕获SQL执行失败原因
                $errMsg = "查询用户失败：" . mysqli_error($connMonitor);
            }
            mysqli_close($connMonitor);
        }
    }
}
?>
<!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>
