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

include("../elements/master.php");

if (isset($_SESSION['token'])) {
    $token = $_SESSION['token'];

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      
        $headers = [
            "Authorization: Bearer $token",
            "Content-Type: application/json",
            "Accept: application/json"
        ];

        //left -API fields -Right :input fields//
        $data = [
           "pid" => $_POST['pid'],
            "password" => $_POST['password'],
            "access_duration" => "default_value", // Replace with actual value or fetch from a form
            "access_level" => "default_value", // Replace with actual value or fetch from a form
            "category" => 0, // Replace with actual value or fetch from a form
            "expiration" => "1970-01-01 00:00:00", // Replace with actual value or fetch from a form
            "login_remaining" => 0, // Replace with actual value or fetch from a form
            "sponsor" => 0, // Replace with actual value or fetch from a form
            "unregdate" => "1970-01-01 00:00:00", // Replace with actual value or fetch from a form
            "valid_from" => "1970-01-01 00:00:00" // Replace with actual value or fetch from a form
           
        ];
        $pid=$data['pid'];

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

        $options = [
            'http' => [
                'header'  => implode("\r\n", $headers),
                'method'  => 'POST',
                'content' => json_encode($data),
                'ignore_errors' => true
            ],
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false
            ]
        ];

        $context  = stream_context_create($options);
        $response = file_get_contents($url, false, $context);
        if ($response === FALSE) {
            $response = json_encode(['status' => 'error', 'message' => 'An error occurred']);
        }

        $response_data = json_decode($response, true);

        if ($response_data['status'] === 201) {
            //echo '<script>window.location.href = "../user/userlist.php";</script>';
            $_SESSION['message']="User Created Successfully";
            $redirect_page="../user/userlist.php";

        } elseif ($response_data['status'] === 409) {
            $_SESSION['message'] = 'User not created . User already exists';
            $redirect_page="../user/createuser.php";
        }
        else{
            $_SESSION['message'] = 'User not created';
            $redirect_page="../user/createuser.php";
        }
        } else {
        header('Location: /admin/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="/admin/css/userstyle.css" type="text/css">
</head>
<body>
    <div id="popup" class="popup">
        <div class="popup-user-create-meg">
            <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>
