{{define "content"}}
<h1>{{if .IsEdit}}Edit user{{else}}Create user{{end}}</h1>

{{template "back_link" (back "/users" "Back to users")}}

<div class="card narrow">
  {{if .Error}}<p class="error">{{.Error}}</p>{{end}}
  <form method="post" action="{{if .IsEdit}}/users/{{.UserID}}{{else}}/users/new{{end}}">
    <label for="username">Username</label>
    <input id="username" name="username" autocomplete="username"
           autocapitalize="none" spellcheck="false" value="{{.FormUsername}}" required>

    <label for="password">Password{{if .IsEdit}} (leave empty to keep){{end}}</label>
    <input id="password" name="password" type="password" autocomplete="new-password" {{if not .IsEdit}}required{{end}}>

    <label for="role">Role</label>
    {{if .LastGlobalLocked}}<input type="hidden" name="role" value="{{.FormRole}}">{{end}}
    <select id="role" name="role" data-global-role="{{.GlobalRole}}"{{if .LastGlobalLocked}} disabled{{end}}>
      <option value="domain_admin" {{if eq .FormRole "domain_admin"}}selected{{end}}>Domain administrator</option>
      <option value="global" {{if eq .FormRole "global"}}selected{{end}}>Global administrator</option>
    </select>
    {{if .LastGlobalLocked}}<p class="muted">The only global administrator cannot be demoted.</p>{{end}}

    <fieldset id="domain-pick" data-domain-pick>
      <legend>Assigned domains</legend>
      <p class="muted">Required for domain administrators.</p>
      {{range .Domains}}
      <label class="check">
        <input type="checkbox" name="domain_ids" value="{{.ID}}" {{if index $.FormDomains .ID}}checked{{end}}>
        {{.Name}}
      </label>
      {{end}}
    </fieldset>

    <button type="submit">{{if .IsEdit}}Save{{else}}Create{{end}}</button>
  </form>
  {{if .IsEdit}}
  {{if .LastGlobalLocked}}
  <p class="muted">The only global administrator cannot be deleted.</p>
  {{else}}
  <a class="danger" href="/users/{{.UserID}}/delete">Delete user</a>
  {{end}}
  {{end}}
</div>
{{end}}
