<?php
session_start();
include('userlistfunctions.php');
include('user_delete.php');

?>

<!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="../css/style.css" type="text/css">
    <link rel="stylesheet" href="../css/userstyle.css" type="text/css">
</head>
<body>
<?php if(isset($_SESSION['messages'])) ?>
<div id="popup_bulk" class="popup_bulk">
        <div class="popup-user-create-meg">
            <p id="popup-message_bulk"><?php $SESSION['messages']?></p>
        </div>
</div>


<div id="div_userlist">
<div class="user_container">

  <!-- Main content -->
  <maincontent>
    <div class="usertable">
        <h3>User List</h3>
        <?php echo $_SESSION['valid'];?>

            <!-- Search form -->
            <div>
                <form action="userlist.php" method="GET" class="search">
                    <input type="text" name="search" id="search" placeholder="search here" />
                    <button type="submit" name="submit" class="btn_search" id="search_btn"><img src="../images/search.png" alt="search" class="search_logo"></button>
                    <button type="button" class="clear_btn" id="clear_btn">clear</button>
                </form>
            </div>
            <div class="userdetails">

            <?php
                $search_results = [];
                $search = '';  // Initialize search as an empty string

                // Handle search submission
                if (isset($_GET['submit']) || isset($_GET['search'])) {  // Check for either submit or search query
                    $search = $_GET['search'];  // Get the search value
                    foreach ($user_list as $user) {
                        if (stripos($user['pid'], $search) !== false || stripos($user['lastname'], $search) !== false || stripos($user['firstname'], $search) !== false) {
                            $search_results[] = $user;
                        }
                    }
                }

                if (empty($search_results)) {
                    // Normal pagination for user list when there's no search
                    $limit = 10;
                    $page_no = isset($_GET['page']) ? (int)$_GET['page'] : 1;
                    $offset = ($page_no - 1) * $limit;
                
                    $totalUsers = count($user_list);
                    $totalPages = ceil($totalUsers / $limit);
                
                    $user_list = array_slice($user_list, $offset, $limit);
                } else {
                    // Pagination for search results
                    $limit = 10;
                    $total_search_users = count($search_results);
                    $total_search_pages = ceil($total_search_users / $limit);
                
                    $search_page_no = isset($_GET['search_page']) ? (int)$_GET['search_page'] : 1;
                    $offset = ($search_page_no - 1) * $limit;
                
                    $search_results = array_slice($search_results, $offset, $limit);
                }
                ?>

            
            <?php if (!empty($search_results)): ?>
                <!-- Search results table -->
                <table id="user_table">
                    <thead>
                        <tr>
                            <th>
                                <input type="checkbox" id="select_all"/>
                                <img id="bulk_delete_btn"  style="display: none;" src="../images/trash-can.png" alt="img">
                            </th>
                            <th data-column="pid" data-order="desc" class="sortable">PID</th>
                            <th data-column="firstname" data-order="desc" class="sortable">First Name</th>
                            <th data-column="lastname" data-order="desc" class="sortable">Last Name</th>
                            <th data-column="email" data-order="desc" class="sortable">Email</th>
                            <th data-column="Department" data-order="desc" class="sortable">Department</th>
                            <th data-column="Role" data-order="desc" class="sortable">Role</th>
                            <th>Modify</th>
                            <th>Delete</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($search_results as $user): ?>
                            <tr data-pid="<?php echo htmlspecialchars($user['pid']); ?>">
                                <td><input type="checkbox" class="user_checkbox" data-pid="<?php echo htmlspecialchars($user['pid']); ?>" /></td>
                                <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['Role']); ?></td>
                                <td><button class="btn_modify" id="btn1" data-pid="<?php echo htmlspecialchars($user['pid']); ?>">Modify</button></td>
                                <td><button class="btn_delete" id="btn2" data-pid="<?php echo htmlspecialchars($user['pid']); ?>">Delete</button></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>

            <?php else: ?>
                <!-- User list table -->
                <?php if (!empty($user_list)): ?>
                    <table id="user_table">
                        <thead>
                            <tr>
                                 <th class="check_box">
                                    <input type="checkbox" id="select_all" title="Delete selected users"/>
                                    <img id="bulk_delete_btn" style="display: none;" src="../images/trash-can.png" alt="img">
                                </th>
                                <th data-column="pid" data-order="asc" class="sortable"><img src="../images/arrow.png" alt="image" class="sort"> PID</th>
                                <th data-column="firstname" data-order="asc" class="sortable"><img src="../images/arrow.png" alt="image" class="sort"> First Name</th>
                                <th data-column="lastname" data-order="asc" class="sortable"><img src="../images/arrow.png" alt="image" class="sort"> Last Name</th>
                                <th data-column="email" data-order="asc" class="sortable"><img src="../images/arrow.png" alt="image" class="sort"> Email</th>
                                <th>Modify</th>
                                <th>Delete</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($user_list as $user): ?>
                                <tr data-pid="<?php echo htmlspecialchars($user['pid']); ?>">
                                    <td><input type="checkbox" class="user_checkbox" data-pid="<?php echo htmlspecialchars($user['pid']); ?>" /></td>
                                    <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><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>
                <?php endif; ?>
            <?php endif; ?>

            <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------------------------------------------>

      <?php if(empty($search_results)): ?>
                    <!-- Pagination for the main user list -->
                    <div class="pagination">
                        <?php if ($page_no > 1): ?>
                            <a href="?page=<?php echo $page_no - 1; ?>">&laquo; Previous</a>
                        <?php endif; ?>
                        
                        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                            <a href="?page=<?php echo $i; ?>" <?php if ($i == $page_no) echo 'class="active"'; ?>><?php echo $i; ?></a>
                        <?php endfor; ?>
                        
                        <?php if ($page_no < $totalPages): ?>
                            <a href="?page=<?php echo $page_no + 1; ?>">Next &raquo;</a>
                        <?php endif; ?>
                    </div>
                <?php else: ?>
                    <!-- Pagination for search results -->
                    <div class="pagination">
                        <?php if ($search_page_no > 1): ?>
                            <a href="?search=<?php echo urlencode($search); ?>&search_page=<?php echo $search_page_no - 1; ?>">&laquo; Previous</a>
                        <?php endif; ?>
                        
                        <?php for ($i = 1; $i <= $total_search_pages; $i++): ?>
                            <a href="?search=<?php echo urlencode($search); ?>&search_page=<?php echo $i; ?>" <?php if ($i == $search_page_no) echo 'class="active"'; ?>><?php echo $i; ?></a>
                        <?php endfor; ?>
                        
                        <?php if ($search_page_no < $total_search_pages): ?>
                            <a href="?search=<?php echo urlencode($search); ?>&search_page=<?php echo $search_page_no + 1; ?>">Next &raquo;</a>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>

<!----------------PAGINATION ENDS ---------------------------------->

                <!-- Debugging info -->
            <!--    <p>total pages: <?php // echo $total_search_pages ?></p> -->
             <!---   <p>total users: <?php // echo $total_search_users ?></p>
                <p>search result page: <?php // echo $search_page_no ?></p>
                <p>search query: <?php //echo htmlspecialchars($search) ?></p>      -->          
                                        

 <!------------------PAGINATION ENDS ------------------------------------>


  </maincontent>
  <!---main end-->
</div>
</div>

<!-- Popup -->
<div id="popup" class="popup">
  <div class="popup-content">
    <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>

<div id="confirmation_popup" style="display: none;">
    <div class="popup_cont">
        <p>Do you really wanna delete the selected users?</p>
        <button id="confirm_yes">Yes</button>
        <button id="confirm_no">No</button>
    </div>
</div>


<script src="../js/sorting_popup.js"></script>
<script src="../js/bulk_delete.js"></script>
</body>
</html>
