<?php
include("./include/config.php");

if (!isset($_SESSION['a_user'])) {
    header("Location: index.php");
} else {
    include("./include/sidebar.php");
?>

<div class="col-xs-12">
    <!--<a href="dashboard.php" class="margin-bottom-10 btn waves-effect waves-light btn-primary btn-xs">Home</a>-->
    <!--<a href="data_view.php" class="margin-bottom-10 btn waves-effect waves-light btn-primary btn-xs">View Users</a>-->
    <!--<a href="update_label.php" class="margin-bottom-10 btn waves-effect waves-light btn-primary btn-xs">Update Label</a>-->
    <div class="box-content">
        <h4 class="box-title">Change Password</h4>
        <div class="row">
            <div class="col-md-12">

<?php
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve the submitted password
    $newPassword = $_POST['new_password'];

    // Prepare the SQL statement to update the password
    $sql = "UPDATE admin_login SET al_pwd = ? WHERE al_id = 1";
    $stmt = $conn->prepare($sql);
    $hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
    $stmt->bind_param("s", $newPassword);

    // Execute the statement
    if ($stmt->execute()) {
        echo '<div class="alert alert-success">Password updated successfully!</div>';
    } else {
        echo '<div class="alert alert-danger">Failed to update the password.</div>';
    }

    // Close the statement
    $stmt->close();
}

// Close the database connection
$conn->close();
?>

<form method="POST">
    <div class="form-group">
        <label for="new_password">New Password</label>
        <input type="password" class="form-control" id="new_password" name="new_password">
    </div>
    <button type="submit" class="btn btn-primary">Change Password</button>
</form>

            </div>
        </div>
    </div>
</div>

<?php
}

include("./include/footer.php");
?>
