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

require_once __DIR__ . "/config/db.php";

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

$error = "";

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

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

    $stmt = $conn->prepare("
        SELECT penghuni.*, kamar.nomor_kamar 
        FROM penghuni
        JOIN kamar ON penghuni.kamar_id = kamar.id
        WHERE kamar.nomor_kamar=? 
        AND penghuni.status='aktif'
        LIMIT 1
    ");

    $stmt->bind_param("s", $kamar);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {

        $user = $result->fetch_assoc();

        if (!empty($user['password']) && password_verify($password, $user['password'])) {

            $_SESSION['penghuni_id'] = $user['id'];
            $_SESSION['nama'] = $user['nama'];
            $_SESSION['kamar'] = $user['nomor_kamar'];

            header("Location: penghuni/dashboard.php");
            exit;

        } else {
            $error = "Password salah!";
        }

    } else {
        $error = "Kamar tidak ditemukan atau tidak aktif!";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../assets/style.css">
    <title>Login Penghuni - LuminaCare</title>
    <style>
        body {
            margin:0;
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg,#1e3a8a,#2563eb);
            height:100vh;
            display:flex;
            justify-content:center;
            align-items:center;
        }

        .card {
            background: rgba(255,255,255,0.15);
            backdrop-filter: blur(10px);
            padding:40px;
            border-radius:16px;
            width:360px;
            text-align:center;
            box-shadow:0 15px 40px rgba(0,0,0,0.2);
            color:white;
        }

        .card h2 {
            margin-bottom:5px;
        }

        .card p {
            font-size:14px;
            opacity:0.9;
            margin-bottom:25px;
        }

        input {
            width:100%;
            padding:12px;
            margin-bottom:15px;
            border:none;
            border-radius:8px;
            outline:none;
            font-size:14px;
        }

        input::placeholder {
            color:#555;
        }

        button {
            width:100%;
            padding:12px;
            border:none;
            border-radius:8px;
            background:#16a34a;
            color:white;
            font-weight:bold;
            cursor:pointer;
            font-size:14px;
            transition:0.3s;
        }

        button:hover {
            background:#15803d;
            transform:translateY(-2px);
        }

        .error {
            background:#dc2626;
            padding:10px;
            border-radius:8px;
            margin-bottom:15px;
            font-size:13px;
        }

        .back {
            display:block;
            margin-top:20px;
            font-size:13px;
            text-decoration:none;
            color:white;
            opacity:0.8;
        }

        .back:hover {
            opacity:1;
        }
    </style>
</head>
<body>

<div class="card">
    <h2>LuminaCare</h2>
    <p>Login Penghuni</p>

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

    <form method="POST">
        <input type="text" name="kamar" placeholder="Nomor Kamar" required>
        <input type="password" name="password" placeholder="Password" required>
        <button type="submit">Login</button>
    </form>

    <a href="https://laporan.my.id" class="back">← Kembali ke Home</a>
</div>

</body>
</html>