<?php
require __DIR__ . '/../api/monitoring/koneksi.php';
session_start();

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email']);
    $pass  = $_POST['password'];

    $stmt = $conn->prepare("SELECT * FROM users WHERE email=? LIMIT 1");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $user = $stmt->get_result()->fetch_assoc();

    if ($user && password_verify($pass, $user['password'])) {
        $_SESSION['user'] = [
            'id'    => $user['id'],
            'name'  => $user['name'],
            'email' => $user['email'],
            'role'  => $user['role']
        ];
        header("Location: /monitoring/dashboard/");
        exit;
    } else {
        $error = "Email atau password salah";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Monitoring</title>
<style>
/* Reset sederhana */
* { margin:0; padding:0; box-sizing:border-box; font-family: 'Segoe UI', sans-serif; }

body {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #2b7cff, #4ecdc4);
}

.login-container {
    background: #fff;
    padding: 40px 30px;
    border-radius: 12px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.2);
    width: 100%;
    max-width: 400px;
}

.login-container h2 {
    text-align: center;
    margin-bottom: 25px;
    color: #333;
}

.login-container .error {
    background: #ffe6e6;
    color: #d8000c;
    padding: 10px 12px;
    border-radius: 6px;
    margin-bottom: 15px;
    text-align: center;
}

.login-container form input[type=email],
.login-container form input[type=password] {
    width: 100%;
    padding: 12px 10px;
    margin: 10px 0;
    border-radius: 8px;
    border: 1px solid #ccc;
    font-size: 14px;
}

.login-container form button {
    width: 100%;
    padding: 12px;
    margin-top: 15px;
    border: none;
    border-radius: 8px;
    background: #2b7cff;
    color: #fff;
    font-size: 16px;
    cursor: pointer;
    transition: 0.3s;
}

.login-container form button:hover {
    background: #1a5fcc;
}

.login-container form .footer {
    text-align: center;
    margin-top: 15px;
    font-size: 13px;
    color: #555;
}
</style>
</head>
<body>

<div class="login-container">
    <h2>Login Monitoring</h2>

    <?php if ($error): ?>
        <div class="error"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <form method="post">
        <input type="email" name="email" placeholder="Email" required>
        <input type="password" name="password" placeholder="Password" required>
        <button type="submit">Login</button>
        <div class="footer">© <?= date('Y') ?> Monitoring System</div>
    </form>
</div>

</body>
</html>
