<?php
include('includes/db.php');

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Assuming $userId is the ID of the user you want to update
    $userId = $_POST["user_id"]; // Replace with the actual form field name

    // Generate a new password (you can use your own method to generate a new password)
    $newPassword = password_hash($_POST["new_password"], PASSWORD_BCRYPT); // Replace with the actual form field name

    // Update the password and set the updationDate to the current timestamp
    $query = "UPDATE `admin` SET `Password`='$newPassword', `updationDate`=NOW() WHERE `id`='$userId'";
    $result = $conn->query($query);

    if ($result) {
          // Include session validation
                include('session_validator.php');

                // Redirect to the dashboard
                header("Location: adminlogin.php");
        echo "Password updated successfully.";
    } else {
        echo "Error updating password: " . $conn->error;
    }
}

// Close the database connection (set to null)
$conn = null;
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/loginstyle.css">
        <link rel="stylesheet" href="css/bootstrap.min.css" media="screen">
    <link rel="stylesheet" href="css/font-awesome.min.css" media="screen">
    <link rel="stylesheet" href="css/animate-css/animate.min.css" media="screen">
    <link rel="stylesheet" href="css/lobipanel/lobipanel.min.css" media="screen">
    <link rel="stylesheet" href="css/toastr/toastr.min.css" media="screen">
    <link rel="stylesheet" href="css/icheck/skins/line/blue.css">
    <link rel="stylesheet" href="css/icheck/skins/line/red.css">
    <link rel="stylesheet" href="css/icheck/skins/line/green.css">

    <title>Update Password</title>
    <!-- Include your CSS stylesheets here -->
</head>
<body>
    <div class="login-page">
      <div class="form">
        <div class="login">
          <div class="login-header">
            <h3>Reset Password</h3>
           
            <p>Please enter your credentials and New Password.</p>
          </div>
        </div>
        <form method="POST" action="updatepassword.php">
            <div class="form-group">
            <label for="user_id">User ID:</label>
            <select  type="text" name="user_id" id="user_id" class="form-control" required>
                <option>Select User</option>
            <?php
        // Include database connection code
        include('includes/db.php');

        // Fetch users from the database
        $query = "SELECT id, UserName FROM admin";
        $result = $conn->query($query);

        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $userId = $row['id'];
                $username = $row['UserName'];
                echo "<option value='$userId'>$username</option>";
            }
        } else {
            echo "<option value='' disabled>No users found</option>";
        }

        // Close the database connection
        $conn->close();
        ?>
    </select>
<br>
            <label for="new_password">New Password:</label>
            <input type="password" name="new_password"  class="form-control" required>
            <br>
                    <button type="submit">Update Password</button>

             <p class="message">Go back to login? <a href="adminlogin.php">Log in</a></p>

    
             </div>
        </form>
      </div>
    </div>
</body>


</html>
