<?php
session_start();

if (!isset($_SESSION['token'])) {
    header('Location: index.php');
    exit();
}
$activePage = 'User';
$activeSubPage = 'Userlist';

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

//--------------------------------Api Clling-------------------------------//
$jsonFilePath = __DIR__ . '/../urls/api_endpoints.json';
$jsonData = file_get_contents($jsonFilePath);
$endpoints = json_decode($jsonData, true);
$url =  $endpoints['user_details'];


//------------------------------------ends here---------------------------//
$token = $_SESSION['token'];
$result = make_get_request($url, ["Authorization: $token"]);
$response = json_decode($result, true);
$_SESSION["response"] = $response;

include("../user/user_details_function.php")
  

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
    <link rel="stylesheet" href="../css/style.css" type="text/css">
    <link rel="stylesheet" href="../css/userstyle.css" type="text/css">
</head>
<div id="div_userlist" >
<div class="user_container">
   
  <!---main start-->
  <?php if (isset($user_details_fetch) && is_array($user_details_fetch)): ?>
    
  <maincontent>
    <div class="usertable">
        <h3>User List</h3>
        <div class="userdetails">
        <table id="user_table" >
            <thead>
                <tr>
                <th>PID</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>Department</th>
                <th>Designation</th>
                <th>Phone No</th>
                <th>Company</th>
                </tr>
            </thead>
            <tbody>

            <form action="userdetails.php">
            <?php foreach ($user_details_fetch as $user): ?>
                <tr data-pid="<?php echo htmlspecialchars($user['pid']); ?>">
                   <td><?php echo htmlspecialchars($user['pid']); ?></td>
                   <td><?php echo htmlspecialchars($user['firstname']); ?></td>
                    <td><?php echo htmlspecialchars($user['lastname']); ?></td>
                    <td><?php echo htmlspecialchars($user['email']); ?></td>
                    <td><?php echo htmlspecialchars($user['Department']); ?></td>
                    <td><?php echo htmlspecialchars($user['Designation']); ?></td>
                    <td><?php echo htmlspecialchars($user['work_phone']); ?></td>
                    <td><?php echo htmlspecialchars($user['Company']); ?></td>
                </tr>
            <?php endforeach; ?>
            
            </tbody>
        </table>
        <form id="pid_form" action="../user/userdetails.php" method="post" style="display: none;">
    <input type="hidden" name="pid" id="pid_input">
</form>
        <a href="../user/userlist.php" class="btn_back" >Back</a>
    </div>
    </div>
    <!-- Pagination -->

  </maincontent>
  
  <?php else: ?>
  <p><?php echo $user_list; ?></p>
  <?php endif; ?>
  <!---main end-->

</div>
</div>

</body>
</html>