<h1>Users</h1>

<% if (msg) { %><div class="flash"><%= msg %></div><% } %>
<% if (err) { %><div class="flash err"><%= err %></div><% } %>

<div class="card">
  <table>
    <thead><tr><th>Username</th><th>Role</th><th>Projects</th><th>Created</th><th>Reset password</th><th></th></tr></thead>
    <tbody>
      <% users.forEach(function (u) { %>
        <tr>
          <td><strong><%= u.username %></strong><%= u.id === user.id ? ' (you)' : '' %></td>
          <td><span class="chip <%= u.role === 'admin' ? 'live' : 'queued' %>"><%= u.role %></span></td>
          <td><%= u.project_count %></td>
          <td class="muted"><%= h.fmtTime(u.created_at) %></td>
          <td>
            <form class="inline" method="post" action="/users/<%= u.id %>/password">
              <input type="password" name="password" placeholder="new password (min 8)" minlength="8" required style="width:180px">
              <button class="btn small" type="submit">Reset</button>
            </form>
          </td>
          <td>
            <% if (u.id !== user.id) { %>
              <form class="inline" method="post" action="/users/<%= u.id %>/delete" onsubmit="return confirm('Delete user <%= u.username %>?')">
                <button class="btn small danger" type="submit">Delete</button>
              </form>
            <% } %>
          </td>
        </tr>
      <% }) %>
    </tbody>
  </table>
</div>

<div class="card">
  <h2>Add user</h2>
  <form method="post" action="/users">
    <label for="u-name">Username</label>
    <input id="u-name" type="text" name="username" required pattern="[a-z][a-z0-9_.\-]{1,30}" placeholder="lowercase">
    <label for="u-pass">Password</label>
    <input id="u-pass" type="password" name="password" minlength="8" required>
    <label for="u-role">Role</label>
    <select id="u-role" name="role">
      <option value="user">user — own projects, groups and git accounts</option>
      <option value="admin">admin — additionally manages users, sees all projects</option>
    </select>
    <div style="margin-top:14px"><button class="btn primary" type="submit">Create user</button></div>
  </form>
  <div class="hint">Users with projects cannot be deleted — remove or hand over their projects first.</div>
</div>
