<?php
require_once "../config/db.php";
require_once "../inc/auth_penghuni.php";

$success = '';
$error = '';

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

    $penghuni_id = $_SESSION['penghuni_id'];
    $password_lama = $_POST['password_lama'];
    $password_baru = $_POST['password_baru'];
    $konfirmasi    = $_POST['konfirmasi_password'];

    if($password_baru !== $konfirmasi){
        $error = "Konfirmasi password tidak cocok.";
    } else {

        $stmt = $conn->prepare("SELECT password FROM penghuni WHERE id=?");
        $stmt->bind_param("i", $penghuni_id);
        $stmt->execute();
        $result = $stmt->get_result();
        $data = $result->fetch_assoc();

        if(!password_verify($password_lama, $data['password'])){
            $error = "Password lama salah.";
        } else {

            $hash = password_hash($password_baru, PASSWORD_DEFAULT);

            $update = $conn->prepare("UPDATE penghuni SET password=? WHERE id=?");
            $update->bind_param("si", $hash, $penghuni_id);
            $update->execute();

            $success = "Password berhasil diperbarui.";
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../assets/style.css">
    <title>Ganti Password - LuminaCare</title>
    <style>
        body{
            margin:0;
            font-family:Arial;
            background:#f4f6f9;
        }

        .header{
            background:#1e3a8a;
            color:white;
            padding:20px;
            display:flex;
            justify-content:space-between;
        }

        .nav a{
            color:white;
            text-decoration:none;
            margin-left:20px;
            font-size:14px;
        }

        .container{
            max-width:450px;
            margin:50px auto;
            background:white;
            padding:30px;
            border-radius:10px;
            box-shadow:0 5px 20px rgba(0,0,0,0.1);
        }

        h2{
            margin-bottom:20px;
        }

        label{
            font-size:14px;
            font-weight:bold;
        }

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

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

        button:hover{
            background:#1e40af;
        }

        .alert-success{
            background:#16a34a;
            color:white;
            padding:10px;
            border-radius:6px;
            margin-bottom:15px;
        }

        .alert-error{
            background:#dc2626;
            color:white;
            padding:10px;
            border-radius:6px;
            margin-bottom:15px;
        }

        .back{
            display:block;
            margin-top:15px;
            text-align:center;
            text-decoration:none;
            color:#1e3a8a;
        }

        .toggle{
            font-size:12px;
            cursor:pointer;
            color:#2563eb;
            margin-top:-10px;
            margin-bottom:15px;
            display:inline-block;
        }
    </style>
</head>
<body>

<?php include "../inc/navbar_penghuni.php"; ?>


<div class="container">

    <h2>Ganti Password</h2>

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

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

    <form method="POST">

        <label>Password Lama</label>
        <input type="password" name="password_lama" id="pass1" required>

        <label>Password Baru</label>
        <input type="password" name="password_baru" id="pass2" required>

        <label>Konfirmasi Password Baru</label>
        <input type="password" name="konfirmasi_password" id="pass3" required>

        <button type="submit">Update Password</button>

    </form>

    <a href="dashboard.php" class="back">← Kembali</a>

</div>
<?php include "../inc/footer.php"; ?>

</body>
</html>