<?php
include 'header.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Initialize variables
$msg = "";
$err = "";

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $current_password = $_POST['current_password'] ?? '';
    $new_password = $_POST['password'] ?? '';
    $password_confirmation = $_POST['password_confirmation'] ?? '';

    // Validate CSRF token
    if (!isset($_POST['_token']) || $_POST['_token'] !== 'KM5EnItCap3TL8nux8waUAAOvZgA0DvbjTGJOMsF') {
        $err = "Invalid CSRF token";
    }

    // Validate inputs
    if (empty($err) && empty($current_password)) {
        $err = "Current password is required";
    }
    if (empty($err) && empty($new_password)) {
        $err = "New password is required";
    }
    if (empty($err) && strlen($new_password) < 6) {
        $err = "New password must be at least 6 characters long";
    }
    if (empty($err) && $new_password !== $password_confirmation) {
        $err = "New password and confirmation do not match";
    }

    // Verify current password
    if (empty($err)) {
        $query = "SELECT password FROM users WHERE id = $user_id";
        $result = mysqli_query($link, $query);
        if ($result && mysqli_num_rows($result) > 0) {
            $user = mysqli_fetch_assoc($result);
            if ($new_password == $user['password']) {
                $err = "Current password is incorrect";
            }
        } else {
            $err = "User not found";
        }
    }

    // Update password
    if (empty($err)) {
        $query = "UPDATE users SET password = '$new_password' WHERE id = $user_id";
        if (mysqli_query($link, $query)) {
            $msg = "Password updated successfully";

        } else {
            $err = "Failed to update password: " . mysqli_error($link);
        }
    }

    // Output messages
    if ($err) {
        echo "<div class='alert alert-danger'>$err</div>";
    } elseif ($msg) {
        echo "<div class='alert alert-success'>$msg</div>";
    }
}
?>

    <main class="flex-grow-1" style="padding-top: 80px;">
        
        
        <div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Change Password</div>
                
                  <?php if ($msg) { ?>
                    <div class='alert alert-success'><?php echo $msg; ?></div>
                <?php } ?>
                <?php if ($err) { ?>
                    <div class='alert alert-danger'><?php echo $err; ?></div>
                <?php } ?>

                <div class="card-body">
                    <form method="POST" action="">
                        <input type="hidden" name="_token" value="KM5EnItCap3TL8nux8waUAAOvZgA0DvbjTGJOMsF" autocomplete="off">                        <input type="hidden" name="_method" value="PUT">
                        <div class="row mb-3">
                            <label for="current_password" class="col-md-4 col-form-label text-md-end">Current Password</label>
                            <div class="col-md-6">
                                <input id="current_password" type="password" class="form-control " name="current_password" required autocomplete="current-password">
                                                            </div>
                        </div>

                        <div class="row mb-3">
                            <label for="password" class="col-md-4 col-form-label text-md-end">New Password</label>
                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control " name="password" required autocomplete="new-password">
                                                                <small class="form-text text-muted">
                                    Your password must be at least 6 characters long.
                                </small>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <label for="password-confirm" class="col-md-4 col-form-label text-md-end">Confirm New Password</label>
                            <div class="col-md-6">
                                <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
                            </div>
                        </div>

                        <div class="row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    Update Password
                                </button>
                                <a href="profile.php" class="btn btn-link">
                                    Cancel
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
    </main>

 <?php
include 'footer.php';

?>