<?php
session_start();

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

/* ==========================
   CEK LOGIN ADMIN
========================== */
if (!isset($_SESSION['admin_id'])) {
    header("Location: ../login.php");
    exit;
}

/* ==========================
   PROSES GANTI PASSWORD
========================== */
$message="";

if(isset($_POST['ubah'])){

$id=$_SESSION['admin_id'];
$lama=$_POST['password_lama'];
$baru=$_POST['password_baru'];

$data=$conn->query("SELECT password FROM admin WHERE id='$id'")->fetch_assoc();

if(password_verify($lama,$data['password'])){

$hash=password_hash($baru,PASSWORD_DEFAULT);

$conn->query("UPDATE admin SET password='$hash' WHERE id='$id'");

$message="Password berhasil diubah";

}else{

$message="Password lama salah";

}

}
?>

<!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, sans-serif;
background:#f4f6f9;
}

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

.header-title{
font-size:20px;
font-weight:bold;
}

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

.nav a:hover{
text-decoration:underline;
}

.container{
padding:30px;
}

.section{
background:white;
padding:20px;
border-radius:8px;
margin-bottom:20px;
box-shadow:0 4px 10px rgba(0,0,0,0.05);
}

input{
padding:10px;
margin-top:5px;
width:250px;
border:1px solid #ccc;
border-radius:5px;
}

button{
padding:10px 15px;
background:#1e40af;
color:white;
border:none;
border-radius:5px;
cursor:pointer;
margin-top:10px;
}

button:hover{
background:#1e3a8a;
}

.success{
color:green;
margin-top:10px;
}

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

</style>
</head>

<body>

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

<div class="container">

<div class="section">

<h3>Ganti Password</h3>

<?php if($message=="Password berhasil diubah"){ ?>
<p class="success"><?= $message ?></p>
<?php } elseif($message!=""){ ?>
<p class="error"><?= $message ?></p>
<?php } ?>

<form method="POST">

<p>Password Lama</p>
<input type="password" name="password_lama" required>

<p>Password Baru</p>
<input type="password" name="password_baru" required>

<br>

<button name="ubah">Ubah Password</button>

</form>

</div>

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

</body>
</html>
