<?php
session_start();
$activePage = 'User'; 

include("../master.php");
include("../elements/functions.php");
if (isset($_SESSION['token'])) {
    $token = $_SESSION['token'];
   $username= $_SESSION['username'];
   $_SESSION['password'];

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        
        //left -API fields -Right :input fields//
        $data = [
            "password" =>  $_POST['password'],
            "password_two" =>  $_POST['password_two']
        ];
       if( $_SESSION['password'] != $data['password'] && $data['password']===$data['password_two']){


//-------------------API CALL--------------------------------//
        $jsonFilePath = __DIR__ . '/../urls/api_endpoints.json';
        $jsonData = file_get_contents($jsonFilePath);
        $endpoints = json_decode($jsonData, true);
        $url = $endpoints['change_password'] . $username . "/password?";
//-----------------ENDS HERE-------------------------------------//

        $response =  make_patch_request($url, $data);
        $_SESSION['password'] = $data['password'];
       }
      

        if ($response === FALSE) {
            $response = json_encode(['status' => 'error', 'message' => 'An error occurred']);
        }

        $response_data = json_decode($response, true);

        if ($response_data['status'] === 200) {
            //echo '<script>window.location.href = "../user/userlist.php";</script>';
            $_SESSION['message']="password changed sucessfully";
            $redirect_page="../elements/index.php";

        } elseif ($response_data['status'] === 401) {
            $_SESSION['message'] = 'password not changed';
            $redirect_page="../user/user_edit.php";
        }
        else{
            $_SESSION['message'] = "password not changed";
            $redirect_page="../elements/master.php";
        }
        } else {
        header('Location: ../index.php');
        exit();
        }
    }
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create User</title>
    <link rel="stylesheet" href="../css/userstyle.css" type="text/css">
</head>
<body>
    <div id="popup" class="popup">
        <div class="popup-content">
            <p id="popup-message"></p>
        </div>
    </div>
    
    <script>
        <?php if (isset($_SESSION['message'])): ?>
            var popup = document.getElementById("popup");
            var popupMessage = document.getElementById("popup-message");
            popupMessage.textContent = "<?php echo $_SESSION['message']; ?>";
            popup.style.display = "block";
            setTimeout(function() {
                popup.style.display = "none";
                window.location.href = "<?php echo $redirect_page; ?>";// Redirect after 3 seconds
            }, 2000); // Automatically close the popup after 3 seconds
            
           
            <?php unset($_SESSION['message']); // Clear the message after displaying ?>
           
        <?php endif; ?>
    </script>
</body>
</html>
