<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once "config/db.php";

$error = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $username = trim($_POST['username']);
    $password = $_POST['password'];

    $stmt = $conn->prepare("SELECT id, username, password, role, kamar_id, penghuni_id FROM users WHERE username=? AND status='aktif' LIMIT 1");

    if (!$stmt) {
        die("Query error: " . $conn->error);
    }

    $stmt->bind_param("s", $username);
    $stmt->execute();
    $stmt->bind_result($id, $db_username, $db_password, $role, $kamar_id, $penghuni_id);

    if ($stmt->fetch()) {

        // cek password
        if ($password === $db_password) {

            $_SESSION['user_id'] = $id;
            $_SESSION['role'] = $role;
            $_SESSION['kamar_id'] = $kamar_id;
            $_SESSION['penghuni_id'] = $penghuni_id;

            // redirect sesuai role
            if ($role === 'admin' || $role === 'owner') {
                header("Location: admin/dashboard.php");
            } else {
                header("Location: penghuni/dashboard.php");
            }

            exit;

        } else {
            $error = "Username atau Password salah";
        }

    } else {
        $error = "Username atau Password salah";
    }

    $stmt->close();
}
?>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LuminaCare Login</title>

<style>

body {
    margin: 0;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #2563EB, #1E3A8A);
    font-family: Arial, sans-serif;
}

.login-box {
    background: white;
    padding: 40px;
    border-radius: 10px;
    width: 320px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.2);
    text-align: center;
}

.login-box h2 {
    margin-bottom: 20px;
    color: #2563EB;
}

.login-box input {
    width: 100%;
    padding: 10px;
    margin-bottom: 15px;
    border: 1px solid #ccc;
    border-radius: 5px;
}

.login-box button {
    width: 100%;
    padding: 10px;
    background: #2563EB;
    border: none;
    color: white;
    font-weight: bold;
    border-radius: 5px;
    cursor: pointer;
}

.login-box button:hover {
    background: #1E40AF;
}

.error {
    color: red;
    margin-bottom: 10px;
}

.brand {
    font-size: 22px;
    font-weight: bold;
    margin-bottom: 5px;
}

.subtitle {
    font-size: 13px;
    color: gray;
    margin-bottom: 20px;
}

</style>

</head>
<body>

<div class="login-box">

<div class="brand">LuminaCare</div>
<div class="subtitle">Sistem Maintenance Kost</div>

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

<form method="POST">

<input type="text" name="username" placeholder="Username" required>

<input type="password" name="password" placeholder="Password" required>

<button type="submit">Login</button>

</form>

</div>

</body>
</html>