<?php
session_start();
include "includes/layout.php";
include __DIR__ . '/../database/search_log_config.php';

$id = $_SESSION['admin_id'];

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

    $current = $_POST['current_password'];
    $new = $_POST['new_password'];

    $admin = $log_conn->query("
        SELECT admin_password FROM admin_users WHERE id=$id
    ")->fetch_assoc();

    if(password_verify($current, $admin['admin_password'])){

        $newHash = password_hash($new, PASSWORD_DEFAULT);

        $stmt = $log_conn->prepare("
            UPDATE admin_users SET admin_password=? WHERE id=?
        ");
        $stmt->bind_param("si", $newHash, $id);
        $stmt->execute();
        $stmt->close();

        $success = "Password changed successfully!";
    } else {
        $error = "Current password incorrect!";
    }
}
?>

<h1>Change Password</h1>

<div class="card">

<?php
if(isset($error)) echo "<p style='color:red;'>$error</p>";
if(isset($success)) echo "<p style='color:green;'>$success</p>";
?>

<form method="POST">
<input type="password" name="current_password"
placeholder="Current Password"
style="width:100%;padding:10px;margin-bottom:10px;">

<input type="password" name="new_password"
placeholder="New Password"
style="width:100%;padding:10px;margin-bottom:10px;">

<button class="btn">Change Password</button>
</form>
</div>

<?php
$log_conn->close();
include "includes/footer.php";
?>