<?php
session_start();
include('userlistfunctions.php');
include('user_delete.php');
if (!isset($_SESSION['token'])) {
    include('token_exp.php');
}
// Fetch user data from the API
$jsonFilePath = __DIR__ . '/../urls/api_endpoints.json';
        $jsonData = file_get_contents($jsonFilePath);
        $endpoints = json_decode($jsonData, true);

if (isset($endpoints['search'])) {
$apiUrl = $endpoints['search'];

$token = $_SESSION['token'];
$result = make_get_request($apiUrl, ["Authorization: $token"]);
$user_list = json_decode($result, true);
echo $user_list;
$searchQuery = '';
$matchingUsers = [];

// Check if a search query is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['search'])) {
    $searchQuery = trim($_POST['search']);
    if (is_array($user_list)) {
        foreach ($user_list as $user) {
            if (stripos($user['pid'], $searchQuery) !== false ||
                stripos($user['firstname'], $searchQuery) !== false ||
                stripos($user['lastname'], $searchQuery) !== false ||
                stripos($user['email'], $searchQuery) !== false ||
                stripos($user['custom_field_1'], $searchQuery) !== false ||
                stripos($user['custom_field_2'], $searchQuery) !== false ||
                stripos($user['cell_phone'], $searchQuery) !== false ||
                stripos($user['company'], $searchQuery) !== false) {
                $matchingUsers['pid'] = $user['pid'];
            }
        }
    }
} 
}else {
    $matchingUsers = $user_list; // Show all users if no search query
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User List</title>
    <link rel="stylesheet" href="/admin/css/style.css" type="text/css">
    <link rel="stylesheet" href="/admin/css/userstyle.css" type="text/css">
    <link rel="stylesheet" href="/admin/css/popup.css">
    <script src="../js/userlist.js"></script>
</head>
<body>
<div id="div_userlist">
<div class="user_container">

  <!---main start-->
  <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>Role</th>
                <th>Phone No</th>
                <th>Modify</th>
                <th>Delete</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($matchingUsers 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['custom_field_1']); ?></td>
                    <td><?php echo htmlspecialchars($user['custom_field_2']); ?></td>
                    <td><?php echo htmlspecialchars($user['cell_phone']); ?></td>
                    <td> <button class="btn_modify" data-pid="<?php echo htmlspecialchars($user['pid']); ?>">Modify</button></td>
                   <td> <button class="btn_delete" data-pid="<?php echo htmlspecialchars($user['pid']); ?>">Delete</button></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <form id="pid_form" action="../user/user_edit.php" method="post" style="display: none;">
            <input type="hidden" name="pid" id="pid_input">
        </form>
    </div>
    </div>
    <!-- Pagination -->

  </maincontent>

  <!---main end-->

</div>
</div>

<!-- Popup -->
<div id="popup" class="popup">
  <div class="popup-content">
   <!-- <span class="close">&times;</span>-->
    <p id="popup-message"></p>
    <button id="confirm-button" class="confirm-button" >Yes</button>
    <button id="cancel-button" class="confirm-button">No</button>
  </div>
</div>

</body>
</html>
