<?php
function admin_user_view()
{
  $display = '
    <div class="view_card">
      <div class="row_space_between">
        <h3>Users</h3>
        <a href="admin.php?view=articles">View Articles</a>
      </div>
      <div class="line"></div>
      <form method="post" enctype="multipart/form-data">
        <input type="hidden" name="form_id" value="users">
        <div class="row_space_between">
          <label for="delete" class="form_checkbox_container">Delete
            <input type="radio" class="button" name="action" id="delete" value="delete">
            <span class="checkmark"></span>
          </label>

          <label id="create_users_from_csv_label" for="create_users_from_csv" class="upload_button">Upload a CSV</label>
          <input id="create_users_from_csv" class="file_input" name="csv" type="file" accept=".csv">
          <small class="file_input_feedback">No file selected.</small>

          <input type="submit" class="button narrow" value="Commit">
        </div>

        <table>
          <tr>
            <th>Select</th>
            <th>User ID</th>
            <th>Username</th>
            <th>Email</th>
            <th>Role</th>
            <th>Created</th>
            <th>Last Active</th>
            <th>Active</th>
            <th>Profile Picture</th>
          </tr>
  ';
  $conn = get_connection();
  $results = $conn->query('SELECT user_id, username, email, last_active, role, created_at, is_active, profile_picture FROM user INNER JOIN roles ON user.role_id = roles.role_id;');

  foreach ($results as $key => $result) {
    $display .= '
          <tr data-href="user.php?view=display&user_id=' . $result['user_id'] . '">
            <td class="excluded_cell">
              <label class="form_checkbox_container">
                <input name="selected_users[]" value="' . $result['user_id'] . '" type="checkbox">
                <span class="checkmark"></span>
              </label>
            </td>
            <td>' . $result['user_id'] . '</td>
            <td>' . htmlspecialchars($result['username']) . '</td>
            <td>' . htmlspecialchars($result['email']) . '</td>
            <td>' . ucwords($result['role']) . '</td>
            <td>' . $result['created_at'] . '</td>
            <td>' . $result['last_active'] . '</td>
            <td>' . $result['is_active'] . '</td>
            <td><img src="' . $_ENV['PROFILE_IMAGES_PQ_PATH'] . $result['profile_picture'] . '" class="card_profile_picture"></td>
          </tr>
    ';
  }

  $display .= '
        </table>

      </form>
    </div>
  ';

  return $display;
}
