<!-- views/inusers.ejs -->

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>In-House Users</title>
  <link rel="stylesheet" href="/style.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;700&display=swap">
</head>

<body>
  <header>
    <h1>ECOSAVER MANAGEMENT</h1>
</header>
  <nav>
    <a href="#" id="userDataLink">User Data</a>
    <a href="#" id="addUserLink">Add User</a>
    <a href="#" id="deleteUserLink">Delete User</a>
    <a href="#" id="resetPasswordLink">Reset Password</a>
    <a href="#" id="logsLink">Logs</a>
    <a href="/home" id="homeLink">Home</a>
  </nav>

    <main>
    <h2>Welcome to the In-House Users Page</h2>
   </main>
    <div id="downloadButtonContainer">
      <button id="downloadButton">Download as Excel</button>
    </div>


    <div id="userDataContainer">
      <table class="nice-table">
        <thead>
          <tr>
            <th>Name</th>
            <th>Username</th>
            <th>Email</th>
            <th>Job Title</th>
          </tr>
        </thead>
        <tbody>
          <% if (allUsers && allUsers.length > 0) { %>
            <% allUsers.forEach(user => { %>
              <tr>
                <td><%- user.name %></td>
                <td><%- user.username %></td>
                <td><%- user.email %></td>
                <td><%- user.jobTitle %></td>                
              </tr>
            <% }); %>
          <% } else { %>
            <tr>
              <td colspan="6">No users available.</td>
            </tr>
          <% } %>
        </tbody>
      </table>
    </div>
    <div id="createUserForm" class="user-creation-container" style="display: none;">
      <h3>Registration</h3>
      <div class="content">
        <form action="/createUser" id="userForm" method="post">
          <div class="user-details">
            <div class="input-box">
              <span class="details">Full Name</span>
              <input type="text" name="name"  id="name" placeholder="Enter your name" required>
            </div>
            <div class="input-box">
              <span class="details">Username</span>
              <input type="text" name="username" id="username" placeholder="Enter your username" required>
            </div>
            <div class="input-box">
              <span class="details">Email</span>
              <input type="text" name="email" id="email" placeholder="Enter your email" required>
            </div>
            <div class="input-box">
              <span class="details">Password</span>
              <input type="password" name="password" id="password" placeholder="Enter your password" required>
            </div>
            <div class="input-box">
              <span class="details">Confirm Password</span>
              <input type="password" name="confirmPassword" id="confirmPassword" placeholder="Confirm your password" required>
            </div>
            <div class="input-box">
              <span class="details">Job Title</span>
              <select name="jobTitle" id="jobTitle">
                <option value="admin">Admin</option>
                <option value="user">User</option>
              </select>
            </div>
          </div>
          <input type="hidden" name="csrf_token" value="<%= csrfToken %>">
          <div class="button">
            <input type="submit" value="Register">
          </div>
        </form>
      </div>
    </div>
 
    <div id="additional-text" style="display: none;">
      <div class="condition">
        <span>Conditions for creating a user:</span>
        <ul>
          <li class="error">Username has to be different.</li>
          <li class="error">Email has to be different.</li>
          <li class="error">Password must be at least 10 characters long and include at least one uppercase letter, one lowercase letter, one digit, and one symbol.</li>
        </ul>
      </div>
    </div>

    <div id="resetPasswordFormContainer" style="display: none;">
      <h3>Reset Password</h3>
      <div id="resetPasswordForm" class="user-creation-container">
        <div class="content">
          <form id="resetPasswordForm">
            <div class="user-details">
              <div class="input-box">
                <span class="details">Username</span>
                <input type="text" name="username" id="resetUsername" placeholder="Enter username" required>
              </div>
              <div class="input-box">
                <span class="details">New Password</span>
                <input type="password" name="password" id="resetPassword" placeholder="Enter new password" required>
              </div>
              <div class="input-box">
                <span class="details">Confirm Password</span>
                <input type="password" name="confirmPassword" id="resetConfirmPassword" placeholder="Confirm new password" required>
              </div>
            </div>
            <input type="hidden" name="csrf_token" value="<%= csrfToken %>">
            <div class="button">
              <input type="submit" value="Reset Password">
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
  <div id="additional-text2" style="display: none;">
    <div class="condition">
      <span>Please remind user to reset their password again for safety reason</span>
      <span>Conditions for Reseting user Password:</span>
      <ul>
        <li class="error">Password must be at least 10 characters long and include at least one uppercase letter, one lowercase letter, one digit, and one symbol.</li>
      </ul>
    </div>
  </div>
    <div id="deleteUserContainer" style="display: none;">
      <h3>Delete User</h3>
      <div class="search-container">
        <input type="text" id="searchUserInput" placeholder="Search by username">
        <button id="searchUserButton">Search</button>
      </div>
      <div id="searchResultsContainer" style="display: none;">
        <h4>Search Results</h4>
        <ul id="searchResultsList">
          <input type="hidden" name="csrf_token" value="<%= csrfToken %>">
        </ul>
      </div>
    </div>
    <div id="additional-text3" style="display: none;">
      <div class="condition">
        <span>Please ensure correct user is being deleted user cannot be retived again upon deletion</span>
      </div>
    </div>
    <div id="logsContainer" style="display: none;">
      <!-- Content for logs will be added here -->
      
    </div>
    
    <script>
      const allUsers = <%- JSON.stringify(allUsers) %>;
      const currentUsername = '<%= currentUsername %>';
    </script>
 
  <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.4/xlsx.full.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/exceljs/4.2.1/exceljs.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.js"></script>
  <script src="inusers.js"></script>
 

</body>

</html>
